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⟩⟩