From 9e21d8d9c8ad2dd7bb03a36da2fbeed54e414d0b Mon Sep 17 00:00:00 2001 From: Fedaya Date: Sun, 1 Dec 2024 16:19:37 +0100 Subject: [PATCH] retyped day2, completed days 3 and 4, modified template --- day2/common.py | 5 +---- day3/common.py | 1 + day3/part1.py | 22 ++++++++++++++++++++++ day3/part2.py | 38 ++++++++++++++++++++++++++++++++++++++ day3/toy_input | 6 ++++++ day4/common.py | 29 +++++++++++++++++++++++++++++ day4/part1.py | 13 +++++++++++++ day4/part2.py | 13 +++++++++++++ day4/toy_input | 6 ++++++ template/common.py | 8 +++----- template/part1.py | 5 ++--- template/part2.py | 14 -------------- 12 files changed, 134 insertions(+), 26 deletions(-) create mode 100644 day3/common.py create mode 100644 day3/part1.py create mode 100644 day3/part2.py create mode 100644 day3/toy_input create mode 100644 day4/common.py create mode 100644 day4/part1.py create mode 100644 day4/part2.py create mode 100644 day4/toy_input delete mode 100644 template/part2.py diff --git a/day2/common.py b/day2/common.py index fc718c5..35592df 100644 --- a/day2/common.py +++ b/day2/common.py @@ -1,6 +1,3 @@ -from typing import Any - - -def parse(filename: str) -> Any | None: +def parse(filename: str) -> list[tuple[str, str]]: with open(filename) as f: return [tuple(a[:-1].split()) for a in f.readlines()] diff --git a/day3/common.py b/day3/common.py new file mode 100644 index 0000000..e41068b --- /dev/null +++ b/day3/common.py @@ -0,0 +1 @@ +# Nothing in common diff --git a/day3/part1.py b/day3/part1.py new file mode 100644 index 0000000..1e9bf97 --- /dev/null +++ b/day3/part1.py @@ -0,0 +1,22 @@ +from string import ascii_lowercase + + +def parse(filename: str) -> list[tuple[str, str]]: + with open(filename) as f: + return list(map(lambda x: (x[0 : int((len(x) - 1) / 2)], x[int((len(x) - 1) / 2) : -1]), f.readlines())) + + +def convert(char: str) -> int: + return ord(char) - 96 if char in ascii_lowercase else ord(char) - 38 + + +def solve(bagpacks: list[tuple[str, str]]) -> int: + return sum(map(convert, [(filter(lambda x: x in bagpack[1], bagpack[0])).__next__() for bagpack in bagpacks])) + + +def main(): + print(solve(parse("input"))) + + +if __name__ == "__main__": + main() diff --git a/day3/part2.py b/day3/part2.py new file mode 100644 index 0000000..a812501 --- /dev/null +++ b/day3/part2.py @@ -0,0 +1,38 @@ +from string import ascii_lowercase + + +def parse(filename: str) -> list[tuple[str, str, str]]: + groups = [] + i = 0 + group = [] + with open(filename) as f: + for line in f.readlines(): + if i % 3 == 0 and i != 0: + groups.append(group) + group = [] + group.append(line[:-1]) + i += 1 + groups.append(group) + return tuple(groups) + + +def convert(char: str) -> int: + return ord(char) - 96 if char in ascii_lowercase else ord(char) - 38 + + +def common_character(group: tuple[str, str, str]) -> str: + assert len(group) == 3 + b1, b2, b3 = group + return filter(lambda c: c in b2 and c in b3, b1).__next__() + + +def solve(groups: list[tuple[str, str, str]]) -> int: + return sum(map(convert, map(common_character, groups))) + + +def main(): + print(solve(parse("input"))) + + +if __name__ == "__main__": + main() diff --git a/day3/toy_input b/day3/toy_input new file mode 100644 index 0000000..9919ffa --- /dev/null +++ b/day3/toy_input @@ -0,0 +1,6 @@ +vJrwpWtwJgWrhcsFMMfFFhFp +jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL +PmmdzqPrVvPwwTWBwg +wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn +ttgJtRGJQctTZtZT +CrZsJsPPZsGzwwsLwLmpwMDw \ No newline at end of file diff --git a/day4/common.py b/day4/common.py new file mode 100644 index 0000000..4dec53c --- /dev/null +++ b/day4/common.py @@ -0,0 +1,29 @@ +from __future__ import annotations + +import re +from typing import NamedTuple + + +class Range(NamedTuple): + first: int + last: int + + def __contains__(self, other: Range) -> bool: + return self.first <= other.first and self.last >= other.last + + def __str__(self): + return f"{self.first}-{self.last}" + + def overlaps_with(self, other: Range) -> bool: + return Range(other.first, other.first) in self or Range(other.last, other.last) in self + + +def parse(filename: str) -> list[tuple[Range, Range]]: + pairs = [] + with open(filename) as f: + while line := f.readline(): + if match := re.match(r"(\d+)-(\d+),(\d+)-(\d+)", line): + pairs.append( + (Range(int(match.group(1)), int(match.group(2))), Range(int(match.group(3)), int(match.group(4)))) + ) + return pairs diff --git a/day4/part1.py b/day4/part1.py new file mode 100644 index 0000000..6da428d --- /dev/null +++ b/day4/part1.py @@ -0,0 +1,13 @@ +from common import Range, parse + + +def solve(input: list[tuple[Range, Range]]) -> int: + return len(list(filter(lambda x: x[0] in x[1] or x[1] in x[0], input))) + + +def main(): + print(solve(parse("input"))) + + +if __name__ == "__main__": + main() diff --git a/day4/part2.py b/day4/part2.py new file mode 100644 index 0000000..e3573b8 --- /dev/null +++ b/day4/part2.py @@ -0,0 +1,13 @@ +from common import Range, parse + + +def solve(input: list[tuple[Range, Range]]) -> int: + return len(list(filter(lambda x: x[0].overlaps_with(x[1]) or x[1].overlaps_with(x[0]), input))) + + +def main(): + print(solve(parse("input"))) + + +if __name__ == "__main__": + main() diff --git a/day4/toy_input b/day4/toy_input new file mode 100644 index 0000000..99a66c5 --- /dev/null +++ b/day4/toy_input @@ -0,0 +1,6 @@ +2-4,6-8 +2-3,4-5 +5-7,7-9 +2-8,3-7 +6-6,4-6 +2-6,4-8 \ No newline at end of file diff --git a/template/common.py b/template/common.py index 8814171..18487f9 100644 --- a/template/common.py +++ b/template/common.py @@ -1,5 +1,3 @@ -from typing import Any - - -def parse(filename: str) -> Any | None: - pass +def parse(filename: str): + with open(filename) as f: + return [line[:-1] for line in f.readlines()] diff --git a/template/part1.py b/template/part1.py index 8d3baac..305aecf 100644 --- a/template/part1.py +++ b/template/part1.py @@ -1,13 +1,12 @@ from common import parse -def solve(): +def solve(input): pass def main(): - r = solve() - print(r) + print(solve(parse("input"))) if __name__ == "__main__": diff --git a/template/part2.py b/template/part2.py deleted file mode 100644 index 8d3baac..0000000 --- a/template/part2.py +++ /dev/null @@ -1,14 +0,0 @@ -from common import parse - - -def solve(): - pass - - -def main(): - r = solve() - print(r) - - -if __name__ == "__main__": - main()