diff --git a/aoc_wim/aoc2023/q12.py b/aoc_wim/aoc2023/q12.py new file mode 100644 index 00000000..44b0ffcd --- /dev/null +++ b/aoc_wim/aoc2023/q12.py @@ -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) diff --git a/tests/2023/12/0.txt b/tests/2023/12/0.txt new file mode 100644 index 00000000..86418814 --- /dev/null +++ b/tests/2023/12/0.txt @@ -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