-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path201818.py
86 lines (72 loc) · 1.86 KB
/
201818.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from collections import defaultdict
DIM = 50
initial = defaultdict(lambda: ".")
with open("201818.txt", "r") as f:
for y, line in enumerate(f.readlines()):
for x, c in enumerate(line.strip()):
initial[x + y * 1j] = c
def get_adj_counts(p):
lumberyards = 0
trees = 0
for idx in [
p - 1 - 1j,
p - 1,
p - 1 + 1j,
p - 1j,
p + 1j,
p + 1 - 1j,
p + 1,
p + 1 + 1j,
]:
if idx not in map:
continue
elif map[idx] == "|":
trees += 1
elif map[idx] == "#":
lumberyards += 1
return lumberyards, trees
def evolve(m):
n = defaultdict(lambda: ".")
for y in range(0, DIM):
for x in range(0, DIM):
p = x + y * 1j
c = m[p]
lumberyards, trees = get_adj_counts(p)
if c == "." and trees >= 3:
n[p] = "|"
elif c == "|":
if lumberyards >= 3:
n[p] = "#"
else:
n[p] = "|"
elif c == "#" and lumberyards > 0 and trees > 0:
n[p] = "#"
return n
def score(m):
lumberyards = 0
trees = 0
for c in m.values():
if c == "|":
trees += 1
elif c == "#":
lumberyards += 1
return lumberyards * trees
def debug_print_map(m):
for y in range(0, DIM):
for x in range(0, DIM):
print(m[x + y * 1j], end="")
print()
print()
map = initial
scores = defaultdict(set)
for i in range(1000000):
map = evolve(map)
s = score(map)
if i == 9:
print(f"Part One: {s}")
if s in scores and len(scores[s]) > 1:
period = i - max(scores[s])
if (i + 1) % period == 1000000000 % period:
print(f"Part Two: {s}")
break
scores[s].add(i)