Skip to content

Commit

Permalink
Day 7: Bridge Repair
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 7, 2024
1 parent 9a681d5 commit 99ecc10
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ Development occurs in language-specific directories:
|[Day4.hs](hs/src/Day4.hs)|[Day4.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day4.kt)|[day4.py](py/aoc2024/day4.py)|[day4.rs](rs/src/day4.rs)|
|[Day5.hs](hs/src/Day5.hs)|[Day5.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day5.kt)|[day5.py](py/aoc2024/day5.py)|[day5.rs](rs/src/day5.rs)|
|[Day6.hs](hs/src/Day6.hs)|[Day6.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day6.kt)|[day6.py](py/aoc2024/day6.py)|[day6.rs](rs/src/day6.rs)|
|[Day7.hs](hs/src/Day7.hs)|[Day7.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day7.kt)|||
|[Day7.hs](hs/src/Day7.hs)|[Day7.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day7.kt)|[day7.py](py/aoc2024/day7.py)||
76 changes: 76 additions & 0 deletions py/aoc2024/day7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
Day 7:
"""

from typing import Callable, Iterable

SAMPLE_INPUT = """
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20
"""


def _solve(data: str, op: Callable[[int, int], Iterable[int]]) -> int:
total = 0
for line in filter(None, data.splitlines()):
lhs, rhs = line.split(":", maxsplit=1)
lhs, rhs = int(lhs), [int(value) for value in rhs.split()]

def ok(x, remaining):
y = remaining[-1]
return (
x == y
if len(remaining) == 1
else any(ok(z, remaining[:-1]) for z in op(x, y))
)

if ok(lhs, rhs):
total += lhs
return total


def part1(data: str) -> int:
"""
>>> part1(SAMPLE_INPUT)
3749
"""

def op(x, y):
values = []
if x >= y:
values.append(x - y)
if not x % y:
values.append(x // y)
return values

return _solve(data, op)


def part2(data: str) -> int:
"""
>>> part2(SAMPLE_INPUT)
11387
"""

def op(x, y):
values = []
if x >= y:
values.append(x - y)
if not x % y:
values.append(x // y)
x, y = str(x), str(y)
if x.endswith(y) and len(x) > len(y):
values.append(int(x[: -len(y)]))
return values

return _solve(data, op)


parts = (part1, part2)
1 change: 1 addition & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ day3 = "aoc2024.day3:parts"
day4 = "aoc2024.day4:parts"
day5 = "aoc2024.day5:parts"
day6 = "aoc2024.day6:parts"
day7 = "aoc2024.day7:parts"

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit 99ecc10

Please sign in to comment.