aoc2024/days/day22.st
2024-12-22 10:12:46 +00:00

61 lines
2.2 KiB
Smalltalk

Object subclass: Monkey [
| history |
history: h [history := h]
lastValue [^history at: history size]
collectPricesInto: intoTbl seen: seenTbl
[ | ch prices deltas key newV prevSeen localBest |
prices := history collect: [:it | it \\ 10].
deltas := 2 to: history size collect: [
:i | (prices at: i) - (prices at: i - 1) + 9].
ch := 0. "past four deltas as a base-19 integer"
1 to: 4 do: [:i | ch := 19 * ch + (deltas at: i)].
"Accumulate the best value globally, (local variable as a
'compiler' optimisation)."
localBest := Monkey bestBananas.
5 to: deltas size do: [
:i | ch := (19 * ch + (deltas at: i)) \\ 130321.
"ch is theoretically in the range [0,19^4),
so add 1 to keep it in array range"
key := ch + 1.
(seenTbl at: key) == self ifFalse: [
seenTbl at: key put: self.
newV := (prices at: i + 1) + (intoTbl at: key).
intoTbl at: key put: newV.
newV > localBest ifTrue: [localBest := newV].
]].
Monkey bestBananas: localBest. ]
]
Monkey class extend [
| bestV |
bestBananas: v [bestV := v]
bestBananas [^bestV]
initialize [bestV := 0]
keyDigits [^19]
keyMax [^130321 "19 raisedTo: 4"]
stepSecret: initial times: n
[ | s hist | s := initial.
hist := 1 to: n collect: [
:i |
s := (s bitXor: (s bitShift: 6)) bitAnd: 16777215.
s := (s bitXor: (s bitShift: -5)) bitAnd: 16777215.
s := (s bitXor: (s bitShift: 11)) bitAnd: 16777215.
s ].
^Monkey new history: {initial},hist ]
]
Monkey initialize.
AOC input: [ stdin toLines collect: [
:it | Monkey stepSecret: it asNumber times: 2000] ];
part1: [ :monkeys | (monkeys collect: [:it | it lastValue]) sum ];
part2: [ :monkeys | | buyMap seenMap |
buyMap := Array new: Monkey keyMax withAll: 0.
seenMap := Array new: Monkey keyMax withAll: nil.
monkeys do: [:it | it collectPricesInto: buyMap seen: seenMap].
Monkey bestBananas ];
result: [ :monkeys :part | part value: monkeys ];
finish.