From da1c74cc6ac28819332f1d71da9949eea8e26b55 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Tue, 13 Dec 2022 10:11:31 +0100 Subject: Day 13 --- day13/__init__.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 day13/__init__.py (limited to 'day13/__init__.py') diff --git a/day13/__init__.py b/day13/__init__.py new file mode 100644 index 0000000..d9fd9f2 --- /dev/null +++ b/day13/__init__.py @@ -0,0 +1,87 @@ +# -*- coding: utf-8 -*- +import json +from abc import ABC +from functools import cmp_to_key, reduce +from typing import Iterator, Any, List, Tuple, Union + +from aoc import BaseAssignment + + +Packet = Union[List[int], int, List["Packet"]] + + +class Assignment(BaseAssignment, ABC): + def compare_pair(self, pair: Tuple[Packet, Packet]): + packet_a, packet_b = pair + + if isinstance(packet_a, int) and isinstance(packet_b, int): + diff = packet_b - packet_a + return diff / abs(diff) if diff != 0 else 0 + + if isinstance(packet_a, list) and isinstance(packet_b, int): + packet_b = [packet_b] + + if isinstance(packet_b, list) and isinstance(packet_a, int): + packet_a = [packet_a] + + try: + for sub_pair in zip(packet_a, packet_b, strict=True): + comparison = self.compare_pair(sub_pair) + + if comparison != 0: + return comparison + except ValueError: + return self.compare_pair((len(packet_a), len(packet_b))) + + return 0 + + +class AssignmentOne(Assignment): + example_result = 13 + + def pairs(self, input: Iterator[str]): + while True: + yield (json.loads(next(input)), json.loads(next(input))) + + try: + next(input) + except StopIteration: + break + + def run(self, input: Iterator) -> Any: + return sum( + index + for index, pair in enumerate(self.pairs(input), start=1) + if self.compare_pair(pair) > 0 + ) + + +class AssignmentTwo(Assignment): + example_result = 140 + + def packets(self, input: Iterator[str]): + for item in input: + if item == "": + continue + + yield json.loads(item) + + def run(self, input: Iterator) -> Any: + packets = sorted( + [[[2]], [[6]], *self.packets(input)], + key=cmp_to_key(lambda a, b: self.compare_pair((a, b))), + reverse=True, + ) + + return reduce( + lambda o, i: o * i, + [ + index + for index, packet in enumerate(packets, start=1) + if packet + in ( + [[2]], + [[6]], + ) + ], + ) -- cgit v1.2.3