From 0b5bf6596c9de61757dd6668c08ddf9b88aaa263 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 29 Dec 2024 20:55:14 -0500 Subject: [PATCH] 2018: d11: ex1: add solution --- 2018/d11/ex1/ex1.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 2018/d11/ex1/ex1.py diff --git a/2018/d11/ex1/ex1.py b/2018/d11/ex1/ex1.py new file mode 100755 index 0000000..dedbfc5 --- /dev/null +++ b/2018/d11/ex1/ex1.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +import functools +import itertools +import sys +from typing import NamedTuple + + +class Point(NamedTuple): + x: int + y: int + + +def solve(input: str) -> str: + def cell_power(cell: Point, serial: int) -> int: + rack_id = cell.x + 10 + power = rack_id * cell.y + power += serial + power *= rack_id + return ((power // 100) % 10) - 5 + + def total_power(top_left: Point, serial: int) -> int: + return sum( + cell_power(Point(top_left.x + dx, top_left.y + dy), serial) + for dx, dy in itertools.product(range(3), repeat=2) + ) + + serial = int(input) + cell = max( + map(Point._make, itertools.product(range(1, 300 - 3 + 1), repeat=2)), + key=functools.partial(total_power, serial=serial), + ) + return f"{cell.x},{cell.y}" + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()