From 096c3d3e5dd03a0021ec19576256fa8f24dcac2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Thu, 2 Jan 2025 19:21:30 +0100 Subject: [PATCH] Solve 2024:22 p1-2 "Monkey Market" Part 1 was relatively easy, 2 was harder. > So, by asking the monkey to sell the *first time* > each buyer's prices go down 2, then up 1, then > down 1, then up 3, you would get 23 (7 + 7 + 9) > bananas! Emphazis mine. Earlier versions of the code missed this fact, and added all occourences of the sequence. providing a answer that is too high. --- 2024-python/output/day_22.py | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 2024-python/output/day_22.py diff --git a/2024-python/output/day_22.py b/2024-python/output/day_22.py new file mode 100644 index 0000000..22a22e6 --- /dev/null +++ b/2024-python/output/day_22.py @@ -0,0 +1,44 @@ +import math +from collections import deque, defaultdict + +from output import ints + + +def solve(data): + s = [(n, n % 10) for n in ints(data)] + bananas = defaultdict(int) + sum_2000th_secrets = 0 + for j, n in enumerate(s): + seen = set() + L = deque() + for i in range(2000): + prize, dff = calc(*n) + if len(L) == 4: + L.popleft() + L.append(dff) + if len(L) == 4: + if tuple(L) not in seen: + seen.add(tuple(L)) + bananas[tuple(L)] += prize % 10 + n = prize, dff + sum_2000th_secrets += prize + max_bananas = max(bananas.values()) + return sum_2000th_secrets, max_bananas + + +def calc(i, d=0): + a = i % 10 + i = ((i * 64) ^ i) % 16777216 + i = (math.floor(i / 32) ^ i) % 16777216 + i = ((i * 2048) ^ i) % 16777216 + return i, (i % 10) - a + + +if __name__ == "__main__": + with open("./input/22.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp) + + print(p1) + print(p2)