Day 18 cleanup

This commit is contained in:
Beatrice Szilvasy 2024-12-18 06:40:22 +00:00
parent e0256a440d
commit 855b3e2d3d
2 changed files with 16 additions and 24 deletions

View file

@ -1,7 +1,6 @@
Object subclass: MemorySpace [ Object subclass: MemorySpace [
| corruption origin dirs end | corruption origin dirs end
grid steps size grid size queue |
queue seen |
initialize initialize
[ origin := Posn x: 1 y: 1. [ origin := Posn x: 1 y: 1.
@ -10,16 +9,8 @@ Object subclass: MemorySpace [
corruption: n [corruption := n collect: [:it | it + origin]] corruption: n [corruption := n collect: [:it | it + origin]]
corruption [^corruption] corruption [^corruption]
grid [^grid]
plotAtTime: time
[ | g | g := Grid width: size height: size initWith: [:i | $.].
g rows: (g rows collect: [:it | it asString]).
1 to: time do: [:i | g at: (corruption at: i) put: $#].
g printNl ]
findExitWithInitial: initial findExitWithInitial: initial
[ | stepsInitial | [ | steps stepsInitial |
size := 71. size := 71.
grid := Grid width: size height: size initWith: [ grid := Grid width: size height: size initWith: [
:i | {"blocked at: "FloatD infinity. :i | {"blocked at: "FloatD infinity.
@ -27,7 +18,7 @@ Object subclass: MemorySpace [
corruption keysAndValuesDo: [ corruption keysAndValuesDo: [
:time :pos | (grid at: pos) at: 1 put: time ]. :time :pos | (grid at: pos) at: 1 put: time ].
queue := OrderedCollection new. queue := LookupTable new.
self enqueue: origin withLastTime: ((grid at: origin) at: 1). self enqueue: origin withLastTime: ((grid at: origin) at: 1).
steps := 0. steps := 0.
@ -49,27 +40,28 @@ Object subclass: MemorySpace [
stepOnce stepOnce
[ | oldQueue | [ | oldQueue |
oldQueue := queue. oldQueue := queue.
queue := OrderedCollection new. queue := LookupTable new.
oldQueue do: [ oldQueue keysAndValuesDo: [
:posn :time | dirs do: [ :posn :time | dirs do: [
:dir | :dir | self enqueue: posn + dir withLastTime: time ]]]
self enqueue: (posn + dir)
withLastTime: time ]]
asSpreader ]
enqueue: posn withLastTime: lastTime enqueue: posn withLastTime: lastTime
[ | entry bestTime | [ | entry bestTime |
entry := grid at: posn. entry := grid at: posn.
entry ifNil: [^false]. entry ifNil: [^false].
bestTime := (entry at: 1) bestTime := lastTime min: (entry at: 1).
ifNil: [lastTime]
ifNotNil: [:e | lastTime min: e].
(entry at: 2) ifNotNil: [ (entry at: 2) ifNotNil: [
:prevLastReachable | :prevLastReachable |
bestTime <= prevLastReachable ifTrue: [^false]]. bestTime > prevLastReachable ifFalse: [^false]].
entry at: 2 put: bestTime. entry at: 2 put: bestTime.
queue add: {posn . bestTime}. queue at: posn put: bestTime.
^true ] ^true ]
plotAtTime: time
[ | g | g := Grid width: size height: size initWith: [:i | $.].
g rows: (g rows collect: [:it | it asString]).
1 to: time do: [:i | g at: (corruption at: i) put: $#].
g printNl ]
] ]
AOC input: [ | posns | AOC input: [ | posns |

View file

@ -285,7 +285,7 @@ Object subclass: Grid [
rows: n [rows:=n] rows: n [rows:=n]
rows [^rows] rows [^rows]
at: pos [ ^(rows at: pos y ifAbsent: [^nil]) at: pos x ifAbsent: [nil] ] at: pos [ ^(rows at: pos y ifAbsent: [^nil]) at: pos x ifAbsent: [^nil] ]
at: pos put: elt at: pos put: elt
[ ^(rows at: pos y ifAbsent: [self error: 'Out of bounds']) [ ^(rows at: pos y ifAbsent: [self error: 'Out of bounds'])
at: pos x put: elt ] at: pos x put: elt ]