The Runaway Ore Cart
The mine under bitwick is worked out, but one cart still sits on the rails, and the mine-captain will pay for a single last run.
You are given rows, a non-empty list of strings of equal length: the mine floor seen from above. Row 0 is the north edge and column 0 the west edge, so north is row minus one, south is row plus one, east is column plus one, west is column minus one. Exactly one square holds S. The floor is at most 12 by 12 and holds at most 6 switch squares.
The squares:
.bare rail, andSbehaves as bare rail*a pile of ore/and\mirrors#a bufferTa switch
The cart starts on the S square heading east. It then repeats this step: look at the square one place ahead in its current heading.
- If that square is outside the grid, the run ends.
- If it holds
#, the cart does not move, and its heading reverses (turns 180°). - Otherwise the cart moves onto that square, and the square acts on it:
.orS: nothing.*: one ore is taken, and that square becomes bare rail for the rest of the run. The heading does not change./: east becomes north, north becomes east, west becomes south, south becomes west.\: east becomes south, south becomes east, west becomes north, north becomes west.T: every switch square keeps its own tally. On the cart's first arrival at that square the cart turns 90° left (east→north, north→west, west→south, south→east); on its second arrival at that same square, 90° right; alternating on every later arrival there.
oreRun(rows) returns the number of ore taken if the run ends, and -1 if the cart never leaves the grid.
examples
- case 1
rows = ["S.*.*"]2 - the cart rolls east along the single row, takes both piles, and leaves past the last column - case 2
rows = ["#S*#"]-1 - it takes the one pile, then bounces between the two buffers forever; the ore already taken does not matter - case 3
rows = ["/\..", "ST*.", "\/.."]1 - the switch turns the cart left, the four mirrors bring it back to the same switch, which now turns it right, and it leaves east past the ore
- reward
- 520 XP
- signature
oreRun
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- straight run takes both piles
- mirrors bend the rails
- switch turns left then right
- trapped between two buffers
- ore taken but never leaves
- each switch keeps its own tally
- same corridor twice is not a loop