-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_GearRatios.py
37 lines (28 loc) · 966 Bytes
/
03_GearRatios.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
from lib import *
inp = get_input()
p1 = 0
gears = {}
for y in range(len(inp)):
num = ""
has_sym = False
num_gears = set()
for x in range(len(inp[y])):
if inp[y][x].isdigit():
num += inp[y][x]
for yy, xx in iterate_positions(y, x, get_positions(), inp):
if inp[yy][xx] != "." and not inp[yy][xx].isdigit():
has_sym = True
if inp[yy][xx] == "*":
num_gears.add((yy, xx))
if (x + 1) > (len(inp[y]) - 1) or not inp[y][x + 1].isdigit():
if has_sym:
num = int(num)
p1 += num
for yyy, xxx in num_gears:
gears.setdefault((yyy, xxx), []).append(num)
num = ""
has_sym = False
num_gears = set()
p2 = sum(nums[0] * nums[1] for nums in gears.values() if len(nums) == 2)
print(p1)
print(p2)