From 11b3fde3f501f4a0a16b51072a34a6e458eb6e67 Mon Sep 17 00:00:00 2001 From: Daniel Weber Date: Sun, 22 Dec 2024 00:45:19 -0500 Subject: [PATCH] Stayed up late for this one --- 2024/day22/main.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 2024/day22/main.py diff --git a/2024/day22/main.py b/2024/day22/main.py new file mode 100644 index 0000000..20a6546 --- /dev/null +++ b/2024/day22/main.py @@ -0,0 +1,51 @@ +import sys +import os +sys.path.insert(1, os.path.abspath('../../')) +from python_tools.aoc_utils import * + +def calc_secret(number): + n = number + prune = lambda x: x%16777216 + n = prune((n << 6) ^ n) + n = prune((n >> 5) ^ n) + n = prune((n << 11) ^ n) + return n + +def part_one(input): + p1=0 + for i in input: + n = i + for j in range(2000): + n = calc_secret(n) + p1+=n + print("P1 ", p1) + +def part_two(input): + p2 = 0 + LARGEST = {} + for i in input: + VISITED = set() + n = i + diffs = [] + for j in range(2000): + new_n = calc_secret(n) + diffs.append((n % 10) - (new_n % 10)) + if len(diffs) > 4: + temp_str = str(diffs[-4:]) + if temp_str not in VISITED: + VISITED.add(temp_str) + if temp_str not in LARGEST: + LARGEST[temp_str] = new_n % 10 + else: + LARGEST[temp_str] += new_n % 10 + n = new_n + print("P2: ", max(LARGEST.values())) + +def main(): + contents = list(map(int, file2list("input.txt"))) + part_one(contents) + part_two(contents) + +if __name__ == "__main__": + main() +