From f234fc62bffd90e1218683901e2c9625d7cfd58c Mon Sep 17 00:00:00 2001 From: Fedaya Date: Tue, 12 Dec 2023 08:06:58 +0100 Subject: [PATCH] Added template + day12 in progress --- day12/common.py | 66 ++++++++++++++++++++++++++++++++++++++ day12/example_input.txt | 6 ++++ day12/part1.py | 13 ++++++++ day12/part2.py | 3 ++ day12/test.py | 31 ++++++++++++++++++ template/common.py | 2 ++ template/example_input.txt | 0 template/part1.py | 3 ++ template/part2.py | 3 ++ template/test.py | 21 ++++++++++++ 10 files changed, 148 insertions(+) create mode 100644 day12/common.py create mode 100644 day12/example_input.txt create mode 100755 day12/part1.py create mode 100755 day12/part2.py create mode 100755 day12/test.py create mode 100644 template/common.py create mode 100644 template/example_input.txt create mode 100755 template/part1.py create mode 100755 template/part2.py create mode 100755 template/test.py diff --git a/day12/common.py b/day12/common.py new file mode 100644 index 0000000..3acc00a --- /dev/null +++ b/day12/common.py @@ -0,0 +1,66 @@ +from dataclasses import dataclass +from typing import NamedTuple, NewType +import re + + +def im_replace(line: str, mask: str) -> str: + idx = 0 + new_line = "" + for c in line: + if c != "?": + new_line += c + else: + new_line += mask[idx] + idx += 1 + return new_line + + +class SpringLine(NamedTuple): + line: str + groups: tuple[int, ...] + + def arrangments(self) -> list[str]: + count_im = self.line.count("?") + print(count_im) + possibles = [] + for i in range(pow(2, count_im)): + rep_str = f"{i:b}".rjust(count_im, "0").replace("0", ".").replace("1", "#") + print(rep_str, end=" / ") + tentative = im_replace(self.line, rep_str) + possible = SpringLine(tentative, self.groups) + print(possible, end=": ") + if possible.is_valid(): + possibles.append(im_replace(self.line, rep_str)) + print("valid") + else: + print("invalid") + 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 + + +Spring = NewType("Spring", list[SpringLine]) + + +def parse(input_file: str) -> Spring: + spring_lines = [] + 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)) + spring_lines.append( + SpringLine( + match.group(1), + tuple(map(lambda c: int(c), match.group(2).split(","))), + ) + ) + return Spring(spring_lines) diff --git a/day12/example_input.txt b/day12/example_input.txt new file mode 100644 index 0000000..c5bec3a --- /dev/null +++ b/day12/example_input.txt @@ -0,0 +1,6 @@ +???.### 1,1,3 +.??..??...?##. 1,1,3 +?#?#?#?#?#?#?#? 1,3,1,6 +????.#...#... 4,1,1 +????.######..#####. 1,6,5 +?###???????? 3,2,1 \ No newline at end of file diff --git a/day12/part1.py b/day12/part1.py new file mode 100755 index 0000000..52cd0c3 --- /dev/null +++ b/day12/part1.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3.11 + +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) diff --git a/day12/part2.py b/day12/part2.py new file mode 100755 index 0000000..4367f5b --- /dev/null +++ b/day12/part2.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python3.11 + +from common import * diff --git a/day12/test.py b/day12/test.py new file mode 100755 index 0000000..9e1d082 --- /dev/null +++ b/day12/test.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3.11 + +import logging +from unittest import TestCase, main +from common import * +from part1 import part1 + + +class DayXTests(TestCase): + def test_parsing(self): + parsed_spring = parse("example_input.txt") + spring: Spring = [ + SpringLine("???.###", (1, 1, 3)), + SpringLine(".??..??...?##.", (1, 1, 3)), + SpringLine("?#?#?#?#?#?#?#?", (1, 3, 1, 6)), + SpringLine("????.#...#...", (4, 1, 1)), + SpringLine("????.######..#####.", (1, 6, 5)), + SpringLine("?###????????", (3, 2, 1)), + ] + self.assertEqual(parsed_spring, spring) + + def test_part1(self): + self.assertEqual(part1("example_input.txt"), 21) + + def test_part2(self): + pass + + +if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG) + main(verbosity=2) diff --git a/template/common.py b/template/common.py new file mode 100644 index 0000000..dcbad20 --- /dev/null +++ b/template/common.py @@ -0,0 +1,2 @@ +def parse(input_file: str) -> T: + pass diff --git a/template/example_input.txt b/template/example_input.txt new file mode 100644 index 0000000..e69de29 diff --git a/template/part1.py b/template/part1.py new file mode 100755 index 0000000..4367f5b --- /dev/null +++ b/template/part1.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python3.11 + +from common import * diff --git a/template/part2.py b/template/part2.py new file mode 100755 index 0000000..4367f5b --- /dev/null +++ b/template/part2.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python3.11 + +from common import * diff --git a/template/test.py b/template/test.py new file mode 100755 index 0000000..2cc4ef8 --- /dev/null +++ b/template/test.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3.11 + +from unittest import TestCase, main +import logging +from common import * + + +class DayXTests(TestCase): + def test_parsing(self): + pass + + def test_part1(self): + pass + + def test_part2(self): + pass + + +if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG) + main(verbosity=2)