12a
This commit is contained in:
parent
0c6a0075db
commit
e91d2a88dc
|
@ -0,0 +1,6 @@
|
|||
???.### 1,1,3
|
||||
.??..??...?##. 1,1,3
|
||||
?#?#?#?#?#?#?#? 1,3,1,6
|
||||
????.#...#... 4,1,1
|
||||
????.######..#####. 1,6,5
|
||||
?###???????? 3,2,1
|
|
@ -0,0 +1,38 @@
|
|||
#!/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))
|
Loading…
Reference in New Issue