Day 9/10 cleanup
This commit is contained in:
parent
c88b867b83
commit
51cf7c1b88
3 changed files with 72 additions and 28 deletions
|
|
@ -66,10 +66,44 @@ Link subclass: Block [
|
||||||
printOn: st [st << '#[' << len << ']@' << pos]
|
printOn: st [st << '#[' << len << ']@' << pos]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
LinkedList subclass: FreeList [
|
||||||
|
| searchCache |
|
||||||
|
|
||||||
|
initialize [ searchCache := nil ]
|
||||||
|
|
||||||
|
searchCache
|
||||||
|
[ searchCache ifNil: [
|
||||||
|
searchCache := Array new: 9 withAll: self first].
|
||||||
|
^searchCache ]
|
||||||
|
|
||||||
|
cachedLinkForSize: sz [ ^self searchCache at: sz ]
|
||||||
|
cachedLinkForSize: sz put: block [ searchCache at: sz put: block ]
|
||||||
|
|
||||||
|
remove: block
|
||||||
|
[ searchCache keysAndValuesDo: [
|
||||||
|
:i :elt | elt == block ifTrue: [
|
||||||
|
searchCache at: i put: elt nextLink ]].
|
||||||
|
super remove: block ]
|
||||||
|
|
||||||
|
freeForBlock: block
|
||||||
|
[ | link len |
|
||||||
|
len := block len.
|
||||||
|
link := self cachedLinkForSize: len.
|
||||||
|
[ link isNil ] whileFalse: [
|
||||||
|
link pos > block pos ifTrue: [
|
||||||
|
self cachedLinkForSize: len put: link. ^nil ].
|
||||||
|
link len >= len ifTrue: [
|
||||||
|
self cachedLinkForSize: len put: link. ^link ].
|
||||||
|
link nextLink
|
||||||
|
ifNil: [ self cachedLinkForSize: len put: link. ^nil ]
|
||||||
|
ifNotNil: [:it | link := it]].
|
||||||
|
^nil ]
|
||||||
|
]
|
||||||
|
|
||||||
DriveBase subclass: DriveP2 [
|
DriveBase subclass: DriveP2 [
|
||||||
| freeList idMap lastId |
|
| freeList idMap lastId |
|
||||||
map: m
|
map: m
|
||||||
[ freeList := LinkedList new.
|
[ freeList := FreeList new.
|
||||||
idMap := Dictionary new.
|
idMap := Dictionary new.
|
||||||
super map: m ]
|
super map: m ]
|
||||||
|
|
||||||
|
|
@ -101,13 +135,8 @@ DriveBase subclass: DriveP2 [
|
||||||
|
|
||||||
compactBlock: id
|
compactBlock: id
|
||||||
[ | block | block := idMap at: id.
|
[ | block | block := idMap at: id.
|
||||||
freeList do: [
|
(freeList freeForBlock: block) ifNotNil: [
|
||||||
:free |
|
:free | self moveBlock: block to: free ]]
|
||||||
free pos > block pos ifTrue: [^self].
|
|
||||||
free len >= block len ifTrue: [
|
|
||||||
self moveBlock: block to: free.
|
|
||||||
^self
|
|
||||||
]]]
|
|
||||||
]
|
]
|
||||||
|
|
||||||
AOC input: [ stdin nextLine ];
|
AOC input: [ stdin nextLine ];
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,15 @@
|
||||||
|
Grid subclass: HeightMap [
|
||||||
|
| invHeights |
|
||||||
|
rows: r [super rows: r. self initInvHeights]
|
||||||
|
|
||||||
|
initInvHeights
|
||||||
|
[invHeights := (0 to: 9) collect: [:h | (super allPosnsOf: h) asArray]]
|
||||||
|
|
||||||
|
allPosnsOf: h [^invHeights at: h + 1]
|
||||||
|
]
|
||||||
|
|
||||||
Object subclass: TrailMap [
|
Object subclass: TrailMap [
|
||||||
| heightMap scores allDirs |
|
| heightMap scores allDirs heightsRev |
|
||||||
scores [^scores]
|
scores [^scores]
|
||||||
|
|
||||||
initFor: pos [ ^Set new add: pos; yourself ]
|
initFor: pos [ ^Set new add: pos; yourself ]
|
||||||
|
|
@ -10,32 +20,29 @@ Object subclass: TrailMap [
|
||||||
initialize [ allDirs := {Posn up. Posn down. Posn left. Posn right}. ]
|
initialize [ allDirs := {Posn up. Posn down. Posn left. Posn right}. ]
|
||||||
heights: hm [ heightMap := hm. self initScores. ]
|
heights: hm [ heightMap := hm. self initScores. ]
|
||||||
initScores
|
initScores
|
||||||
[ | rows |
|
[ scores :=
|
||||||
rows := (1 to: heightMap height) collect: [
|
Grid width: heightMap width height: heightMap height
|
||||||
:y | (1 to: heightMap width) collect: [
|
initWith: [
|
||||||
:x | | pos | pos := Posn x: x y: y.
|
:pos | ((heightMap at: pos) = 9)
|
||||||
((heightMap at: pos) = 9)
|
ifTrue: [self initFor: pos]
|
||||||
ifTrue: [self initFor: pos]
|
ifFalse: [nil]] ]
|
||||||
ifFalse: [nil]]].
|
|
||||||
scores := Grid new rows: rows ]
|
|
||||||
|
|
||||||
computeScores
|
computeScores
|
||||||
[ (8 to: 0 by: -1) do: [:i | self computeScoresForHeight: i]. ]
|
[ (8 to: 0 by: -1) do: [:i | self computeScoresForHeight: i]. ]
|
||||||
|
|
||||||
computeScoresForHeight: h
|
computeScoresForHeight: h
|
||||||
[ | h1 | h1 := h + 1.
|
[ | h1 | h1 := h + 1.
|
||||||
heightMap allPosnsDo: [
|
(heightMap allPosnsOf: h) do: [
|
||||||
:pos |
|
:pos | | res | res := self initScore.
|
||||||
(heightMap at: pos) = h ifTrue: [
|
allDirs collect: [
|
||||||
| res | res := self initScore.
|
:off | | offp | offp := pos + off.
|
||||||
allDirs collect: [
|
((heightMap at: offp) = h1) ifTrue: [
|
||||||
:off | | offp | offp := pos + off.
|
res := self combineScore: res with: (scores at: offp) ]].
|
||||||
((heightMap at: offp) = h1) ifTrue:
|
scores at: pos put: res ]]
|
||||||
[ res := self combineScore: res with: (scores at: offp) ]].
|
|
||||||
scores at: pos put: res ]]]
|
|
||||||
|
|
||||||
trailheadScores
|
trailheadScores
|
||||||
[ ^(heightMap allPosnsOf: 0) collect: [:p | self numberFromScore: (scores at: p)] ]
|
[ ^(heightMap allPosnsOf: 0) collect: [
|
||||||
|
:p | self numberFromScore: (scores at: p)] ]
|
||||||
]
|
]
|
||||||
|
|
||||||
TrailMap subclass: TrailMapP2 [
|
TrailMap subclass: TrailMapP2 [
|
||||||
|
|
@ -48,7 +55,7 @@ TrailMap subclass: TrailMapP2 [
|
||||||
AOC input: [ | rows |
|
AOC input: [ | rows |
|
||||||
rows := stdin toLines asArray collect: [
|
rows := stdin toLines asArray collect: [
|
||||||
:line | line asArray collect: [:it | it digitValue]].
|
:line | line asArray collect: [:it | it digitValue]].
|
||||||
Grid new rows: rows ];
|
HeightMap new rows: rows ];
|
||||||
part1: TrailMap; part2: TrailMapP2;
|
part1: TrailMap; part2: TrailMapP2;
|
||||||
result: [ :heights :Class | | map |
|
result: [ :heights :Class | | map |
|
||||||
map := Class new heights: heights.
|
map := Class new heights: heights.
|
||||||
|
|
|
||||||
|
|
@ -261,6 +261,14 @@ Object subclass: Grid [
|
||||||
|
|
||||||
printOn: st [ rows do: [ :r | st << r; nl ] ]
|
printOn: st [ rows do: [ :r | st << r; nl ] ]
|
||||||
]
|
]
|
||||||
|
Grid class extend [
|
||||||
|
width: w height: h initWith: block
|
||||||
|
[ | rows |
|
||||||
|
rows := (1 to: h) collect: [
|
||||||
|
:y | (1 to: h) collect: [
|
||||||
|
:x | block value: (Posn x: x y: y)]].
|
||||||
|
^self new rows: rows ]
|
||||||
|
]
|
||||||
|
|
||||||
Dictionary extend [
|
Dictionary extend [
|
||||||
at: key inA: Type [^self at: key ifAbsentPut: [Type new]]
|
at: key inA: Type [^self at: key ifAbsentPut: [Type new]]
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue