You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.6 KiB
63 lines
1.6 KiB
import itertools
|
|
from functools import cache
|
|
|
|
lines = [line.strip("\n") for line in open("inputs/day12.txt").readlines()]
|
|
|
|
def encodings(lines: list[str]) -> tuple:
|
|
damaged = []
|
|
alt = []
|
|
for line in lines:
|
|
line = line.split(" ")
|
|
damaged.append(line[0])
|
|
alt.append([int(x) for x in line[1].split(",")])
|
|
return damaged, alt
|
|
|
|
def qm_idx(string: str) -> list[int]:
|
|
idx = [i for i, s in enumerate(string) if s == "?"]
|
|
return idx
|
|
|
|
def matches(string: str, enc: list[int]) -> bool:
|
|
groups = []
|
|
counter = 0
|
|
cont = False
|
|
for i, s in enumerate(string):
|
|
if s == "#":
|
|
counter += 1
|
|
cont = True
|
|
if i == len(string) - 1:
|
|
groups.append(counter)
|
|
else:
|
|
if cont:
|
|
cont = False
|
|
groups.append(counter)
|
|
counter = 0
|
|
return groups == enc
|
|
|
|
def combinations(string: str) -> list[str]:
|
|
ids = qm_idx(string)
|
|
combs = []
|
|
for comb in itertools.product("#.", repeat=len(ids)):
|
|
newstring = string
|
|
for i, c in zip(ids, comb):
|
|
newstring = newstring[:i] + c + newstring[i+1:]
|
|
combs.append(newstring)
|
|
return combs
|
|
|
|
@cache
|
|
def count_matches(string: str, alt: list[int]) -> int:
|
|
combs = combinations(string)
|
|
counter = 0
|
|
for comb in combs:
|
|
if matches(comb, alt):
|
|
counter += 1
|
|
return counter
|
|
|
|
def main():
|
|
dmgd, alts = encodings(lines)
|
|
counts = 0
|
|
for dmg, alt in zip(dmgd, alts):
|
|
counts += count_matches(dmg, alt)
|
|
print(f"Solution Part 1: {counts}")
|
|
# part 2 missing
|
|
main()
|