diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-08 14:37:48 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-08 14:37:48 +0000 |
commit | 3b35e9173e43462196629d6f84dc47da78646c62 (patch) | |
tree | e6bc5ed8e6a436217460e66c4c6e72b09fc9a7e7 | |
parent | 8e2ac7c1e6ea2653bf3afb68c367f5b78396c181 (diff) | |
download | aoc2021-3b35e9173e43462196629d6f84dc47da78646c62.tar.gz aoc2021-3b35e9173e43462196629d6f84dc47da78646c62.tar.xz aoc2021-3b35e9173e43462196629d6f84dc47da78646c62.zip |
day 8: cleaner
-rw-r--r-- | 8.py | 29 |
1 files changed, 9 insertions, 20 deletions
@@ -26,16 +26,7 @@ dig = [ fset((Seg.A, Seg.B, Seg.C, Seg.D, Seg.E, Seg.F, Seg.G)), fset((Seg.A, Seg.B, Seg.C, Seg.D, Seg.F, Seg.G)), ] - -seg = [ - fset((0, 2, 3, 5, 6, 7, 8, 9)), - fset((0, 4, 5, 6, 8, 9)), - fset((0, 1, 2, 3, 4, 7, 8, 9)), - fset((2, 3, 4, 5, 6, 8, 9)), - fset((0, 2, 6, 8)), - fset((0, 1, 3, 4, 5, 6, 7, 8, 9)), - fset((0, 2, 3, 5, 6, 8, 9)), -] +seg = [ fset(i for i in range(10) if seg in dig[i]) for seg in Seg ] counts = Counter() count = 0 @@ -47,24 +38,22 @@ with open_day(8) as f: count: seg for seg, count in Counter(chain.from_iterable(patterns)).items() } - def patbylen(l): return next(p for p in patterns if len(p) == l) - CF = patbylen(len(dig[1])) - BCDF = patbylen(len(dig[4])) - ACF = patbylen(len(dig[7])) - ABCDEFG = patbylen(len(dig[8])) + lenmap = { len(pat): pat for pat in patterns } + CF = lenmap[len(dig[1])] + BCDF = lenmap[len(dig[4])] + ACF = lenmap[len(dig[7])] + ABCDEFG = lenmap[len(dig[8])] segmap = dict() segmap[Seg.B] = countmap[len(seg[Seg.B])] segmap[Seg.E] = countmap[len(seg[Seg.E])] segmap[Seg.F] = countmap[len(seg[Seg.F])] - def single(s): - assert(len(s) == 1) - return next(iter(s)) + def single(s): return next(iter(s)) segmap[Seg.A] = single(ACF - CF) segmap[Seg.C] = single(CF - fset(segmap[Seg.F])) segmap[Seg.D] = single(BCDF - CF - fset(segmap[Seg.B])) segmap[Seg.G] = single(ABCDEFG - fset(segmap.values())) - digmap = { fset(segmap[s] for s in dig[i]): i for i in range(10) } - digits = [digmap[fset(d)] for d in digits.split()] + digmap = { fset(segmap[s] for s in d): i for i, d in enumerate(dig) } + digits = [ digmap[fset(d)] for d in digits.split() ] counts.update(digits) count += sum(d * 10 ** i for i, d in enumerate(reversed(digits))) print(counts[1] + counts[4] + counts[7] + counts[8]) |