From 36429349a81a3c32f469121839ef9af115d7fec5 Mon Sep 17 00:00:00 2001 From: Lachlan Meyer Date: Sun, 22 Dec 2024 20:38:09 +1100 Subject: [PATCH] 22-2 --- 2024/22/22-2.py | 62 ++++++++++++++++++++++++++++++++++++++++++ 2024/22/testinput2.txt | 4 +++ 2 files changed, 66 insertions(+) create mode 100644 2024/22/22-2.py create mode 100644 2024/22/testinput2.txt diff --git a/2024/22/22-2.py b/2024/22/22-2.py new file mode 100644 index 0000000..c791f4b --- /dev/null +++ b/2024/22/22-2.py @@ -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) + diff --git a/2024/22/testinput2.txt b/2024/22/testinput2.txt new file mode 100644 index 0000000..cfb1988 --- /dev/null +++ b/2024/22/testinput2.txt @@ -0,0 +1,4 @@ +1 +2 +3 +2024 \ No newline at end of file