-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14_RestroomRedoubt.py
65 lines (50 loc) · 1.38 KB
/
14_RestroomRedoubt.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
# aoc:
# flags: png
# pypy: false
# ---
import collections, functools, numpy
import tqdm
from lib import *
CREATE_PNG = "png" in FLAGS
if CREATE_PNG:
from PIL import Image
inp = get_input()
R = {}
for i,l in enumerate(inp):
R[i] = [int(y) for x in l.split(" ") for y in x.split("=")[1].split(",")]
H,W = (7,11) if IS_EXAMPLE else (103,101)
MW, MH = W//2, H//2
def safety_factor(C):
Q = [0,0,0,0]
for (y,x),c in C.items():
if y < MH and x < MW:
Q[0] += c
elif y < MH and x > MW:
Q[1] += c
elif y > MH and x < MW:
Q[2] += c
elif y > MH and x > MW:
Q[3] += c
return functools.reduce(int.__mul__, Q)
VAR = {}
images = []
for t in tqdm.tqdm(range(1,max(W*H, 100)+1)):
C = collections.Counter()
for i, (px,py,vx,vy) in R.items():
py = (py + vy) % H
px = (px + vx) % W
R[i] = (px,py,vx,vy)
C[py,px] += 1
if CREATE_PNG:
img = Image.new("1", (W,H), "black")
for py,px in C:
img.putpixel((px,py), 1)
images.append(img)
if t == 100:
print(safety_factor(C))
VAR[t] = functools.reduce(numpy.add, numpy.std(list(C.keys()), axis=0))
# lowest standard derivation should be the time where the tree is visible
p2 = min(VAR.items(), key=lambda x: x[1])[0]
print(p2)
if CREATE_PNG:
images[p2-1].save("./out.png")