-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22.py
88 lines (70 loc) · 1.83 KB
/
day22.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
file = open('day22.txt').read().split()
size = 400
dimx, dimy = len(file), len(file[0])
x, y = int(dimx / 2), int(dimy / 2)
x, y = x + size, y + size
# 0 1 2 3 are the directions, starting 0 is up, clockwise
facing = 0
lines = list()
for _ in range(size):
lines.append("." * (2 * size + len(file)))
for a in range(len(file)):
lines.append('.' * size + file[a] + '.' * size)
for _ in range(size):
lines.append("." * (2 * size + len(file)))
copy = list()
for line in lines:
copy.append(line)
add = 0
for _ in range(10000):
if lines[y][x] == "#":
facing += 1
facing %= 4
lines[y] = lines[y][:x] + '.' + lines[y][x + 1:]
elif lines[y][x] == ".":
facing -= 1
facing %= 4
lines[y] = lines[y][:x] + '#' + lines[y][x + 1:]
add += 1
if facing == 0:
y -= 1
elif facing == 1:
x += 1
elif facing == 2:
y += 1
elif facing == 3:
x -= 1
print("part1:", add)
dimx, dimy = len(file), len(file[0])
x, y = int(dimx / 2), int(dimy / 2)
x, y = x + size, y + size
facing = 0
add = 0
lines = list()
for line in copy:
lines.append(line)
for _ in range(10000000):
if lines[y][x] == "#": # infected
facing += 1
facing %= 4
lines[y] = lines[y][:x] + 'F' + lines[y][x + 1:]
elif lines[y][x] == "W": # weakened
lines[y] = lines[y][:x] + '#' + lines[y][x + 1:]
add += 1
elif lines[y][x] == "F": # flagged
facing += 2
facing %= 4
lines[y] = lines[y][:x] + '.' + lines[y][x + 1:]
elif lines[y][x] == ".": # clean
facing -= 1
facing %= 4
lines[y] = lines[y][:x] + 'W' + lines[y][x + 1:]
if facing == 0:
y -= 1
elif facing == 1:
x += 1
elif facing == 2:
y += 1
elif facing == 3:
x -= 1
print("part2:", add)