-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.py
executable file
·149 lines (112 loc) · 3.42 KB
/
day18.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
# [Day 18: Settlers of The North Pole](https://adventofcode.com/2018/day/18)
import sys
import time
from collections import Counter
from copy import deepcopy
from pathlib import Path
if animation := "-a" in sys.argv:
sys.argv.remove("-a")
if verbose := "-v" in sys.argv:
sys.argv.remove("-v")
if self_tests := "-T" in sys.argv:
sys.argv.remove("-T")
filename = ("test.txt" if sys.argv[1] == "-t" else sys.argv[1]) if len(sys.argv) > 1 else "input.txt"
data = Path(filename).read_text().strip()
lines = data.splitlines()
OPEN_ACRE = 0
TREE = 1
LUMBERYARD = 2
N = len(lines[0])
# read the area
area = bytearray(N * N)
for y, line in enumerate(lines):
for x, acre in enumerate(line):
area[y * N + x] = {".": OPEN_ACRE, "|": TREE, "#": LUMBERYARD}[acre]
def collect(area):
new_area = bytearray(N * N)
for y in range(N):
for x in range(N):
acre = area[y * N + x]
adjacents = Counter(
area[(y + dy) * N + x + dx]
for dx in range(-1, 2)
for dy in range(-1, 2)
if 0 <= x + dx < N and 0 <= y + dy < N and (dx, dy) != (0, 0)
)
if acre == OPEN_ACRE:
if adjacents[TREE] >= 3:
acre = TREE
elif acre == TREE:
if adjacents[LUMBERYARD] >= 3:
acre = LUMBERYARD
elif acre == LUMBERYARD:
if not (adjacents[TREE] >= 1 and adjacents[LUMBERYARD] >= 1):
acre = OPEN_ACRE
else:
raise ValueError
new_area[y * N + x] = acre
return new_area
def value(area):
trees = sum(1 for i in range(N * N) if area[i] == TREE)
lumberyards = sum(1 for i in range(N * N) if area[i] == LUMBERYARD)
return trees * lumberyards
def show(area):
ACRE = "\033[38:5:{n}m".format(n=231)
GREEN = "\033[1;32m"
BROWN = "\033[38;2;{r};{g};{b}m".format(r=165, g=42, b=42)
RESET = "\033[0m"
return (
f"{RESET}\n".join(
"".join((f"{ACRE}..", f"{GREEN}||", f"{BROWN}##")[area[y * N + x]] for x in range(N)) for y in range(N)
)
+ RESET
)
def animate(area):
a = deepcopy(area)
for i in range(500):
print("\033[H\033[2J", show(a))
time.sleep(0.010)
a = collect(a)
if animation:
import os
from imgcat import imgcat
from PIL import Image
frames = []
a = deepcopy(area)
for i in range(500):
frame = Image.new("RGB", (N * 4, N * 4))
for x in range(N):
for y in range(N):
c = ((0, 0, 0), (0, 255, 0), (165, 42, 42))[a[y * N + x]]
for k in range(16):
frame.putpixel((x * 4 + k // 4, y * 4 + k % 4), c)
frame.save(f"frame{i}.png")
frames.append(f"frame{i}.png")
a = collect(a)
os.system("magick -delay 3 -loop 0 " + " ".join(frames) + " lumberarea.gif")
for f in frames:
os.unlink(f)
if verbose:
animate(area)
# part 1
a = deepcopy(area)
for _ in range(10):
a = collect(a)
print(value(a))
# part 2
a = deepcopy(area)
seen = dict()
values = []
for i in range(1000):
values.append(value(a))
s = bytes(a)
if s in seen:
cycle_start = seen[s]
cycle_end = i
break
seen[s] = i
a = collect(a)
n = 1000000000
cycle = cycle_end - cycle_start
print(values[cycle_start + (n - cycle_end) % cycle])