advent2023/twelve.py

39 lines
1.0 KiB
Python
Executable File

#!/usr/bin/env python3
from useful import *
OFF = 0
ON = 1
MAYBE = 2
def munge(line):
l, r = line.split()
hint = [int(n) for n in r.split(',')]
puzzle = [ON if c == '#' else
MAYBE if c == '?' else OFF for c in l]
return puzzle, hint
def hint_from_puzzle(puzzle):
def _eh():
for state, group in groupby(puzzle):
if state == ON:
yield sum(group)
return [h for h in _eh()]
def cor_rec_t(left, right, solution):
"""
e.g. [ON, OFF] [MAYBE, MAYBE, ON] [1, 2] should recur into
[ON, OFF, {ON, OFF}, {ON, OFF}, ON], [] and return 0 + 0 + 1 + 0, ducy
"""
if not right:
if hint_from_puzzle(left) == solution:
return 1
return 0
if right[0] == MAYBE:
return (cor_rec_t(left + [ON,], right[1:], solution) +
cor_rec_t(left + [OFF,], right[1:], solution))
return cor_rec_t(left + right[:1], right[1:], solution)
phs = [munge(line) for line in lines(open(0))]
print(sum(cor_rec_t([], p, h) for p, h in phs))