diff --git a/day12/common.py b/day12/common.py index 3acc00a..bd15537 100644 --- a/day12/common.py +++ b/day12/common.py @@ -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), diff --git a/day12/part1.py b/day12/part1.py index 52cd0c3..2ef65d4 100755 --- a/day12/part1.py +++ b/day12/part1.py @@ -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")) \ No newline at end of file diff --git a/day12/part2.py b/day12/part2.py index 4367f5b..c98728b 100755 --- a/day12/part2.py +++ b/day12/part2.py @@ -1,3 +1,7 @@ #!/usr/bin/env python3.11 from common import * + + +def part2(input_file:str) -> int: + return 0 \ No newline at end of file diff --git a/day12/test.py b/day12/test.py index 9e1d082..43ffd6a 100755 --- a/day12/test.py +++ b/day12/test.py @@ -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__":