From 6df1e1c3ec33aab80d847e7cfaf5e1b815ee7124 Mon Sep 17 00:00:00 2001 From: Fedaya Date: Wed, 13 Dec 2023 08:08:13 +0100 Subject: [PATCH] =?UTF-8?q?Changement=20d'ordi!=20(Non=20le=20jour=2012=20?= =?UTF-8?q?n'est=20pas=20termin=C3=A9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day12/common.py | 14 ++++++++++++-- day12/part2.py | 5 +++-- day12/test.py | 3 ++- day13/common.py | 35 +++++++++++++++++++++++++++++++++++ day13/example_input.txt | 15 +++++++++++++++ day13/part1.py | 7 +++++++ day13/part2.py | 7 +++++++ day13/test.py | 23 +++++++++++++++++++++++ get_today_puzzle.sh | 6 +++++- 9 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 day13/common.py create mode 100644 day13/example_input.txt create mode 100755 day13/part1.py create mode 100755 day13/part2.py create mode 100755 day13/test.py diff --git a/day12/common.py b/day12/common.py index bd15537..168dd53 100644 --- a/day12/common.py +++ b/day12/common.py @@ -1,8 +1,10 @@ +from __future__ import annotations from dataclasses import dataclass -from typing import NamedTuple, NewType +from typing import NamedTuple, NewType, Self import re import logging + def im_replace(line: str, mask: str) -> str: idx = 0 new_line = "" @@ -29,7 +31,7 @@ class SpringLine(NamedTuple): debug_str += rep_str + " / " tentative = im_replace(self.line, rep_str) possible = SpringLine(tentative, self.groups) - debug_str += str(possible)+": " + debug_str += str(possible) + ": " if possible.is_valid(): possibles.append(im_replace(self.line, rep_str)) debug_str += "valid" @@ -43,6 +45,14 @@ class SpringLine(NamedTuple): # logging.debug(pattern) return re.match(pattern, self.line) + def unfold(self) -> SpringLine: + new_line = [] + new_groups = [] + for i in range(5): + new_line.append(self.line) + new_groups += self.groups + return SpringLine("?".join(new_line), new_groups) + Spring = NewType("Spring", list[SpringLine]) diff --git a/day12/part2.py b/day12/part2.py index c98728b..8000604 100755 --- a/day12/part2.py +++ b/day12/part2.py @@ -3,5 +3,6 @@ from common import * -def part2(input_file:str) -> int: - return 0 \ No newline at end of file +def part2(input_file: str) -> int: + spring = parse(input_file) + return sum(list(map(len, map(lambda s: s.unfold().arrangments(), spring)))) diff --git a/day12/test.py b/day12/test.py index 43ffd6a..400b92b 100755 --- a/day12/test.py +++ b/day12/test.py @@ -4,9 +4,10 @@ import logging from unittest import TestCase, main from common import * from part1 import part1 +from part2 import part2 -class DayXTests(TestCase): +class Day12Tests(TestCase): def test_parsing(self): parsed_spring = parse("example_input.txt") spring: Spring = [ diff --git a/day13/common.py b/day13/common.py new file mode 100644 index 0000000..d373332 --- /dev/null +++ b/day13/common.py @@ -0,0 +1,35 @@ +from dataclasses import dataclass +from collections.abc import Iterator + + +@dataclass(init=False) +class MirrorField: + _data: list[str] + + def __getitem__(self, key: int) -> str: + return self._data[key] + + def __len__(self) -> int: + return len(self._data) + + def __iter__(self) -> Iterator[str]: + return iter(self._data) + + def iter_horizontal(self) -> Iterator[str]: + return iter(self) + + def iter_vertical(self) -> Iterator[str]: + x = 0 + while x < len(self._data[0]): + the_str = "" + for line in self._data: + the_str += line[x] + yield the_str + x += 1 + + def find_symetry(self) -> tuple[str, int] | None: + """Return a tuple indicating a symetry horizontal (h) or vertical (v) and the first row or column index""" + + +def parse(input_file: str) -> MirrorField: + return MirrorField() diff --git a/day13/example_input.txt b/day13/example_input.txt new file mode 100644 index 0000000..f226414 --- /dev/null +++ b/day13/example_input.txt @@ -0,0 +1,15 @@ +#.##..##. +..#.##.#. +##......# +##......# +..#.##.#. +..##..##. +#.#.##.#. + +#...##..# +#....#..# +..##..### +#####.##. +#####.##. +..##..### +#....#..# \ No newline at end of file diff --git a/day13/part1.py b/day13/part1.py new file mode 100755 index 0000000..fb31d16 --- /dev/null +++ b/day13/part1.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python3.11 + +from common import * + + +def part1(input_file: str) -> int: + return 0 diff --git a/day13/part2.py b/day13/part2.py new file mode 100755 index 0000000..6694468 --- /dev/null +++ b/day13/part2.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python3.11 + +from common import * + + +def part2(input_file: str) -> int: + return 0 diff --git a/day13/test.py b/day13/test.py new file mode 100755 index 0000000..e5ed919 --- /dev/null +++ b/day13/test.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3.11 + +from unittest import TestCase, main +import logging +from common import * +from part1 import part1 +from part2 import part2 + + +class DayXTests(TestCase): + def test_parsing(self): + pass + + def test_part1(self): + self.assertEqual(405, part1("example_input.txt")) + + def test_part2(self): + pass + + +if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG) + main(verbosity=2) diff --git a/get_today_puzzle.sh b/get_today_puzzle.sh index d0a2a20..8afd635 100755 --- a/get_today_puzzle.sh +++ b/get_today_puzzle.sh @@ -1,5 +1,9 @@ #!/bin/bash + DAY=`date "+%-e"` -mkdir day${DAY} +if [ ! -d day${DAY0} ]; then + mkdir day${DAY} + cp template/* day${DAY} +fi # You have to get your cookie header in text format (Cookie: session=xxxx) wget --verbose --header "`cat cookies.txt`" "https://adventofcode.com/2023/day/${DAY}/input" -O day${DAY}/input.txt \ No newline at end of file