diff options
-rw-r--r-- | 20.py | 15 |
1 files changed, 6 insertions, 9 deletions
@@ -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())) |