about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2020-12-16 20:54:26 -0500
committerBen Harris <ben@tilde.team>2020-12-16 20:54:26 -0500
commit34d27fd823ef60e8c4165d522ed4d8453d1b9626 (patch)
tree61c839bbd344836bc9a0af4bdb7fbc34dd389c5c
parent98f3016b477077a8478895b67ee37af71ae68717 (diff)
day 19
-rw-r--r--aoc2019.test/Tests.cs2
-rw-r--r--aoc2019/Day19.cs32
2 files changed, 24 insertions, 10 deletions
diff --git a/aoc2019.test/Tests.cs b/aoc2019.test/Tests.cs
index 61f9093..ebe7a7e 100644
--- a/aoc2019.test/Tests.cs
+++ b/aoc2019.test/Tests.cs
@@ -26,7 +26,7 @@ namespace aoc2019.test
         [DataRow(typeof(Day15), "280", "400")]
         [DataRow(typeof(Day16), "90744714", "82994322")]
         [DataRow(typeof(Day17), "2804", "")]
-        [DataRow(typeof(Day19), "", "")]
+        [DataRow(typeof(Day19), "114", "10671712")]
         public void TestAllDays(Type dayType, string part1, string part2)
         {
             // create day instance
diff --git a/aoc2019/Day19.cs b/aoc2019/Day19.cs
index 73d3e22..3ce8ee9 100644
--- a/aoc2019/Day19.cs
+++ b/aoc2019/Day19.cs
@@ -1,4 +1,5 @@
-using System.Linq;
+using System;
+using System.Linq;
 using aoc2019.lib;
 
 namespace aoc2019
@@ -6,30 +7,43 @@ namespace aoc2019
     public sealed class Day19 : Day
     {
         private readonly IntCodeVM vm;
-
+        private readonly long[,] grid;
         public Day19() : base(19, "Tractor Beam")
         {
             vm = new IntCodeVM(Input.First());
+            grid = new long[50, 50];
         }
 
         public override string Part1()
         {
-            var count = 0;
-
             for (var x = 0; x < 50; x++)
                 for (var y = 0; y < 50; y++)
                 {
-                    vm.AddInput(x, y);
-                    vm.Run();
-                    if (vm.Result == 1) count++;
+                    vm.Reset();
+                    vm.Run(x, y);
+                    grid[x, y] = vm.Result;
                 }
 
-            return $"{count}";
+            return $"{grid.Cast<long>().Sum()}";
         }
 
         public override string Part2()
         {
-            return "";
+            for (int x = 101, y = 0;; x++)
+            {
+                while (true)
+                {
+                    vm.Reset();
+                    vm.Run(x, y);
+                    if (vm.Result == 1) break;
+                    y++;
+                }
+
+                vm.Reset();
+                vm.Run(x - 99, y + 99);
+                if (vm.Result == 1)
+                    return $"{(x - 99) * 1e4 + y}";
+            }
         }
     }
 }