-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.py
executable file
·56 lines (43 loc) · 1.23 KB
/
day3.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
#!/usr/bin/env python3
# [Day 3: Gear Ratios](https://adventofcode.com/2023/day/3)
import sys
from collections import defaultdict
from pathlib import Path
filename = ("test.txt" if sys.argv[1] == "-t" else sys.argv[1]) if len(sys.argv) > 1 else "input.txt"
data = Path(filename).read_text().strip()
lines = data.splitlines()
sx = len(lines[0])
sy = len(lines)
def has_symbol(x0, x1, y):
for yy in range(y - 1, y + 2):
for xx in range(x0 - 1, x1 + 1):
if yy == y and x0 <= xx < x1:
continue
if 0 <= xx < sx and 0 <= yy < sy:
c = lines[yy][xx]
if c != "." and not c.isdigit():
return c, yy, xx
return None
gears = defaultdict(list)
part1 = 0
for y in range(sy):
x = 0
while x < sx:
n = 0
x0 = x
while x < sx and lines[y][x].isdigit():
n = n * 10 + int(lines[y][x])
x += 1
if n != 0:
symbol = has_symbol(x0, x, y)
if symbol:
part1 += n
if symbol[0] == "*":
gears[symbol].append(n)
x += 1
print(part1)
part2 = 0
for g in gears.values():
if len(g) == 2:
part2 += g[0] * g[1]
print(part2)