summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--20.py15
1 files 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()))