-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
1 | ||
2 | ||
3 | ||
2024 |