Skip to content

Commit

Permalink
22-2
Browse files Browse the repository at this point in the history
  • Loading branch information
koosvary committed Dec 22, 2024
1 parent 76656e7 commit 3642934
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
62 changes: 62 additions & 0 deletions 2024/22/22-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import math
import ast

def mixThenPrune(secretNumber, multiplying, mutator):
if multiplying:
newSecretNumber = secretNumber * mutator
else:
newSecretNumber = math.floor(secretNumber / mutator)
# mix
newSecretNumber = secretNumber ^ newSecretNumber
# prune
newSecretNumber %= 16777216
return newSecretNumber

patternSales = {}
with open('2024/22/input.txt') as f:

for line in f:
line = line.strip()

secretNumber = int(line)
sequence = [secretNumber % 10]
diffs = []
testedDiffs = []

for _ in range(2000):
secretNumber = mixThenPrune(secretNumber, True, 64)
secretNumber = mixThenPrune(secretNumber, False, 32)
secretNumber = mixThenPrune(secretNumber, True, 2048)

newPrice = secretNumber % 10
sequence.append(newPrice)
lastPrice = sequence[-2]
diffs.append(newPrice - lastPrice)

if len(diffs) >= 4:
diffPattern = diffs[-4:]
key = repr(diffPattern)

# only add to the total if first instance of each sale pattern with this vendor
if key in testedDiffs:
continue

testedDiffs.append(key)

if key not in patternSales:
patternSales[key] = newPrice
else:
patternSales[key] += newPrice

bestPatternSales = 0
bestPattern = ''
for key in patternSales.keys():
salesMade = patternSales[key]

if salesMade > bestPatternSales:
bestPatternSales = salesMade
bestPattern = key

print(bestPattern)
print(bestPatternSales)

4 changes: 4 additions & 0 deletions 2024/22/testinput2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
2
3
2024

0 comments on commit 3642934

Please sign in to comment.