Skip to content

Commit

Permalink
Merge pull request #98 from wimglenn/2023/12
Browse files Browse the repository at this point in the history
2023/12
  • Loading branch information
wimglenn authored Dec 14, 2023
2 parents de5e495 + 572ef2f commit 8995eb8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
39 changes: 39 additions & 0 deletions aoc_wim/aoc2023/q12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
--- Day 12: Hot Springs ---
https://adventofcode.com/2023/day/12
"""
from aocd import data
from functools import cache


@cache
def fits(ss, cs, h=0):
if not any(cs):
return "#" not in ss
if not ss:
return 0
c, cs = cs[-1], cs[:-1]
s, ss = ss[-1], ss[:-1]
if not c:
return (s != "#") and fits(ss, cs, 0)
if h:
return (s != ".") and fits(ss, (*cs, c-1), 1)
if s == "#":
return fits(ss, (*cs, c - 1), 1)
if s == ".":
return fits(ss, (*cs, c), 0)
if s == "?":
return fits(ss, (*cs, c), 0) + fits(ss, (*cs, c - 1), 1)


a = b = 0
for line in data.splitlines():
template_a, counts = line.split()
counts_a = *map(int, counts.split(",")),
template_b = "?".join([template_a]*5)
counts_b = counts_a*5
a += fits(template_a, counts_a)
b += fits(template_b, counts_b)

print("answer_a:", a)
print("answer_b:", b)
8 changes: 8 additions & 0 deletions tests/2023/12/0.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 8995eb8

Please sign in to comment.