-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.py
205 lines (178 loc) · 6.73 KB
/
day03.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import aoc
"""
--- Day 3: Gear Ratios ---
You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source, but this is as far as he can bring you.
You go inside.
It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.
"Aaah!"
You turn around to see a slightly-greasy Elf with a wrench and a look of surprise.
"Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help.
The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one.
If you can add up all the part numbers in the engine schematic, it should be easy to work out which part is missing.
The engine schematic (your puzzle input) consists of a visual representation of the engine.
There are lots of numbers and symbols you don't really understand, but apparently any number adjacent to a symbol, even diagonally, is a "part number" and should be included in your sum.
(Periods (.) do not count as a symbol.)
Here is an example engine schematic:
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114 (top right) and 58 (middle right).
Every other number is adjacent to a symbol and so is a part number; their sum is 4361.
Of course, the actual engine schematic is much larger.
What is the sum of all of the part numbers in the engine schematic?
--- Part Two ---
The engineer finds the missing part and installs it in the engine!
As the engine springs to life, you jump in the closest gondola, finally ready to ascend to the water source.
You don't seem to be going very fast, though.
Maybe something is still wrong? Fortunately, the gondola has a phone labeled "help", so you pick it up and the engineer answers.
Before you can explain the situation, she suggests that you look out the window.
There stands the engineer, holding a phone in one hand and waving with the other.
You're going so slowly that you haven't even left the station.
You exit the gondola.
The missing part wasn't the only issue - one of the gears in the engine is wrong.
A gear is any * symbol that is adjacent to exactly two part numbers.
Its gear ratio is the result of multiplying those two numbers together.
This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out which gear needs to be replaced.
Consider the same engine schematic again:
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
In this schematic, there are two gears.
The first is in the top left; it has part numbers 467 and 35, so its gear ratio is 16345.
The second gear is in the lower right; its gear ratio is 451490.
(The * adjacent to 617 is not a gear because it is only adjacent to one part number.)
Adding up all of the gear ratios produces 467835.
What is the sum of all of the gear ratios in your engine schematic?
"""
def solvePart1(lines):
result = 0
grid = {}
width = len(lines[0].strip())
height = len(lines)
grid[0] = '.' * (width+2)
grid[height+1] = grid[0]
for i in range(0, len(lines)):
grid[i+1] = '.' + lines[i].strip() + '.'
for y in range(1, height + 1):
partNumber = 0
validNumber = False
for x in range(1, width+2):
c = grid[y][x]
if c.isdigit() == False:
if validNumber:
result += partNumber
partNumber = 0
validNumber = False
continue
# y - 1, x - 1
c = grid[y-1][x-1]
if c != '.' and c.isdigit() == False:
validNumber = True
# y - 1, x + 0
c = grid[y-1][x]
if c != '.' and c.isdigit() == False:
validNumber = True
# y - 1, x + 1
c = grid[y-1][x+1]
if c != '.' and c.isdigit() == False:
validNumber = True
# y + 0, x - 1
c = grid[y][x-1]
if c != '.' and c.isdigit() == False:
validNumber = True
# y + 0, x + 1
c = grid[y][x+1]
if c != '.' and c.isdigit() == False:
validNumber = True
# y + 1, x - 1
c = grid[y+1][x-1]
if c != '.' and c.isdigit() == False:
validNumber = True
# y + 1, x + 0
c = grid[y+1][x]
if c != '.' and c.isdigit() == False:
validNumber = True
# y + 1, x + 1
c = grid[y+1][x+1]
if c != '.' and c.isdigit() == False:
validNumber = True
partNumber = partNumber * 10 + int(grid[y][x])
return result
def solvePart2(lines):
result = 0
grid = {}
width = len(lines[0].strip())
height = len(lines)
grid[0] = '.' * (width+2)
grid[height+1] = grid[0]
for i in range(0, len(lines)):
grid[i+1] = '.' + lines[i].strip() + '.'
gears = {}
for y in range(1, height + 1):
partNumber = 0
gear = None
for x in range(1, width+2):
c = grid[y][x]
if c.isdigit() == False:
if gear != None:
if gears.get(gear) == None:
gears[gear] = []
gears[gear].append(partNumber)
partNumber = 0
gear = None
continue
# y - 1, x - 1
c = grid[y-1][x-1]
if c == '*':
gear = (y-1,x-1)
# y - 1, x + 0
c = grid[y-1][x]
if c == '*':
gear = (y-1,x)
# y - 1, x + 1
c = grid[y-1][x+1]
if c == '*':
gear = (y-1,x+1)
# y + 0, x - 1
c = grid[y][x-1]
if c == '*':
gear = (y,x-1)
# y + 0, x + 1
c = grid[y][x+1]
if c == '*':
gear = (y,x+1)
# y + 1, x - 1
c = grid[y+1][x-1]
if c == '*':
gear = (y+1,x-1)
# y + 1, x + 0
c = grid[y+1][x]
if c == '*':
gear = (y+1,x)
# y + 1, x + 1
c = grid[y+1][x+1]
if c == '*':
gear = (y+1,x+1)
partNumber = partNumber * 10 + int(grid[y][x])
for gear in gears:
if len(gears[gear]) == 2:
result += gears[gear][0] * gears[gear][1]
return result
def main():
aoc.run_day(3, solvePart1, 538046, solvePart2, 81709807)
if __name__ == '__main__':
main()