-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.py
executable file
·269 lines (246 loc) · 22.5 KB
/
03.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python3
import string
from collections import defaultdict
from typing import List
def parser(text) -> list:
return [line for line in text.split('\n')]
def read_input() -> str:
with open(__file__, encoding="utf-8") as f:
c = f.read()
data = c[c.rindex("🎅") + 1: c.rindex("⛄")].strip()
return data
# MAIN FUNCTIONS
def neighbors(x, y, h, w, exclude: set):
return [(x + i, y + j) for i in (-1, 0, 1) for j in (-1, 0, 1)
if (i, j) != (0, 0) and -1 < x + i < h and -1 < y + j < w and (x + i, y + j) not in exclude]
def part1(grid: List[str]) -> int:
w, h = len(grid[0]), len(grid)
integer, int_coords = [], set()
nums_with_coords = [] # tuple(integer, {(x1,y1), (x2,y2)})
special_chars = set()
for x, row in enumerate(grid):
for y, col in enumerate(row):
if col in string.digits:
integer.append(col)
int_coords.add((x, y))
else:
if integer and int_coords:
nums_with_coords.append((int(''.join(integer)), int_coords))
integer, int_coords = [], set() # reset for next number
if col != '.':
special_chars.add((x, y))
result = 0
for num, coords in nums_with_coords:
for xn, yn in coords:
for n in neighbors(xn, yn, h, w, coords):
if n in special_chars:
result += num
else:
continue
break
else:
continue
break
return result
def part2(grid: List[str]) -> int:
w, h = len(grid[0]), len(grid)
integer, int_coords = [], set()
nums_with_coords = [] # tuple({(x1,y1), (x2,y2)}, integer)
gears = set()
for x, row in enumerate(grid):
for y, col in enumerate(row):
if col in string.digits:
integer.append(col)
int_coords.add((x, y))
else:
if integer and int_coords:
nums_with_coords.append((int_coords, int(''.join(integer))))
integer, int_coords = [], set() # reset for next number
if col == '*':
gears.add((x, y))
possible_parts = defaultdict(list)
for coords, num in nums_with_coords:
for xn, yn in coords:
for n in neighbors(xn, yn, h, w, coords):
if n in gears:
possible_parts[n].append(num)
else:
continue
break
else:
continue
break
return sum(p[0] * p[1] for p in possible_parts.values() if len(p) == 2)
# TEST
def test():
# GIVEN
given = parser("""
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
""".strip())
assert part1(given) == 4361
# part 2
assert part2(given) == 467835
return True
if __name__ == '__main__':
assert test()
lines = parser(read_input())
# ONE #1
part_1 = part1(lines)
print(part_1)
assert part_1 == 517021
# # TWO #2
part_2 = part2(lines)
print(part_2)
assert part_2 == 81296995
# INPUT
"""🎅
.......262....300...................507.....961..............668.....................189.906...........................624..................
..148.................805..130..880*...........*684.............*......*..............*..-......%.................$........17...65....91*...
......272.....464.....=......*.........................208*.....260.967.38.......692*[email protected].#[email protected].
.................*734........772......171.........728$.....52........................747.=866............350...........*....................
..328....21...........941......../.....*[email protected]$...623.................654..#.....*815....360......812%.........
..........$.....%[email protected]...&[email protected]......................+...............156.........817...../.................340....
.....883........359.....*.*..........................*............@............................./...920............302.712...........*......
........*.....*......100..192.160........791......843...680*643.252.................322...354..........*.....+.........*............556.....
.........958.580.................*.............................................879.....-..$............633...63......561.....289............
..............................745.......221..504-................356..............*827........355...............................*46.........
[email protected]........./.......................*..........535.....................515.....
..201.....336.......-..*.....268..121..775...-...........%..............791.............129...449.....450..*.........790...........*........
............%.559*......7.....+..*..........865......895............930.....427..242......#...............414.830............37..809........
..................85.............466..550-..........+....................44...*...*.........361..215..........*......808.220*...............
................*......709....................577......430...&..148........*.696.988....801.*.....*..........202..25..*.....................
.....636......14...........372.............64...*...........586.%...15..241.............&....387..447...............*..811....146...........
.....-...........892*226....*....161.........%.623..................*...........$...............................................&.....657...
........987.................147.=....+.254..........887........773...321.....221....415.971..................74.......702..212..............
[email protected].....*...........*..77.......#......................./.....29..........%...........&..*.......872......
......643............*...................756.........677.*..........251......521..620......................................685.206.......132
............274*...........929.................871........686.542..%........*......*.......................882....756.............*859..*...
................516.........*.....................*..............*...289...10.....890..985...258...963...............*.................526..
.............................610...............857........210..818......*.......@.........#..=......*.....968.........42..........732.......
.............../..*453.................467..........376......$..........429..218.....................955..%..............357................
.......841....493..............830#.......*342.........*........*................./....562....945................224.......*..435...........
..................843.803......................2........56...248.560.............281.....*.......*.........-........*5....728..*......156...
...822*288....352.*...+..............$.........$........................@..............794......539..-814..528.81.......+.......687.....*...
................*.783..../454.859....809..742........................663...267/..................................*.....847..............167.
....&371.296.484..............*..........=...........516.......925...............................#................441...............224.....
.........#............594..417....458............112*.....65.....#.....337...357....#..369*277.825....386..................212..............
................651..#...........*.....$..................*............*.....*...125...................*......497.........*..............509
......320..........*......-...883..989..733.....668.905...997.......454.....353...........946....18.....340...&......515...774........*.....
......-.....880....476...955.......=................*...........58.....................23....*69.+..102..............*.............933.687..
........931....*..............................757....602.983......-..822..............*.............*.......732.....15......................
....../....*....463.............183$..961........*.......*....#[email protected]......*....&429..
...204......550............-.........%.........628.....240..588............*................................/..........*......167.776.......
................360....*....69..........*..........534..................%..340.....................885*912..52......949.....................
.........483......*..46.754......-....607.346.....*..............851.302..................@....................-........330.-201............
.........@...274.811..............819...........56......326......................496....650...........382......168.420...*.........452......
..............#........325.449........581...........112*........................+............163.......*.................407.......*...460..
......595.........*......*...................226+.................268..153.........#123........*.....$..877..........#..........361...*.....
......*........280.287.427...........755...........................%.........................722...323................307...................
..132.553...............................*.......117......450.109...............849.....749.................................105..537#..741...
...*.................................788.......*....691...%...$............148.....960..*....512.728#..544.624.....$......*.................
.523..97...186..907....508....................739......*................*.....*856.&....15..................$.....911.......&..877..632.....
..............*.*.........*.........161*....+........381.............453.376..........................30..................806.......*.......
............560.877...773...............168..446..............465..............435@..24..622...%.................................=..347.....
...*165................-.......................................*......@816..............*....313..421......269....539.49.530..113...........
768......*626.926....=.....676...981=............703.........682.&141....................444......=........@.........*......*......539......
......159.......$..362......../.......254.....14..=...............................219.................383.........*......182.............733
.324........879.....................*.=...............316.....*480......*............*............439*.........621.961.......*481...........
....*...208*...........188.......763.....553...........*...957.......771.944........445......4...........375..............174.....903.665...
.................651........851.......37*...........303.................................446......61......*......346................@...*....
......160....412*......../.....=.220.........354.........-.....838...........473..........#........$.870.......*............*........391....
.....*....................218.....*.............*.....739..320*.........358....*...521......383.........*.....27...........107..............
...969...718..................+....200.534.237+..........................=..889....*[email protected]...
........&..................841...-......&........./748...655..325.................430............813.................618.....687...#..@.....
..........825...761............800......................*....&...........496............652...$..*...............206..*.....................
...................+....&584.........35..663*590.....677...................*........741*....908...293..918............570....$......%.......
...........794.......................&...................852*...........361...............-.................................502.769.82......
..535.......$............523*522................-..............847..........593..........267.....................................*..........
....*...........73...............................299..262..941*..................813..............682..........877........494...411.........
..528................*997....*...477........464.....................521........@....*315......#...=..............*..........................
............669...909.......342..*...........*..........444@[email protected]..................
............*.....................444.....921................783.......*.......................................*........=...414.............
..........622..................................422..-...............404......&[email protected]@...
....77.........519...282........720.....159=..*....87......225.605......125...........+.............*..860-......................=..........
...$...556.......-.....*..........#..........479............&....*.........*967...343..............400...........*883..................*881.
......*............207.653....113...359............227............357............*..........771...........426.905..............313..563.....
...74..233....959../.........*.........&............*...#.................543..64..*....179................*........@.............*.........
.....*..........*.......707.341..........&489.....206..132..407..........*........224..*................310........255./.........136........
...251.......770........................................................475.............694....514.....................236..................
........944........-379...............403.....934......47........................................*..............694........359..............
732.............14..........*..........*.........*.................720....325..............375.483.............*.....356&..............686..
.......160..672*.........250.125....791.....&76...1.....&963...422..#........../....191......*................925..........771...405&.-.....
...205....%......402................................117........*.........887....509..........44.....417.................=.-.................
...................*............=..145......746.......*........861..804....*.........313...............*....*.........761.......638.........
..852..767.....#67.835......$.359..*..................789.@................675.938.............957..........459.652..............*...949....
........*.................977.....779.......348...627.....136.........+623..........964.............948+..........*.................=.......
........882......+............................./...#...............................*......311...152.......186......233.......374.......989..
..148.........125.....931......706%....#...379.........................181.......227..........*..........*..............581..*..............
.....*..179..............*.............970.../.......605...........736*......................700......970.................*.........+.487...
....209...*..............810....790..............787*......................#...+....-...824......587......709*.....899.....283....324..*....
...........50...750............./.......633...............................364..66..593./......................115......-..............561...
...................#......989..............*217.......................620...........................................740.....*361....+.......
..........266.........647*....$..992....*...........318...345...........*..............-........252..797.................354........8...110.
.............................375.....344.334.......*.......*..........460......558.....364.......#..$...........@....%..........3...........
...$14...314...652...............................626............%...................%.....................778....868..215...................
...........&....*.......$........241.................*796....552..738..#[email protected]....*..................................
................594..230..-..647*.....&............56.............*............479.....921..............519................426......713.....
.....944*................404........53..349....$........459......986..+......=..............-666..............237...897#..*........&....755.
.........176........280......714..........*..876.268...*.............946.492.113..........*..............................855................
.....................*........*...936..................973.................*[email protected].....
[email protected]........./...................185..........630.285............196...../....*...*....349....102....*....*......
........765..677................*..46....85.901.....*...440...................../..........*.............304..952............1........507...
...........=.........669......847....*........*..821......*.301.......880.................829.951.460....................876................
.................466*......*.........69.....207.........419...*...................442............*...........-.....898.....=.667............
....421.56=................318....................................178...............+...............896*855...522..&..........%....151*405..
....*.......481-...70*401..............550...................*.......*296...91........998.566..112..........................................
...235.472..............................*......962....963.935...921.........+.....................-...........168.....=516.304...416*699....
........#...*10.................240=.....687..*...................*.475..-...........697.......................*..............*.............
..261.....47.......750....954.........=......274........-...203.570....*..71...........*...484..*.....%61...397..854...#.......523.....931..
..............34............$....%.....500........216.647............271.......908....906.......488...............*..92..634................
..........83*....................994.......511.55*........991...16.........417*....*......96...................697.................&........
.............582.450=.....................................=.....*...............654.411........313..................29.........32.849.......
.......563..............239.........&.............405............193......+.411.........%......*...&.....623.........$....+748..=.....840...
..........&......#.......-....761....639.275+.......*...................69..$....../[email protected]...*.....366................../......
.............248.16............/...............+...556...........306............354......308..............682.../......894...#..............
..697.......*[email protected].......................*337...................72....108...............*......991.344....781
...*.......715...49.392.........*.......*................652/........#.......55*.......................-.844......./..344..........-........
...591....................334.701.......838.......................397....501....759.....-.........................946.......442.............
..................557*[email protected]..........#170..........%............941..373....425..818.......................828.....237.
..793*144...........................$.302....*.....938*..................................*..........*....%.378........66..63...*............
............474...............893............267.......810................585..........558.......694......../.................340.701.......
..............#..798......332*.....855.....................592.589........&.....833=.........199......730.......+......177..................
.................*..................*.........................*........................#.......*......*.....154.589...*.....................
.....64*809........../[email protected]....../.......771.........534........281...........*......506.......=............
..............427.750......546...98............707..=......&..381..877..*..........*........6.........%......489.........../.795............
...895.805*.....*.....................*694....=........848...............616.....69.86.....*..460......895.......544.....651.......14.886...
...........390...850..777.534..138..52..........570....@.........812.$71................795.......557............-.....................*....
..46*41...............*...........*[email protected]*.................584.......=........*...............106*....627.602....829.
........217*380.............721..928.873...........................593..353...............341..430..989..923.............695......-...%.....
..................586.......$.........*...207.......585&......485..*...*..........................*......*..........................929.....
............#.154*.....60.............95.......................*..138.764..........................425.275......%.331*664..854..........887.
908.917..359........../........659........125..785...........499..........991....358..........102*...........903..........*.....409....-....
...*.........963*.../......408.............%........830*871...............*......*...828..........764.671................293...*............
....................559....=..........81............................391...969.513....*.......991........&..518.....208*.......75............
.....461.560*......................../........950.........320%.....*................97........................#..........+675.....352...339.
......*......439.%[email protected]*862........339....................................*..........
...247...................@......*.................312....#.................................-...271..800...@..818.850....333*722...330..951..
........922.................184.666.............../..........................356...105.........#...*.....353./........................*.....
832.....-.......758...800....$......................337.......419...........=.......*...258........926.........................../592..186..
...................+...........220.287...117....783......646..#.........451........750..........27.....$.......*750......890................
........719............982....%.......*..*.........*144.*..................*...........*764./.....*373..368.201..........@..................
......../..........388....*........115...432.............594............345..658$...442......833.................408.........=..........542.
..........27.#498..*.....286..+490...............................966.....................262.......................#..542.237...............
72....251..#.......667.....................282..556..260...........%.$......................*....107..................*.....................
.......*.......*..............................*..-...*...............64..#[email protected][email protected]...
......85....882.80.......184$................117........................454..583......*..351...............266....................&....*....
.......................................................48...........................436........275...................869............258.....
⛄"""