Stayed up late for this one

This commit is contained in:
Daniel Weber 2024-12-22 00:45:19 -05:00
parent 4164b85ac4
commit 11b3fde3f5

51
2024/day22/main.py Normal file
View File

@ -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()