Day 15 cleanup (?)
This commit is contained in:
parent
296a1c2e4c
commit
45b4c6ccca
1 changed files with 18 additions and 43 deletions
|
|
@ -7,6 +7,7 @@ Object subclass: Transaction [
|
||||||
[ o updates keysAndValuesDo: [
|
[ o updates keysAndValuesDo: [
|
||||||
:pos :new | self at: pos put: new ]]
|
:pos :new | self at: pos put: new ]]
|
||||||
|
|
||||||
|
movedFrom: pos [ self at: pos put: $. ]
|
||||||
at: pos put: new
|
at: pos put: new
|
||||||
[ | old |
|
[ | old |
|
||||||
old := updates at: pos ifAbsent: [nil].
|
old := updates at: pos ifAbsent: [nil].
|
||||||
|
|
@ -36,65 +37,39 @@ Object subclass: Warehouse [
|
||||||
printOn: st [ st << grid. ]
|
printOn: st [ st << grid. ]
|
||||||
|
|
||||||
findRobot
|
findRobot
|
||||||
[ grid allPosnsDo: [
|
[ grid allPosnsDo: [:it | (grid at: it) = $@ ifTrue: [^robot := it]].
|
||||||
:it | (grid at: it) = $@ ifTrue: [
|
|
||||||
^robot := it. ]].
|
|
||||||
self error: 'No robot' ]
|
self error: 'No robot' ]
|
||||||
|
|
||||||
tryMove: src by: off
|
tryMoveIn: trans from: src by: off
|
||||||
[ | dst blk |
|
[ | blk |
|
||||||
blk := grid at: src.
|
blk := grid at: src.
|
||||||
blk = $. ifTrue: [^true].
|
blk = $. ifTrue: [^true].
|
||||||
blk = $# ifTrue: [^false].
|
blk = $# ifTrue: [^false].
|
||||||
dst := src + off.
|
|
||||||
(self tryMove: dst by: off)
|
|
||||||
ifFalse: [^false]
|
|
||||||
ifTrue: [
|
|
||||||
grid at: src put: $..
|
|
||||||
grid at: dst put: blk.
|
|
||||||
^true ] ]
|
|
||||||
|
|
||||||
tryMoveTrans: src by: off
|
|
||||||
[ | dst blk |
|
|
||||||
blk := grid at: src.
|
|
||||||
blk = $. ifTrue: [^Transaction new].
|
|
||||||
blk = $# ifTrue: [^nil].
|
|
||||||
|
|
||||||
off x = 0 ifTrue: [
|
off x = 0 ifTrue: [
|
||||||
blk = $[ ifTrue: [^self tryMoveTrans0: src and: src + Posn right by: off].
|
blk = $[ ifTrue: [^self tryMoveIn0: trans from: src and: src + Posn right by: off].
|
||||||
blk = $] ifTrue: [^self tryMoveTrans0: src and: src + Posn left by: off].
|
blk = $] ifTrue: [^self tryMoveIn0: trans from: src and: src + Posn left by: off].
|
||||||
].
|
].
|
||||||
^self tryMoveTrans0: src by: off ]
|
^self tryMoveIn0: trans from: src by: off ]
|
||||||
|
|
||||||
tryMoveTrans0: src1 and: src2 by: off
|
tryMoveIn0: trans from: src1 and: src2 by: off
|
||||||
[ | dst1 dst2 blk1 blk2 |
|
[ ^(self tryMoveIn0: trans from: src1 by: off) and:
|
||||||
dst1 := src1 + off. dst2 := src2 + off.
|
[self tryMoveIn0: trans from: src2 by: off] ]
|
||||||
blk1 := grid at: src1. blk2 := grid at: src2.
|
|
||||||
^(self tryMoveTrans: dst1 by: off) ifNotNil: [
|
|
||||||
:th | (self tryMoveTrans: dst2 by: off) ifNotNil: [
|
|
||||||
:th2 |
|
|
||||||
th putAll: th2.
|
|
||||||
th at: src1 put: $..
|
|
||||||
th at: src2 put: $..
|
|
||||||
th at: dst1 put: blk1.
|
|
||||||
th at: dst2 put: blk2.
|
|
||||||
th ]]]
|
|
||||||
|
|
||||||
tryMoveTrans0: src by: off
|
tryMoveIn0: trans from: src by: off
|
||||||
[ | dst blk |
|
[ | dst blk |
|
||||||
dst := src + off.
|
dst := src + off.
|
||||||
blk := grid at: src.
|
blk := grid at: src.
|
||||||
^(self tryMoveTrans: dst by: off) ifNotNil: [
|
(self tryMoveIn: trans from: dst by: off) ifTrue: [
|
||||||
:th |
|
trans movedFrom: src; at: dst put: blk.
|
||||||
th at: src put: $..
|
^true ]. ^false ]
|
||||||
th at: dst put: blk.
|
|
||||||
th ]]
|
|
||||||
|
|
||||||
followInsn: insn
|
followInsn: insn
|
||||||
[ | dir |
|
[ | dir trans |
|
||||||
dir := insnMap at: insn ifAbsent: [^false].
|
dir := insnMap at: insn ifAbsent: [^false].
|
||||||
(self tryMoveTrans: robot by: dir) ifNotNil: [
|
trans := Transaction new.
|
||||||
:th | th performOn: grid. robot := robot + dir] ]
|
(self tryMoveIn: trans from: robot by: dir) ifTrue: [
|
||||||
|
trans performOn: grid. robot := robot + dir] ]
|
||||||
|
|
||||||
runInsns [ insns do: [:it | self followInsn: it] ]
|
runInsns [ insns do: [:it | self followInsn: it] ]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue