From 1f4c687139e811011b723638fab9c0ebca7d6289 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 29 Dec 2024 14:43:50 -0500 Subject: [PATCH] 2018: d02: ex2: add solution --- 2018/d02/ex2/ex2.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100755 2018/d02/ex2/ex2.py diff --git a/2018/d02/ex2/ex2.py b/2018/d02/ex2/ex2.py new file mode 100755 index 0000000..fb60f27 --- /dev/null +++ b/2018/d02/ex2/ex2.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +import itertools +import sys + + +def solve(input: str) -> str: + def find_box_ids(boxes: list[str]) -> str: + for lhs, rhs in itertools.combinations(boxes, 2): + assert len(lhs) == len(rhs) # Sanity check + for i in range(len(lhs)): + if lhs[:i] == rhs[:i] and lhs[i + 1 :] == rhs[i + 1 :]: + return lhs[:i] + lhs[i + 1 :] + assert False # Sanity check + + boxes = input.splitlines() + return find_box_ids(boxes) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()