2023/day12.py
2023-12-29 22:03:50 +01:00

34 lines
940 B
Python

import fileinput
import math
from pprint import pprint
from itertools import chain, combinations, count, groupby
import re
RE = re.compile(r"[#\?]")
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
def main():
(part1, part2) = (0, 0)
inp = fileinput.input()
for line in inp:
(springs, groups) = line.split()
groups = [int(g) for g in groups.split(",")]
unknowns = [x for (x, c) in enumerate(springs) if c == "?"]
springs_ = springs.replace("?", ".")
for xs in powerset(unknowns):
h = "".join("#" if i in xs else c for (i, c) in enumerate(springs_))
match = [len(g) for g in RE.findall(h)]
if match == groups:
part1 += 1
print(part1, part2)
if __name__ == "__main__":
main()