chiark / gitweb /
19a gets the example blueprint 1 right
authorBen Harris <bjh21@bjh21.me.uk>
Mon, 27 May 2024 10:40:14 +0000 (11:40 +0100)
committerBen Harris <bjh21@bjh21.me.uk>
Mon, 27 May 2024 10:40:30 +0000 (11:40 +0100)
Takes 17.8 seconds to do so.

19/19a.bqn

index 1b8b26719342578ee7763a30eb9840651333dad1..88e75bf09a81c38addd686800c8c1c4cfaf83591 100644 (file)
@@ -4,13 +4,23 @@
 
 robot_costs ← [⟨4,0,0,0⟩,⟨2,0,0,0⟩,⟨3,14,0,0⟩,⟨2,0,7,0⟩]
 
-Search←{
-  𝕊⟨·,stock,·⟩: ∨´0>stock ? ¯∞; # Bankruptcy is undesirable.
-  𝕊⟨0,stock,·⟩: 3⊑stock;
+# 0÷0 is NaN.  But for our purposes we want it to be 0.
+NtZ←{=˜𝕩 ? 𝕩; 0}¨ # NaN is not equal to itself.
+
+Search←{ 𝕊⟨time,stock,robots⟩:
+  #•Show ⟨time,stock,robots⟩
+  result←⌈´{
+    needed←(𝕩⊏robot_costs)-stock
+    needed_time←1+0⌈⌈´⌈NtZ needed÷robots
+    needed_time≤time ? # If there's enough time, built a robot.
+    Search ⟨time-needed_time
+     (stock+robots×needed_time)-𝕩⊏robot_costs
+     (1⊸+)⌾(𝕩⊸⊑)robots⟩;
+    ¯∞ # No time to built this kind of robot.
+  }¨↕≠robot_costs
+  result≥0 ? result;   # Return if any robot can be built.
   𝕊⟨time,stock,robots⟩:
-    •Show ⟨time,stock,robots⟩
-    ⌈´Search¨⟨⟨time-1,stock+robots,robots⟩⟩∾{
-        ⟨time-1,stock+robots-𝕩⊏robot_costs,(1⊸+)⌾(𝕩⊸⊑)robots⟩}¨↕≠robot_costs
+  3⊑stock+robots×time  # If not, return number of geodes we can get in time.
 }
 
-•Show Search ⟨5,⟨0,0,0,0⟩,⟨1,0,0,0⟩⟩
+•Show Search ⟨24,⟨0,0,0,0⟩,⟨1,0,0,0⟩⟩