diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-12-22 19:52:17 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-12-22 19:53:29 +0100 |
commit | a82498c4141728069d80418e26196193c451a1cd (patch) | |
tree | 4ca62512eba8f4879681a63a1ea390f5b535c255 /2024/22 | |
parent | 069db00a05cdc880a44891e0cbb16543bfd57ebd (diff) |
Add 2024 day 22 solutions
Diffstat (limited to '2024/22')
-rw-r--r-- | 2024/22/.gitignore | 1 | ||||
-rw-r--r-- | 2024/22/Makefile | 1 | ||||
-rw-r--r-- | 2024/22/puzzles.py | 60 |
3 files changed, 62 insertions, 0 deletions
diff --git a/2024/22/.gitignore b/2024/22/.gitignore new file mode 100644 index 0000000..ffc46fe --- /dev/null +++ b/2024/22/.gitignore @@ -0,0 +1 @@ +puzzle-[12].py diff --git a/2024/22/Makefile b/2024/22/Makefile new file mode 100644 index 0000000..0a4e980 --- /dev/null +++ b/2024/22/Makefile @@ -0,0 +1 @@ +include ../../Makefiles/py.mk diff --git a/2024/22/puzzles.py b/2024/22/puzzles.py new file mode 100644 index 0000000..dff825f --- /dev/null +++ b/2024/22/puzzles.py @@ -0,0 +1,60 @@ +#!/usr/bin/python3 + +import collections +from typing import Deque, Iterable + + +type Diff = tuple[int, int, int, int] + + +def main() -> None: + with open("input", "r") as f: + xs = map(int, f.readlines()) + + # START PART 1 + print(sum(map(lastfrom, map(secrets, xs)))) + # END PART 1 START PART 2 + sums = collections.defaultdict(int) + for x in xs: + seen: set[Diff] = set() + dq: Deque[int] = collections.deque(maxlen=4) + + gen = map(lastdigit, secrets(x)) + last = next(gen) + + for _ in range(3): + n = next(gen) + dq.append(n - last) + last = n + + for n in gen: + dq.append(n - last) + if (t := tuple(dq)) not in seen: + seen.add(t) + sums[t] += n + last = n + dq.popleft() + + print(max(sums.values())) + # END PART 2 + + +def secrets(n: int) -> int: + for _ in range(2000): + n = (n ^ n<<6) & 0xFFFFFF + n = (n ^ n>>5) & 0xFFFFFF + n = (n ^ n<<11) & 0xFFFFFF + yield n + + +def lastdigit(n: int) -> int: + return n % 10 + + +def lastfrom[T](xs: Iterable[T]) -> T: + *_, x = xs + return x + + +if __name__ == "__main__": + main() |