summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2021-12-20 05:45:59 +0000
committerTomasz Kramkowski <tk@the-tk.com>2021-12-20 05:45:59 +0000
commit8e251ba55bd1e07bc4107782a1f9c4bc780beeb7 (patch)
treee6530ae0847f345c1cafffb04d63d95e6db6c37f
parent5a8384ba7872c289fcf232dc19bd0e115b819372 (diff)
downloadaoc2021-8e251ba55bd1e07bc4107782a1f9c4bc780beeb7.tar.gz
aoc2021-8e251ba55bd1e07bc4107782a1f9c4bc780beeb7.tar.xz
aoc2021-8e251ba55bd1e07bc4107782a1f9c4bc780beeb7.zip
day 20
-rw-r--r--20.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/20.py b/20.py
new file mode 100644
index 0000000..19c09c3
--- /dev/null
+++ b/20.py
@@ -0,0 +1,42 @@
+from utils import open_day
+
+bg = False
+img = dict()
+with open_day(20) as f:
+ first, rest = f.read().rstrip().split('\n\n')
+ alg = [c == '#' for c in first]
+ 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
+
+def iter():
+ global bg, img
+ new = dict()
+ minx, maxx = float('inf'), float('-inf')
+ miny, maxy = float('inf'), float('-inf')
+ for p, v in img.items():
+ if not v: continue
+ x, y = p
+ minx, maxx = min(minx, x), max(maxx, x)
+ miny, maxy = min(miny, y), max(maxy, y)
+ for y in range(miny - 1, maxy + 2):
+ for x in range(minx - 1, maxx + 2):
+ new[(x, y)] = alg[bits_at(img, x, y)]
+ img = new
+ bg = not bg
+
+iter()
+iter()
+
+print(sum(img.values()))
+
+for _ in range(48): iter()
+
+print(sum(img.values()))