day 12 part 1 completed
This commit is contained in:
parent
f234fc62bf
commit
a9d7c6a101
@ -1,7 +1,7 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import NamedTuple, NewType
|
||||
import re
|
||||
|
||||
import logging
|
||||
|
||||
def im_replace(line: str, mask: str) -> str:
|
||||
idx = 0
|
||||
@ -21,31 +21,27 @@ class SpringLine(NamedTuple):
|
||||
|
||||
def arrangments(self) -> list[str]:
|
||||
count_im = self.line.count("?")
|
||||
print(count_im)
|
||||
logging.debug(count_im)
|
||||
possibles = []
|
||||
for i in range(pow(2, count_im)):
|
||||
debug_str = ""
|
||||
rep_str = f"{i:b}".rjust(count_im, "0").replace("0", ".").replace("1", "#")
|
||||
print(rep_str, end=" / ")
|
||||
debug_str += rep_str + " / "
|
||||
tentative = im_replace(self.line, rep_str)
|
||||
possible = SpringLine(tentative, self.groups)
|
||||
print(possible, end=": ")
|
||||
debug_str += str(possible)+": "
|
||||
if possible.is_valid():
|
||||
possibles.append(im_replace(self.line, rep_str))
|
||||
print("valid")
|
||||
debug_str += "valid"
|
||||
else:
|
||||
print("invalid")
|
||||
debug_str += "invalid"
|
||||
logging.debug(debug_str)
|
||||
return possibles
|
||||
|
||||
def is_valid(self) -> bool:
|
||||
# TODO: correct this (re.match with generated pattern based en groups)
|
||||
line_copy = self.line
|
||||
|
||||
for group in self.groups:
|
||||
idx = line_copy.find("#" * group)
|
||||
if idx == -1:
|
||||
return False
|
||||
line_copy = line_copy[idx + group :]
|
||||
return True
|
||||
pattern = "^\.*" + "\.+".join(["#" * i for i in self.groups]) + "\.*$"
|
||||
# logging.debug(pattern)
|
||||
return re.match(pattern, self.line)
|
||||
|
||||
|
||||
Spring = NewType("Spring", list[SpringLine])
|
||||
@ -56,7 +52,7 @@ def parse(input_file: str) -> Spring:
|
||||
with open(input_file, "r", encoding="utf-8") as inputfd:
|
||||
while line := inputfd.readline():
|
||||
if match := re.match("([?#.]+)\s([0-9,]+)", line):
|
||||
# print(match.group(0))
|
||||
# logging.debug(match.group(0))
|
||||
spring_lines.append(
|
||||
SpringLine(
|
||||
match.group(1),
|
||||
|
@ -5,9 +5,9 @@ from common import *
|
||||
|
||||
def part1(input_file: str) -> int:
|
||||
spring = parse(input_file)
|
||||
print(spring)
|
||||
interm = list(map(lambda s: s.arrangments(), spring))
|
||||
print(interm)
|
||||
interm = list(map(len, interm))
|
||||
print(interm)
|
||||
return sum(interm)
|
||||
#interm = list(map(lambda s: s.arrangments(), spring))
|
||||
#interm = list(map(len, interm))
|
||||
return sum(list(map(len, map(lambda s: s.arrangments(), spring))))
|
||||
|
||||
if __name__=="__main__":
|
||||
print(part1("input.txt"))
|
@ -1,3 +1,7 @@
|
||||
#!/usr/bin/env python3.11
|
||||
|
||||
from common import *
|
||||
|
||||
|
||||
def part2(input_file:str) -> int:
|
||||
return 0
|
@ -23,7 +23,7 @@ class DayXTests(TestCase):
|
||||
self.assertEqual(part1("example_input.txt"), 21)
|
||||
|
||||
def test_part2(self):
|
||||
pass
|
||||
self.assertEqual(part2("example_input.txt"), 525152)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
x
Reference in New Issue
Block a user