-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_solution.py
97 lines (80 loc) · 2.19 KB
/
1_solution.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
print(chr(27)+'[2j')
print('\033c', end='')
lines = open('./22_day/my.input', 'r').read().split('\n')
lines = lines[:-1]
test = [
'..#',
'#..',
'...',
]
# lines = test
middle = len(lines) // 2
grid = {}
for y, line in enumerate(lines):
for x, c in enumerate(line):
grid[(x, y)] = c
initial_grid = grid.copy()
start = (middle, middle)
def print_grid(grid, current_position):
min_x = min([x for x, _ in grid.keys()])
max_x = max([x for x, _ in grid.keys()])
min_y = min([y for _, y in grid.keys()])
max_y = max([y for _, y in grid.keys()])
for y in range(min_y, max_y + 1):
for x in range(min_x, max_x + 1):
if (x, y) == current_position:
if grid[(x, y)] == '#':
print('X', end='')
else:
print('x', end='')
else:
print(grid[(x, y)], end='')
print()
directions = [
(0, -1),
(1, 0),
(0, 1),
(-1, 0),
]
# Part 1
current_direction = 0
pos = start
infections = 0
for i in range(10000):
if pos not in grid:
grid[pos] = '.'
if grid[pos] == '#':
current_direction = (current_direction + 1) % 4
grid[pos] = '.'
else:
current_direction = (current_direction - 1) % 4
grid[pos] = '#'
infections += 1
pos = (pos[0] + directions[current_direction][0],
pos[1] + directions[current_direction][1])
print("Solution part 1:", infections)
# Part 2
current_direction = 0
pos = start
infections = 0
grid = initial_grid.copy()
for i in range(10000000):
if pos not in grid:
grid[pos] = '.'
if grid[pos] == '#':
current_direction = (current_direction + 1) % 4
grid[pos] = 'F'
elif grid[pos] == '.':
current_direction = (current_direction - 1) % 4
grid[pos] = 'W'
elif grid[pos] == 'W':
grid[pos] = '#'
infections += 1
elif grid[pos] == 'F':
current_direction = (current_direction + 2) % 4
grid[pos] = '.'
else:
raise Exception("Unknown state")
pos = (pos[0] + directions[current_direction][0],
pos[1] + directions[current_direction][1])
print("Solution part 2:", infections)