|
| 1 | +from collections import defaultdict |
| 2 | + |
| 3 | +def parse(s): |
| 4 | + i = 0 |
| 5 | + res = [] |
| 6 | + while s: |
| 7 | + if s[0] == "w": res.append((-1,0)); s = s[1:] |
| 8 | + elif s[0] == "e": res.append((1,0)); s = s[1:] |
| 9 | + elif s[0:2] == "se": res.append((1,-1)); s = s[2:] |
| 10 | + elif s[0:2] == "sw": res.append((0,-1)); s = s[2:] |
| 11 | + elif s[0:2] == "ne": res.append((0,1)); s = s[2:] |
| 12 | + elif s[0:2] == "nw": res.append((-1,1)); s = s[2:] |
| 13 | + return res |
| 14 | + |
| 15 | +with open("input.txt") as f: |
| 16 | + lines = [parse(x.strip()) for x in f] |
| 17 | + |
| 18 | +d = defaultdict(int) |
| 19 | + |
| 20 | +for line in lines: |
| 21 | + xs, ys = zip(*line) |
| 22 | + coords = sum(ys), sum(xs) |
| 23 | + d[coords] = 1 - d[coords] |
| 24 | + |
| 25 | +print(sum(d.values())) |
| 26 | + |
| 27 | +def apply(n, ns): |
| 28 | + if n == 0: |
| 29 | + if sum(ns) == 2: |
| 30 | + return 1 |
| 31 | + else: |
| 32 | + if sum(ns) == 0 or sum(ns) > 2: |
| 33 | + return 0 |
| 34 | + return n |
| 35 | + |
| 36 | +dirs = [(1,-1),(0,-1),(-1,1),(0,1),(1,0),(-1,0)] |
| 37 | +for _ in range(100): |
| 38 | + d2 = d.copy() |
| 39 | + for row in range(-100,100): |
| 40 | + for col in range(-100,100): |
| 41 | + coords = (row,col) |
| 42 | + d2[coords] = apply(d[coords], [d[k] for k in [(y+row, x+col) for x, y in dirs]]) |
| 43 | + d = d2 |
| 44 | + |
| 45 | +print(sum(d.values())) |
0 commit comments