-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |