Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- " Extend Point for Manhattan distance: "
- Point extend [ distance: pt [^(self x - pt x) abs + (self y - pt y) abs] ]
- " Class to convert input into a Array of the path: "
- Object subclass: TextGrid [
- | grid dim start end |
- dirs := {(0@-1). (-1@0). (1@0). (0 @1)}.
- TextGrid class >> new: mapArray [^super new init: mapArray]
- init: mapArray [
- | sentinel |
- dim := (mapArray first size) @ (mapArray size).
- grid := mapArray join.
- start := self toCoord: (grid indexOfSubCollection: 'S').
- end := self toCoord: (grid indexOfSubCollection: 'E').
- ^self
- ]
- " Access to grid via Points "
- at: pt [^grid at: (pt y * dim x) + pt x + 1]
- at: pt put: chr [^grid at: (pt y * dim x) + pt x + 1 put: chr]
- toCoord: idx [^(idx - 1 \\ dim x) @ (idx - 1 // dim x)]
- " Return path from start to end as and array of Points "
- getPath [
- | path pos back dir |
- path := OrderedCollection with: start.
- pos := start.
- back := (0@0).
- [pos ~= end] whileTrue: [
- dir := dirs detect: [:d | ((self at: (pos + d)) ~= $#) and: [back ~= d]].
- back := dir * -1.
- pos := path add: (pos + dir).
- ].
- ^path asArray
- ]
- ]
- "
- | Mainline
- "
- grid := TextGrid new: stdin lines contents.
- path := grid getPath.
- " Done with the grid, lets just use the path list for the rest. "
- part1 := 0.
- part2 := 0.
- targ := 100. " Target number of picoseconds for a cheat "
- " Compare points that are far enough apart on the path to potentially gain target."
- (1 to: path size - (targ + 2)) do: [:i |
- (i \\ 50 == 0) ifTrue: [
- stderr nextPutAll: ('[%1] %2' % {i. part2}); cr; flush.
- ].
- (i + (targ + 2) to: path size) do: [:j |
- dist := (path at: i) distance: (path at: j).
- " Check allowed distance, and if we didn't lose too much time. "
- (dist <= 20 and: [j - i - dist >= targ]) ifTrue: [
- part2 := part2 + 1.
- (dist == 2) ifTrue: [part1 := part1 + 1].
- ].
- ].
- ].
- stdout nl.
- ('Part 1: %1' % {(part1)}) displayNl.
- ('Part 2: %1' % {(part2)}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement