Day 22 optimisations
This commit is contained in:
parent
beb0f4a8e7
commit
1e7399f109
1 changed files with 42 additions and 15 deletions
|
|
@ -3,24 +3,48 @@ Object subclass: Monkey [
|
||||||
history: h [history := h]
|
history: h [history := h]
|
||||||
lastValue [^history at: history size]
|
lastValue [^history at: history size]
|
||||||
|
|
||||||
collectPricesInto: intoTbl
|
collectPricesInto: intoTbl seen: seenTbl
|
||||||
[ | ch table prices deltas chMod |
|
[ | ch prices deltas key newV prevSeen localBest |
|
||||||
table := Dictionary new.
|
|
||||||
prices := history collect: [:it | it \\ 10].
|
prices := history collect: [:it | it \\ 10].
|
||||||
deltas := 2 to: history size collect: [
|
deltas := 2 to: history size collect: [
|
||||||
:i | (prices at: i) - (prices at: i - 1) + 9].
|
:i | (prices at: i) - (prices at: i - 1) + 9].
|
||||||
ch := 0.
|
ch := 0. "past four deltas as a base-19 integer"
|
||||||
1 to: 4 do: [
|
1 to: 4 do: [:i | ch := 19 * ch + (deltas at: i)].
|
||||||
:i | ch := 20 * ch + (deltas at: i)].
|
"Accumulate the best value globally, (local variable as a
|
||||||
chMod := 20 raisedTo: 4.
|
'compiler' optimisation)."
|
||||||
|
localBest := Monkey bestBananas.
|
||||||
5 to: deltas size do: [
|
5 to: deltas size do: [
|
||||||
:i | ch := (20 * ch + (deltas at: i)) \\ chMod.
|
:i | ch := (19 * ch + (deltas at: i)) \\ 130321.
|
||||||
table at: ch ifAbsentPut: [prices at: i + 1]].
|
"ch is theoretically in the range [0,19^4),
|
||||||
table keysAndValuesDo: [
|
so add 1 to keep it in array range"
|
||||||
:ch :v | intoTbl at: ch put: v + (intoTbl at: ch ifAbsent: [0])]]
|
key := ch + 1.
|
||||||
|
(seenTbl at: key) == false ifTrue: [
|
||||||
|
"Store a linked list of seen values within the table,
|
||||||
|
to make resetting it easier."
|
||||||
|
seenTbl at: key put: prevSeen.
|
||||||
|
prevSeen := key.
|
||||||
|
newV := (prices at: i + 1) + (intoTbl at: key).
|
||||||
|
intoTbl at: key put: newV.
|
||||||
|
newV > localBest ifTrue: [localBest := newV].
|
||||||
|
]].
|
||||||
|
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 [
|
||||||
|
| bestV |
|
||||||
|
bestBananas: v [bestV := v]
|
||||||
|
bestBananas [^bestV]
|
||||||
|
|
||||||
|
initialize [bestV := 0]
|
||||||
|
|
||||||
|
keyDigits [^19]
|
||||||
|
keyMax [^130321 "19 raisedTo: 4"]
|
||||||
|
|
||||||
stepSecret: initial times: n
|
stepSecret: initial times: n
|
||||||
[ | s hist | s := initial.
|
[ | s hist | s := initial.
|
||||||
hist := 1 to: n collect: [
|
hist := 1 to: n collect: [
|
||||||
|
|
@ -31,12 +55,15 @@ Monkey class extend [
|
||||||
s ].
|
s ].
|
||||||
^Monkey new history: {initial},hist ]
|
^Monkey new history: {initial},hist ]
|
||||||
]
|
]
|
||||||
|
Monkey initialize.
|
||||||
|
|
||||||
AOC input: [ stdin toLines collect: [
|
AOC input: [ stdin toLines collect: [
|
||||||
:it | Monkey stepSecret: it asNumber times: 2000] ];
|
:it | Monkey stepSecret: it asNumber times: 2000] ];
|
||||||
part1: [ :monkeys | (monkeys collect: [:it | it lastValue]) sum ];
|
part1: [ :monkeys | (monkeys collect: [:it | it lastValue]) sum ];
|
||||||
part2: [ :monkeys | | buyMap | buyMap := Dictionary new.
|
part2: [ :monkeys | | buyMap seenMap |
|
||||||
monkeys do: [:it | it collectPricesInto: buyMap].
|
buyMap := Array new: Monkey keyMax withAll: 0.
|
||||||
buyMap values maximum ];
|
seenMap := Array new: Monkey keyMax withAll: false.
|
||||||
|
monkeys do: [:it | it collectPricesInto: buyMap seen: seenMap].
|
||||||
|
Monkey bestBananas ];
|
||||||
result: [ :monkeys :part | part value: monkeys ];
|
result: [ :monkeys :part | part value: monkeys ];
|
||||||
finish.
|
finish.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue