chiark / gitweb /
19a done
authorBen Harris <bjh21@bjh21.me.uk>
Mon, 27 May 2024 12:51:44 +0000 (13:51 +0100)
committerBen Harris <bjh21@bjh21.me.uk>
Mon, 27 May 2024 12:52:34 +0000 (13:52 +0100)
19/19a.bqn

index 88e75bf09a81c38addd686800c8c1c4cfaf83591..28be0de48a9203eca1c36b9bdc4237aee65340ee 100644 (file)
@@ -1,26 +1,48 @@
-# Material types are ore, clay, obsidian, geode.
+Split←{
+  blocknums ← +`𝕨
+  # Set entries in blocknums to -1 when they correspond with delimiters.
+  blocknums ↩ (¯1¨)⌾(𝕨⊸/) blocknums
+  blocknums⊔𝕩
+}
 
-# Initially just handle the first blueprint.
+# Material types are ore, clay, obsidian, geode.
 
-robot_costs ← [⟨4,0,0,0⟩,⟨2,0,0,0⟩,⟨3,14,0,0⟩,⟨2,0,7,0⟩]
+# Only the numbers differ between blueprints.
+Prep←{
+  words←(' '=𝕩)Split 𝕩
+  •ParseFloat¨(⋈¨[⟨6, ¯1,¯1,¯1⟩,
+                  ⟨12,¯1,¯1,¯1⟩,
+                  ⟨18,21,¯1,¯1⟩,
+                  ⟨27,¯1,30,¯1⟩])⊑words∾⟨"0"⟩
+}¨
 
 # 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⟩:
-  3⊑stock+robots×time  # If not, return number of geodes we can get in time.
+EvalBlueprint←{𝕊 robot_costs:
+  max_costs←⌈´˘robot_costs
+  Search←{ 𝕊⟨time,stock,robots⟩:
+    #•Show ⟨time,stock,robots⟩
+    result←⌈´{
+      needed←(𝕩⊏robot_costs)-stock
+      needed_time←1+0⌈⌈´⌈NtZ needed÷robots
+      # Build a robot if there's time and we might need it.
+      (needed_time≤time)∧(𝕩⊑stock<max_costs×time) ?
+      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⟩:
+    3⊑stock+robots×time  # If not, return number of geodes we can get in time.
+  }
+  •Show robot_costs
+  •Show Search ⟨24,⟨0,0,0,0⟩,⟨1,0,0,0⟩⟩
+}
+
+Main←{
+  +´(EvalBlueprint¨𝕩)×(1+↕≠𝕩)
 }
 
-•Show Search ⟨24,⟨0,0,0,0⟩,⟨1,0,0,0⟩⟩
+•Show Main Prep •file.Lines ⊑•args