From d0085fa59347640e202a0f8c1ace272406caa7f0 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Mon, 20 Dec 2021 05:50:23 +0000 Subject: day 20: cleanup --- 20.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/20.py b/20.py index 19c09c3..707c2b0 100644 --- a/20.py +++ b/20.py @@ -5,16 +5,17 @@ img = dict() with open_day(20) as f: first, rest = f.read().rstrip().split('\n\n') alg = [c == '#' for c in first] + assert(len(alg) == 512) for y, line in enumerate(rest.split('\n')): for x, c in enumerate(line): img[(x, y)] = c == '#' def bits_at(img, x, y): bits = [] - for dx, dy in ((-1, -1), (0, -1), (1, -1), (-1, 0), (0, 0), (1, 0), (-1, 1), (0, 1), (1, 1)): - bits.append(img.get((x + dx, y + dy), bg)) - val = int(''.join('1' if b else '0' for b in bits), 2) - return val + for dy in range(-1, 2): + for dx in range(-1, 2): + bits.append(img.get((x + dx, y + dy), bg)) + return sum(b * 2 ** i for i, b in enumerate(reversed(bits))) def iter(): global bg, img @@ -32,11 +33,7 @@ def iter(): img = new bg = not bg -iter() -iter() - +for _ in range(2): iter() print(sum(img.values())) - for _ in range(48): iter() - print(sum(img.values())) -- cgit v1.2.3-54-g00ecf