Skip to content

Commit

Permalink
Day 3: Gear Ratios
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 3, 2023
1 parent 3cc4b7a commit 10ae7a4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Development occurs in language-specific directories:
|--:|--:|--:|--:|
|[Day1.hs](hs/src/Day1.hs)|[Day1.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day1.kt)|[day1.py](py/aoc2023/day1.py)|[day1.rs](rs/src/day1.rs)|
|[Day2.hs](hs/src/Day2.hs)|[Day2.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day2.kt)|[day2.py](py/aoc2023/day2.py)|[day2.rs](rs/src/day2.rs)|
|[Day3.hs](hs/src/Day3.hs)|[Day3.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day3.kt)|||
|[Day3.hs](hs/src/Day3.hs)|[Day3.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day3.kt)|[day3.py](py/aoc2023/day3.py)||
56 changes: 56 additions & 0 deletions py/aoc2023/day3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Day 3: Gear Ratios
"""

import re
from collections import defaultdict
from math import prod

SAMPLE_INPUT = """
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
"""

NUMBER_RE = re.compile(r"\d+")
SYMBOL_RE = re.compile(r"[^\s\d.]")


def _parse(data):
lines = data.splitlines()
for y, line in enumerate(lines):
for match in NUMBER_RE.finditer(line):
number = int(match.group(0))
x0, x1 = match.span()
for yy in range(max(y - 1, 0), min(y + 2, len(lines))):
for match in SYMBOL_RE.finditer(lines[yy], x0 - 1, x1 + 1):
yield match.start(), yy, number


def part1(data):
"""
>>> part1(SAMPLE_INPUT)
4361
"""
return sum(number for _, _, number in _parse(data))


def part2(data):
"""
>>> part2(SAMPLE_INPUT)
467835
"""
gears = defaultdict(list)
for x, y, number in _parse(data):
gears[x, y].append(number)
return sum(prod(numbers) for numbers in gears.values() if len(numbers) == 2)


parts = (part1, part2)
1 change: 1 addition & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ aoc2023 = "aoc2023.main:main"
[tool.poetry.plugins."aoc2023.days"]
day1 = "aoc2023.day1:parts"
day2 = "aoc2023.day2:parts"
day3 = "aoc2023.day3:parts"

[tool.black]
target_version = ["py312"]
Expand Down

0 comments on commit 10ae7a4

Please sign in to comment.