Refactor 2021 for new UI.
Some checks failed
continuous-integration/drone/pr Build is failing
continuous-integration/drone/push Build is failing

This commit is contained in:
Mikaël Capelle
2024-12-08 14:03:34 +01:00
parent cd96140378
commit 1b4dd32898
25 changed files with 310 additions and 355 deletions

View File

@@ -1,8 +1,7 @@
import itertools
import os
import sys
from typing import Any, Iterator
VERBOSE = os.getenv("AOC_VERBOSE") == "True"
from ..base import BaseSolver
digits = {
"abcefg": 0,
@@ -17,71 +16,74 @@ digits = {
"abcdfg": 9,
}
lines = sys.stdin.read().splitlines()
# part 1
lengths = {len(k) for k, v in digits.items() if v in (1, 4, 7, 8)}
answer_1 = sum(
len(p) in lengths for line in lines for p in line.split("|")[1].strip().split()
)
print(f"answer 1 is {answer_1}")
class Solver(BaseSolver):
def solve(self, input: str) -> Iterator[Any]:
lines = input.splitlines()
# part 2
values: list[int] = []
# part 1
lengths = {len(k) for k, v in digits.items() if v in (1, 4, 7, 8)}
yield sum(
len(p) in lengths
for line in lines
for p in line.split("|")[1].strip().split()
)
for line in lines:
parts = line.split("|")
broken_digits = sorted(parts[0].strip().split(), key=len)
# part 2
values: list[int] = []
per_length = {
k: list(v)
for k, v in itertools.groupby(sorted(broken_digits, key=len), key=len)
}
for line in lines:
parts = line.split("|")
broken_digits = sorted(parts[0].strip().split(), key=len)
# a can be found immediately
a = next(u for u in per_length[3][0] if u not in per_length[2][0])
per_length = {
k: list(v)
for k, v in itertools.groupby(sorted(broken_digits, key=len), key=len)
}
# c and f have only two possible values corresponding to the single entry of
# length 2
cf = list(per_length[2][0])
# a can be found immediately
a = next(u for u in per_length[3][0] if u not in per_length[2][0])
# the only digit of length 4 contains bcdf, so we can deduce bd by removing cf
bd = [u for u in per_length[4][0] if u not in cf]
# c and f have only two possible values corresponding to the single entry of
# length 2
cf = list(per_length[2][0])
# the 3 digits of length 5 have a, d and g in common
adg = [u for u in per_length[5][0] if all(u in pe for pe in per_length[5][1:])]
# the only digit of length 4 contains bcdf, so we can deduce bd by removing cf
bd = [u for u in per_length[4][0] if u not in cf]
# we can remove a
dg = [u for u in adg if u != a]
# the 3 digits of length 5 have a, d and g in common
adg = [
u for u in per_length[5][0] if all(u in pe for pe in per_length[5][1:])
]
# we can deduce d and g
d = next(u for u in dg if u in bd)
g = next(u for u in dg if u != d)
# we can remove a
dg = [u for u in adg if u != a]
# then b
b = next(u for u in bd if u != d)
# we can deduce d and g
d = next(u for u in dg if u in bd)
g = next(u for u in dg if u != d)
# f is in the three 6-length digits, while c is only in 2
f = next(u for u in cf if all(u in p for p in per_length[6]))
# then b
b = next(u for u in bd if u != d)
# c is not f
c = next(u for u in cf if u != f)
# f is in the three 6-length digits, while c is only in 2
f = next(u for u in cf if all(u in p for p in per_length[6]))
# e is the last one
e = next(u for u in "abcdefg" if u not in {a, b, c, d, f, g})
# c is not f
c = next(u for u in cf if u != f)
mapping = dict(zip((a, b, c, d, e, f, g), "abcdefg"))
# e is the last one
e = next(u for u in "abcdefg" if u not in {a, b, c, d, f, g})
value = 0
for number in parts[1].strip().split():
digit = "".join(sorted(mapping[c] for c in number))
value = 10 * value + digits[digit]
mapping = dict(zip((a, b, c, d, e, f, g), "abcdefg"))
if VERBOSE:
print(value)
value = 0
for number in parts[1].strip().split():
digit = "".join(sorted(mapping[c] for c in number))
value = 10 * value + digits[digit]
values.append(value)
self.logger.info(f"value for '{line}' is {value}")
values.append(value)
answer_2 = sum(values)
print(f"answer 2 is {answer_2}")
yield sum(values)