AdventOfCode/2024/22/solution.py

84 lines
1.9 KiB
Python

#!/bin/python3
import sys,time,re
from pprint import pprint
from math import trunc
sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,loadFile
start_time = time.time()
from functools import cache
input_f = 'test'
@cache
def genSecretP1(number:int):
mix = number * 64
step1 = (number ^ mix)%16777216
mix = int(trunc(step1/32))
step2 = (step1 ^ mix)%16777216
mix = step2 * 2048
step3 = (step2 ^ mix)%16777216
return step3
#########################################
# #
# Part 1 #
# #
#########################################
def part1():
numbers = list2int(loadFile(input_f))
r = 10
result = 0
for n in numbers:
number = n
for i in range(r):
number = genSecretP1(number)
result += number
return result
start_time = time.time()
print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')
#########################################
# #
# Part 2 #
# #
#########################################
@cache
def genSecretP2(number:int):
mix = number * 64
step1 = (number ^ mix)%16777216
mix = int(trunc(step1/32))
step2 = (step1 ^ mix)%16777216
mix = step2 * 2048
step3 = (step2 ^ mix)%16777216
print(str(number)+": "+str(number)[-1])
return step3
def part2():
numbers = list2int(loadFile(input_f))
r = 10
result = 0
for n in numbers:
number = n
for i in range(r):
number = genSecretP2(number)
result += number
return result
start_time = time.time()
print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms')