Skip to content

Commit

Permalink
Day 12
Browse files Browse the repository at this point in the history
  • Loading branch information
Ted Cassirer committed Jan 4, 2024
1 parent 7c5efb3 commit 6a1874c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
47 changes: 47 additions & 0 deletions aoc_cas/aoc2023/day12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import re
from functools import cache
from typing import Iterator


def resolve(line: str, groups: list[int]) -> int:
@cache
def inner(i0: int, i1: int) -> int:
if i1 == len(groups):
return 1 if "#" not in line[i0:] else 0
if i0 >= len(line):
return 0
pattern = re.compile(rf"[#?]{{{groups[i1]}}}([.?]|$)")
if pattern.match(line, pos=i0):
result = inner(i0 + groups[i1] + 1, i1 + 1)
else:
result = 0
if line[i0] != "#":
result += inner(i0 + 1, i1)
return result

return inner(0, 0)


def unfold(line: str, groups: list[int]) -> tuple[str, list[int]]:
return "?".join([line] * 5), groups * 5


def parse(data: str) -> Iterator[tuple[str, list[int]]]:
for line in data.splitlines():
springs, nums_str = line.split(" ")
nums = [int(c) for c in nums_str.split(",")]
yield springs, nums


def part_a(data: str) -> int:
return sum(resolve(springs, nums) for springs, nums in parse(data))


def part_b(data: str) -> int:
return sum(resolve(*unfold(*d)) for d in parse(data))


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples

solve_with_examples(year=2023, day=12)
8 changes: 8 additions & 0 deletions tests/fixtures/2023/12.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1
21
525152

0 comments on commit 6a1874c

Please sign in to comment.