parent
000ad0f9ec
commit
fb6b307e2e
@ -0,0 +1,62 @@
|
|||||||
|
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()
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,6 @@
|
|||||||
|
???.### 1,1,3
|
||||||
|
.??..??...?##. 1,1,3
|
||||||
|
?#?#?#?#?#?#?#? 1,3,1,6
|
||||||
|
????.#...#... 4,1,1
|
||||||
|
????.######..#####. 1,6,5
|
||||||
|
?###???????? 3,2,1
|
Loading…
Reference in new issue