chiark / gitweb /
17b works on example
authorBen Harris <bjh21@bjh21.me.uk>
Sat, 25 May 2024 21:22:22 +0000 (22:22 +0100)
committerBen Harris <bjh21@bjh21.me.uk>
Sat, 25 May 2024 21:22:22 +0000 (22:22 +0100)
17/17b.bqn [new file with mode: 0644]

diff --git a/17/17b.bqn b/17/17b.bqn
new file mode 100644 (file)
index 0000000..2be224b
--- /dev/null
@@ -0,0 +1,89 @@
+rocks←⟨
+  ["...####.."]
+
+  ["....#...."
+   "...###..."
+   "....#...."]
+
+  [".....#..."
+   ".....#..."
+   "...###..."]
+
+  ["...#....."
+   "...#....."
+   "...#....."
+   "...#....."]
+
+  ["...##...."
+   "...##...."]
+⟩
+rocks ".#"⊸⊐¨↩ # Convert to boolean array
+
+Prep←(⊏⟜⟨«˘,»˘⟩)∘("<>"⊸⊐)
+
+Extend←⟨1,0,0,0,0,0,0,0,1⟩⊸∾
+
+Space←{+´2=+˝˘𝕩}
+
+Cast←{
+  CastRow←{ row_above 𝕊 arena_row:
+    FloodLeft←{
+      FloodLeft1←{ 𝕩∨(«𝕩)∧¬𝕨 }
+      𝕨 FloodLeft1⍟(≠𝕩) 𝕩
+    }
+    FloodRight←{
+      FloodRight1←{ 𝕩∨(»𝕩)∧¬𝕨 }
+      𝕨 FloodRight1⍟(≠𝕩) 𝕩
+    }
+    arena_row FloodRight arena_row FloodLeft row_above∧¬arena_row
+  }
+  # ` on a 2D array applies 𝔽 once for each element of the array,
+  # which isn't really what we want, so we convert the arena into a
+  # list of rows and then convert the result back.
+  rows←(<(≢⊏𝕩)⥊1)CastRow`<˘𝕩
+  >(0≠+´¨rows)/rows
+}
+
+Main←{𝕊 jets:
+  arena←⟨1,9⟩⥊1
+  jetpos←0
+  thisrock←0
+  rockcount←0
+  castlog←⟨⟩•HashMap⟨⟩
+  target←1000000000000
+  {𝕤
+    falling←thisrock⊑rocks
+    arena Extend⍟((3+≠falling)-(Space arena)) ↩
+    thiscast←Cast arena
+    •Show ⟨thiscast,thisrock⟩
+    {𝕤
+      ⟨wasrockcount,washeight⟩←castlog.Get⟨thiscast,thisrock⟩
+      {𝕤
+        •Show "Got it!"
+        nowheight←(≠arena)-(1+Space arena)
+        •Show nowheight+(nowheight-washeight)×(target-rockcount)÷(rockcount-wasrockcount)
+        •Exit 0
+      }⍟(0=(rockcount-wasrockcount)|(target-rockcount)) @
+    }⍟(castlog.Has ⟨thiscast,thisrock⟩) @
+    ⟨thiscast,thisrock⟩ castlog.Set ⟨rockcount,(≠arena)-(1+Space arena)⟩
+    ⟨fallen, fallenpos⟩ ← { 𝕊 ⟨falling,fallpos⟩:
+      fallpos ↩ 1+fallpos
+      jet←jetpos⊑jets
+      jetpos↩(≠jets)|1+jetpos
+      shifted ← Jet falling
+      obstructed ← ∨´⥊shifted∧(≠shifted)↑fallpos↓arena
+      falling ↩ obstructed ⊑ ⟨shifted,falling⟩
+      #•Show ⟨fallpos,falling∨(≠shifted)↑fallpos↓arena⟩
+      ⟨falling,fallpos⟩
+    } •_while_ { 𝕊 ⟨falling,fallpos⟩:
+      ¬∨´⥊falling∧(≠falling)↑(1+fallpos)↓arena
+    } ⟨falling,¯1⟩
+    arena↩ {𝕩∨fallen}⌾{((≠fallen)⊸↑)∘(fallenpos⊸↓)} arena
+    thisrock↩(≠rocks)|1+thisrock
+    rockcount+↩1
+    •Show arena⊏".#"
+  }¨↕100
+  (≠arena)-(1+Space arena)
+}
+
+•Show Main Prep ⊑•file.Lines ⊑•args