-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22.py
58 lines (48 loc) · 1.02 KB
/
22.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
from lib import *
input = read_input(2017, 22)
lines = input.splitlines()
infected = set()
x, y = len(lines[0]) // 2, len(lines) // 2
dx, dy = 0, -1
out = 0
for i, line in enumerate(lines):
for j, c in enumerate(line):
if c == "#":
infected.add((j, i))
for _ in range(10000):
inf = (x, y) in infected
if inf:
dx, dy = -dy, dx
infected.remove((x, y))
else:
dx, dy = dy, -dx
infected.add((x, y))
out += 1
x += dx
y += dy
print(out)
state = {}
x, y = len(lines[0]) // 2, len(lines) // 2
dx, dy = 0, -1
out = 0
for i, line in enumerate(lines):
for j, c in enumerate(line):
if c == "#":
state[(j, i)] = 2
for _ in range(10000000):
s = state.get((x, y), 0)
if s == 0:
dx, dy = dy, -dx
elif s == 2:
dx, dy = -dy, dx
elif s == 3:
dx, dy = -dx, -dy
if s == 1:
out += 1
if s == 3:
state.pop((x, y))
else:
state[(x, y)] = s + 1
x += dx
y += dy
print(out)