chiark / gitweb /
19b done
authorBen Harris <bjh21@bjh21.me.uk>
Tue, 28 May 2024 08:32:22 +0000 (09:32 +0100)
committerBen Harris <bjh21@bjh21.me.uk>
Thu, 6 Jun 2024 22:51:04 +0000 (23:51 +0100)
(committed late because I forgot)

19/19b.bqn [new file with mode: 0644]

diff --git a/19/19b.bqn b/19/19b.bqn
new file mode 100644 (file)
index 0000000..90d075f
--- /dev/null
@@ -0,0 +1,48 @@
+Split←{
+  blocknums ← +`𝕨
+  # Set entries in blocknums to -1 when they correspond with delimiters.
+  blocknums ↩ (¯1¨)⌾(𝕨⊸/) blocknums
+  blocknums⊔𝕩
+}
+
+# Material types are ore, clay, obsidian, geode.
+
+# 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.
+
+EvalBlueprint←{𝕊 robot_costs:
+  •Show robot_costs
+  •Show max_costs←⌈˝(robot_costs∾[⟨0,0,0,∞⟩]) # Want all the geodes.
+  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+robots×time)<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 Search ⟨32,⟨0,0,0,0⟩,⟨1,0,0,0⟩⟩
+}
+
+Main←{
+  ×´EvalBlueprint¨3↑𝕩
+}
+
+•Show Main Prep •file.Lines ⊑•args