-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.py
111 lines (96 loc) · 3.44 KB
/
day11.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
def open_file(file):
with open(file, 'r+') as f:
lines = f.readlines()
return lines
def get_graph(input):
graph = {}
for row in range(len(input)): # row is 0 to 9
for col in range(len(input[0])-1): # col is 0 to 9
graph[(row,col)] = {}
graph[(row,col)]['energy'] = int(input[row][col])
graph[(row,col)]['flashed'] = False
graph[(row,col)]['neighbors'] = []
if row != 0: graph[(row,col)]['neighbors'].append((row-1,col)) # left
if row != len(input)-1: graph[(row,col)]['neighbors'].append((row+1,col)) # right
if col != 0: graph[(row,col)]['neighbors'].append((row, col-1)) # up
if col != len(input[0])-2: graph[(row,col)]['neighbors'].append((row, col+1)) # down
if row != 0 and col != 0: graph[(row,col)]['neighbors'].append((row-1, col-1)) # top-left
if row != 0 and col != len(input[0])-2: graph[(row,col)]['neighbors'].append((row-1, col+1)) # bottom-left
if row != len(input)-1 and col != 0: graph[(row,col)]['neighbors'].append((row+1, col-1)) # top-right
if row != len(input)-1 and col != len(input[0])-2: graph[(row,col)]['neighbors'].append((row+1, col+1)) # bottom-right
return graph
def print_graph(graph):
for row in range(100):
try:
test = graph[row,0]
for col in range(100):
try:
print(graph[(row,col)]['energy'], end='')
except KeyError:
print()
break
except KeyError:
return
def solver(part):
input = open_file('input/day11.txt')
graph = get_graph(input)
# print(graph)
if part == 1:
steps = 100
total_flashes = 0
for i in range(steps):
for octopus in graph:
graph[octopus]['energy'] += 1
done = False
while not done:
for octopus in graph:
if graph[octopus]['energy'] > 9:
graph[octopus]['energy'] = 0
graph[octopus]['flashed'] = True
total_flashes += 1
for neighbor in graph[octopus]['neighbors']:
if graph[neighbor]['flashed'] == False:
graph[neighbor]['energy'] += 1
for octopus in graph:
done = True
if graph[octopus]['energy'] > 9:
done = False
break
# Reset 'flashed' status at the end of the step
for octopus in graph:
graph[octopus]['flashed'] = False
print_graph(graph)
return total_flashes
elif part == 2:
total_steps = 0
while True:
for octopus in graph:
graph[octopus]['energy'] += 1
done = False
while not done:
for octopus in graph:
if graph[octopus]['energy'] > 9:
graph[octopus]['energy'] = 0
graph[octopus]['flashed'] = True
for neighbor in graph[octopus]['neighbors']:
if graph[neighbor]['flashed'] == False:
graph[neighbor]['energy'] += 1
for octopus in graph:
done = True
if graph[octopus]['energy'] > 9:
done = False
break
total_steps += 1
# check if all have flashed
everything_flashed = True
for octopus in graph:
if graph[octopus]['flashed'] == False:
everything_flashed = False
if everything_flashed:
return total_steps
# Reset 'flashed' status at the end of the step
for octopus in graph:
graph[octopus]['flashed'] = False
print_graph(graph)
print("Part 1: ", solver(1))
print("Part 2: ", solver(2))