aboutsummaryrefslogtreecommitdiff
path: root/2024
diff options
context:
space:
mode:
Diffstat (limited to '2024')
-rw-r--r--2024/22/.gitignore1
-rw-r--r--2024/22/Makefile1
-rw-r--r--2024/22/puzzles.py60
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()