Skip to content

Commit

Permalink
2023-03
Browse files Browse the repository at this point in the history
  • Loading branch information
Viliam Pucik authored and Viliam Pucik committed Dec 18, 2023
1 parent b8fdb90 commit 0eeba38
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 2023/03.py
Original file line number Diff line number Diff line change
@@ -0,0 1,27 @@
#!/usr/bin/env python
import re
from collections import defaultdict
from math import prod


def parse(lines):
re_number = re.compile(r"\d ")
re_symbol = re.compile(r"[^\d.]")

for y, line in enumerate(lines):
for m in re_number.finditer(line):
number, x0, x1 = int(m.group()), m.start() - 1, m.end() 1
for yy in range(max(y - 1, 0), min(y 2, len(lines))):
for m in re_symbol.finditer(lines[yy], x0, x1):
yield number, m.group(), m.start(), yy


s1, gears = 0, defaultdict(list)

for number, symbol, x, y in parse(open(0).read().splitlines()):
s1 = number
if symbol == "*":
gears[x, y].append(number)

print(s1)
print(sum(prod(numbers) for numbers in gears.values() if len(numbers) == 2))

0 comments on commit 0eeba38

Please sign in to comment.