chiark / gitweb /
22b: pad map so we can detect falling off
authorBen Harris <bjh21@bjh21.me.uk>
Sat, 1 Jun 2024 16:49:48 +0000 (17:49 +0100)
committerBen Harris <bjh21@bjh21.me.uk>
Sat, 1 Jun 2024 16:49:48 +0000 (17:49 +0100)
22/22b.bqn [new file with mode: 0644]

diff --git a/22/22b.bqn b/22/22b.bqn
new file mode 100644 (file)
index 0000000..99bbf69
--- /dev/null
@@ -0,0 +1,28 @@
+Pad←{ ⟨h,w⟩←≢𝕩 ⋄ »˘»(2+w)↑˘(2+h)↑𝕩 }
+PrepMap←{ Pad >(⌈´≠¨𝕩)⊸↑¨𝕩 }
+PrepStep←{ 𝕊"L": 'L'; 𝕊"R": 'R'; •ParseFloat 𝕩 }
+PrepPath←{ PrepStep¨(+`⟨0⟩∾≠˝⍉2↕𝕩∊"LR")⊔𝕩 }
+Prep←{ ⟨ PrepMap ¯2↓𝕩, PrepPath ¯1⊑𝕩 ⟩ }
+
+Mark←{map𝕊⟨pos,dir⟩:
+  ({𝕊⟨0,1⟩:'>';𝕊⟨1,0⟩:'v';𝕊⟨0,¯1⟩:'<';𝕊⟨¯1,0⟩:'^'}dir)⌾(pos⊸⊑) map
+}
+
+Main←{𝕊⟨map,path⟩:
+  Move←{
+    'L' 𝕊 ⟨pos,dir⟩: ⟨pos,⟨¯1,1⟩×⌽dir⟩;
+    'R' 𝕊 ⟨pos,dir⟩: ⟨pos,⟨1,¯1⟩×⌽dir⟩;
+    n 𝕊 ⟨pos,dir⟩: ⟨dir Advance⍟n pos,dir⟩
+  }
+  Advance←{dir 𝕊 pos:
+    dest←(≢map)|pos+dir
+    dest↩{ (≢map)|𝕩+dir } •_while_ { ' '=𝕩⊑map } dest
+    ('.'=dest⊑map)⊑⟨pos,dest⟩
+  }
+  pos←⟨1,⊑(1⊏map)⊐'.'⟩
+  dir←⟨0,1⟩
+  ⟨⟨r,c⟩,d⟩ ← ⟨pos,dir⟩ Move´ ⌽path
+  (1000×r) + (4×c) + (|1-˜+´⟨2,1⟩×d)
+}
+
+•Show Main Prep •file.Lines ⊑•args