Day 19 -- No LL

This commit is contained in:
Beatrice Szilvasy 2024-12-22 10:12:46 +00:00
parent 1e7399f109
commit 496d70786a

View file

@ -18,21 +18,13 @@ Object subclass: Monkey [
"ch is theoretically in the range [0,19^4), "ch is theoretically in the range [0,19^4),
so add 1 to keep it in array range" so add 1 to keep it in array range"
key := ch + 1. key := ch + 1.
(seenTbl at: key) == false ifTrue: [ (seenTbl at: key) == self ifFalse: [
"Store a linked list of seen values within the table, seenTbl at: key put: self.
to make resetting it easier."
seenTbl at: key put: prevSeen.
prevSeen := key.
newV := (prices at: i + 1) + (intoTbl at: key). newV := (prices at: i + 1) + (intoTbl at: key).
intoTbl at: key put: newV. intoTbl at: key put: newV.
newV > localBest ifTrue: [localBest := newV]. newV > localBest ifTrue: [localBest := newV].
]]. ]].
Monkey bestBananas: localBest. Monkey bestBananas: localBest. ]
"Reset the seen table via the linked list."
[prevSeen notNil] whileTrue: [
| tmp | tmp := seenTbl at: prevSeen.
seenTbl at: prevSeen put: false.
prevSeen := tmp. ]]
] ]
Monkey class extend [ Monkey class extend [
@ -62,7 +54,7 @@ AOC input: [ stdin toLines collect: [
part1: [ :monkeys | (monkeys collect: [:it | it lastValue]) sum ]; part1: [ :monkeys | (monkeys collect: [:it | it lastValue]) sum ];
part2: [ :monkeys | | buyMap seenMap | part2: [ :monkeys | | buyMap seenMap |
buyMap := Array new: Monkey keyMax withAll: 0. buyMap := Array new: Monkey keyMax withAll: 0.
seenMap := Array new: Monkey keyMax withAll: false. seenMap := Array new: Monkey keyMax withAll: nil.
monkeys do: [:it | it collectPricesInto: buyMap seen: seenMap]. monkeys do: [:it | it collectPricesInto: buyMap seen: seenMap].
Monkey bestBananas ]; Monkey bestBananas ];
result: [ :monkeys :part | part value: monkeys ]; result: [ :monkeys :part | part value: monkeys ];