From 91ba8ec86fc05d776cd9bb55a08bde37a0bfc20b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sat, 14 Dec 2024 21:02:23 +0100 Subject: [PATCH] 2015 day 25. --- src/holt59/aoc/2015/day25.py | 16 ++++++++++++++++ src/holt59/aoc/inputs/holt59/2015/day25.txt | 1 + src/holt59/aoc/inputs/tests/2015/day25.txt | 1 + src/holt59/aoc/tools/__init__.py | 0 src/holt59/aoc/tools/math.py | 21 +++++++++++++++++++++ 5 files changed, 39 insertions(+) create mode 100644 src/holt59/aoc/2015/day25.py create mode 100644 src/holt59/aoc/inputs/holt59/2015/day25.txt create mode 100644 src/holt59/aoc/inputs/tests/2015/day25.txt create mode 100644 src/holt59/aoc/tools/__init__.py create mode 100644 src/holt59/aoc/tools/math.py diff --git a/src/holt59/aoc/2015/day25.py b/src/holt59/aoc/2015/day25.py new file mode 100644 index 0000000..0389521 --- /dev/null +++ b/src/holt59/aoc/2015/day25.py @@ -0,0 +1,16 @@ +import re +from typing import Any, Iterator + +from ..base import BaseSolver +from ..tools.math import pow_mod + + +class Solver(BaseSolver): + def solve(self, input: str) -> Iterator[Any]: + m = re.search(r"row\s*([0-9]+)\s*,\s*column\s*([0-9]+)", input) + assert m is not None + + row, col = int(m.group(1)), int(m.group(2)) + n = (row * (row - 1)) // 2 + col * (col + 1) // 2 + (row - 1) * (col - 1) + + yield (20151125 * pow_mod(252533, n - 1, 33554393)) % 33554393 diff --git a/src/holt59/aoc/inputs/holt59/2015/day25.txt b/src/holt59/aoc/inputs/holt59/2015/day25.txt new file mode 100644 index 0000000..9dcb923 --- /dev/null +++ b/src/holt59/aoc/inputs/holt59/2015/day25.txt @@ -0,0 +1 @@ +To continue, please consult the code grid in the manual. Enter the code at row 3010, column 3019. diff --git a/src/holt59/aoc/inputs/tests/2015/day25.txt b/src/holt59/aoc/inputs/tests/2015/day25.txt new file mode 100644 index 0000000..31e0d72 --- /dev/null +++ b/src/holt59/aoc/inputs/tests/2015/day25.txt @@ -0,0 +1 @@ +To continue, please consult the code grid in the manual. Enter the code at row 6, column 5. diff --git a/src/holt59/aoc/tools/__init__.py b/src/holt59/aoc/tools/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/holt59/aoc/tools/math.py b/src/holt59/aoc/tools/math.py new file mode 100644 index 0000000..c02a802 --- /dev/null +++ b/src/holt59/aoc/tools/math.py @@ -0,0 +1,21 @@ +def pow_mod(b: int, e: int, m: int): + """ + Compute (b ** e) % m using right-to-left binary method. + + See https://en.wikipedia.org/wiki/Modular_exponentiation. + + Args: + b: Base to exponentiate. + e: Exponent. + m: Modulus. + + Returns: + (b ** e) % m. + """ + r = 1 + while e > 0: + if e % 2 == 1: + r = (r * b) % m + e >>= 1 + b = (b * b) % m + return r