advent-of-code/2023/day12.py

39 lines
979 B
Python
Raw Normal View History

2023-12-12 06:32:12 +00:00
import itertools
2023-12-04 18:32:41 +00:00
import sys
from collections import defaultdict
from dataclasses import dataclass
2023-12-12 06:32:12 +00:00
from tqdm import tqdm
2023-12-04 18:32:41 +00:00
lines = sys.stdin.read().splitlines()
2023-12-12 06:32:12 +00:00
count = 0
for line in lines:
parts = line.split(" ")
pattern = parts[0]
counts = [int(c) for c in parts[1].split(",")]
missing = [i for i in range(len(pattern)) if pattern[i] == "?"]
for replacements in itertools.product(".#", repeat=len(missing)):
c_pattern = list(pattern)
for i_missing, replacement in zip(missing, replacements):
c_pattern[i_missing] = replacement
parts = [p for p in "".join(c_pattern).split(".") if p]
if len(parts) == len(counts) and all(
len(p) == c for p, c in zip(parts, counts)
):
# print("".join(c_pattern), counts)
count += 1
2023-12-04 18:32:41 +00:00
# part 1
2023-12-12 06:32:12 +00:00
answer_1 = count
2023-12-04 18:32:41 +00:00
print(f"answer 1 is {answer_1}")
# part 2
answer_2 = ...
print(f"answer 2 is {answer_2}")
2023-12-12 06:32:12 +00:00
print(f"answer 2 is {answer_2}")