blob: 04208a26f3e74931fba0d0e5f6166119d9aca0ba (
plain)
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
|
import sys
import re
from collections import defaultdict
D = open(sys.argv[1]).read().strip()
lines = D.split("\n")
G = [[c for c in line] for line in lines]
R = len(G)
C = len(G[0])
p1 = 0
nums = defaultdict(list)
for r in range(len(G)):
gears = set() # positions of '*' characters next to the current number
n = 0
has_part = False
for c in range(len(G[r]) + 1):
if c < C and G[r][c].isdigit():
n = n * 10 + int(G[r][c])
for rr in [-1, 0, 1]:
for cc in [-1, 0, 1]:
if 0 <= r + rr < R and 0 <= c + cc < C:
ch = G[r + rr][c + cc]
if not ch.isdigit() and ch != ".":
has_part = True
if ch == "*":
gears.add((r + rr, c + cc))
elif n > 0:
for gear in gears:
nums[gear].append(n)
if has_part:
p1 += n
n = 0
has_part = False
gears = set()
print(p1)
p2 = 0
for k, v in nums.items():
if len(v) == 2:
p2 += v[0] * v[1]
print(p2)
|