From e5f1d74b0885e8ebe822d915336b0284a450fda9 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 13 Dec 2022 10:03:37 +0100 Subject: [PATCH] 2022: d13: ex2: add solution --- 2022/d13/ex2/ex2.py | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 2022/d13/ex2/ex2.py diff --git a/2022/d13/ex2/ex2.py b/2022/d13/ex2/ex2.py new file mode 100755 index 0000000..5400d83 --- /dev/null +++ b/2022/d13/ex2/ex2.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +import functools +import sys +from typing import Union + +Packet = Union[int, list["Packet"]] + + +def cmp_packet(lhs: Packet, rhs: Packet) -> int: + if isinstance(lhs, int) and isinstance(rhs, int): + return lhs - rhs + if isinstance(lhs, int): + lhs = [lhs] + if isinstance(rhs, int): + rhs = [rhs] + non_equal_cmp = (res for res in map(cmp_packet, lhs, rhs) if res != 0) + len_cmp = cmp_packet(len(lhs), len(rhs)) + return next(non_equal_cmp, len_cmp) + + +def solve(input: list[str]) -> int: + def to_packet(input: str) -> Packet: + # Sanity checks + assert "\n" not in input + assert input + + return eval(input) # Secret best way to parse JSON + + packets = [to_packet(line) for line in input if line] + packets.extend( + ( + [[2]], + [[6]], + ) + ) + packets.sort(key=functools.cmp_to_key(cmp_packet)) + + # Packets are 1-indexed + first = packets.index([[2]]) + 1 + second = packets.index([[6]]) + 1 + + return first * second + + +def main() -> None: + input = sys.stdin.read().splitlines() + print(solve(input)) + + +if __name__ == "__main__": + main()