Day 14 (glamorized)
This commit is contained in:
@@ -1,27 +1,24 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import re
|
import re
|
||||||
from collections import Counter, defaultdict
|
from collections import Counter
|
||||||
|
from itertools import groupby
|
||||||
|
from operator import itemgetter
|
||||||
|
|
||||||
lines = (x.strip() for x in open("input.txt"))
|
lines = (x.strip() for x in open("input.txt"))
|
||||||
first = next(lines)
|
first = next(lines)
|
||||||
next(lines)
|
next(lines)
|
||||||
|
|
||||||
|
repl = (re.match(r"([A-Z]{2}) -> ([A-Z])", line).groups() for line in lines)
|
||||||
|
repl = (v for (a, b), insert in repl for v in [(a + insert, a + b), (insert + b, a + b)])
|
||||||
|
repl = {k: list(map(itemgetter(1), v)) for k, v in groupby(sorted(repl), itemgetter(0))}
|
||||||
|
|
||||||
combinations = Counter(first[a:a+2] for a in range(len(first)-1))
|
combinations = Counter(first[a:a+2] for a in range(len(first)-1))
|
||||||
replacements = defaultdict(list)
|
|
||||||
|
|
||||||
for line in lines:
|
|
||||||
(a, b), insert = re.match(r"([A-Z]{2}) -> ([A-Z])", line).groups()
|
|
||||||
replacements[a+insert].append(a+b)
|
|
||||||
replacements[insert+b].append(a+b)
|
|
||||||
|
|
||||||
for _ in range(10):
|
for _ in range(10):
|
||||||
combinations = {
|
combinations = {k: sum_val for k, v in repl.items() if (sum_val := sum(combinations.get(x, 0) for x in v)) > 0}
|
||||||
k: sum_val for k, v in replacements.items() if (sum_val := sum(combinations.get(x, 0) for x in v)) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
letters = Counter(first[0] + first[-1])
|
letters = ((l, cnt) for key, cnt in combinations.items() for l in key)
|
||||||
for key, cnt in combinations.items():
|
letters = {k: sum(map(itemgetter(1), v)) for k, v in groupby(sorted(letters), itemgetter(0))}
|
||||||
letters[key[0]] = letters.get(key[0], 0) + cnt
|
letters[first[0]] += 1
|
||||||
letters[key[1]] = letters.get(key[1], 0) + cnt
|
letters[first[-1]] += 1
|
||||||
|
|
||||||
print((max(letters.values()) - min(letters.values())) // 2)
|
print((max(letters.values()) - min(letters.values())) // 2)
|
||||||
|
|||||||
@@ -1,27 +1,24 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import re
|
import re
|
||||||
from collections import Counter, defaultdict
|
from collections import Counter
|
||||||
|
from itertools import groupby
|
||||||
|
from operator import itemgetter
|
||||||
|
|
||||||
lines = (x.strip() for x in open("input.txt"))
|
lines = (x.strip() for x in open("input.txt"))
|
||||||
first = next(lines)
|
first = next(lines)
|
||||||
next(lines)
|
next(lines)
|
||||||
|
|
||||||
|
repl = (re.match(r"([A-Z]{2}) -> ([A-Z])", line).groups() for line in lines)
|
||||||
|
repl = (v for (a, b), insert in repl for v in [(a + insert, a + b), (insert + b, a + b)])
|
||||||
|
repl = {k: list(map(itemgetter(1), v)) for k, v in groupby(sorted(repl), itemgetter(0))}
|
||||||
|
|
||||||
combinations = Counter(first[a:a+2] for a in range(len(first)-1))
|
combinations = Counter(first[a:a+2] for a in range(len(first)-1))
|
||||||
replacements = defaultdict(list)
|
|
||||||
|
|
||||||
for line in lines:
|
|
||||||
(a, b), insert = re.match(r"([A-Z]{2}) -> ([A-Z])", line).groups()
|
|
||||||
replacements[a+insert].append(a+b)
|
|
||||||
replacements[insert+b].append(a+b)
|
|
||||||
|
|
||||||
for _ in range(40):
|
for _ in range(40):
|
||||||
combinations = {
|
combinations = {k: sum_val for k, v in repl.items() if (sum_val := sum(combinations.get(x, 0) for x in v)) > 0}
|
||||||
k: sum_val for k, v in replacements.items() if (sum_val := sum(combinations.get(x, 0) for x in v)) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
letters = Counter(first[0] + first[-1])
|
letters = ((l, cnt) for key, cnt in combinations.items() for l in key)
|
||||||
for key, cnt in combinations.items():
|
letters = {k: sum(map(itemgetter(1), v)) for k, v in groupby(sorted(letters), itemgetter(0))}
|
||||||
letters[key[0]] = letters.get(key[0], 0) + cnt
|
letters[first[0]] += 1
|
||||||
letters[key[1]] = letters.get(key[1], 0) + cnt
|
letters[first[-1]] += 1
|
||||||
|
|
||||||
print((max(letters.values()) - min(letters.values())) // 2)
|
print((max(letters.values()) - min(letters.values())) // 2)
|
||||||
|
|||||||
Reference in New Issue
Block a user