about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2020-12-16 23:37:03 -0500
committerBen Harris <ben@tilde.team>2020-12-16 23:38:49 -0500
commitbbd0d2b4db5f40ddf09c456f195e68f91251172c (patch)
treeb323c3690c0c47d0ada0b318ba62856550a0c438
parent34d27fd823ef60e8c4165d522ed4d8453d1b9626 (diff)
day 23
-rw-r--r--aoc2019.test/Tests.cs10
-rw-r--r--aoc2019/Day08.cs2
-rw-r--r--aoc2019/Day11.cs14
-rw-r--r--aoc2019/Day17.cs3
-rw-r--r--aoc2019/Day19.cs20
-rw-r--r--aoc2019/Day21.cs27
-rw-r--r--aoc2019/Day23.cs90
7 files changed, 143 insertions, 23 deletions
diff --git a/aoc2019.test/Tests.cs b/aoc2019.test/Tests.cs
index ebe7a7e..acfb08d 100644
--- a/aoc2019.test/Tests.cs
+++ b/aoc2019.test/Tests.cs
@@ -16,10 +16,12 @@ namespace aoc2019.test
         [DataRow(typeof(Day05), "7692125", "14340395")]
         [DataRow(typeof(Day06), "145250", "274")]
         [DataRow(typeof(Day07), "19650", "35961106")]
-        [DataRow(typeof(Day08), "2413", "xxx   xx  xxx  xxxx xxx  \nx  x x  x x  x    x x  x \nxxx  x    x  x   x  xxx  \nx  x x    xxx   x   x  x \nx  x x  x x    x    x  x \nxxx   xx  x    xxxx xxx  ")]
+        [DataRow(typeof(Day08), "2413",
+            "\nxxx   xx  xxx  xxxx xxx  \nx  x x  x x  x    x x  x \nxxx  x    x  x   x  xxx  \nx  x x    xxx   x   x  x \nx  x x  x x    x    x  x \nxxx   xx  x    xxxx xxx  ")]
         [DataRow(typeof(Day09), "3409270027", "82760")]
         [DataRow(typeof(Day10), "260", "608")]
-        [DataRow(typeof(Day11), "2054", " #  # ###  #### ####  ##    ## #  # ###    \n # #  #  #    # #    #  #    # #  # #  #   \n ##   #  #   #  ###  #  #    # #### ###    \n # #  ###   #   #    ####    # #  # #  #   \n # #  # #  #    #    #  # #  # #  # #  #   \n #  # #  # #### #### #  #  ##  #  # ###    ")]
+        [DataRow(typeof(Day11), "2054",
+            "\n #  # ###  #### ####  ##    ## #  # ###    \n # #  #  #    # #    #  #    # #  # #  #   \n ##   #  #   #  ###  #  #    # #### ###    \n # #  ###   #   #    ####    # #  # #  #   \n # #  # #  #    #    #  # #  # #  # #  #   \n #  # #  # #### #### #  #  ##  #  # ###    ")]
         [DataRow(typeof(Day12), "10635", "583523031727256")]
         //[DataRow(typeof(Day13), "361", "after 7133 moves, the score is: 17590")]
         [DataRow(typeof(Day14), "397771", "3126714")]
@@ -27,6 +29,8 @@ namespace aoc2019.test
         [DataRow(typeof(Day16), "90744714", "82994322")]
         [DataRow(typeof(Day17), "2804", "")]
         [DataRow(typeof(Day19), "114", "10671712")]
+        [DataRow(typeof(Day21), "", "")]
+        [DataRow(typeof(Day23), "23626", "19019")]
         public void TestAllDays(Type dayType, string part1, string part2)
         {
             // create day instance
@@ -53,4 +57,4 @@ namespace aoc2019.test
             Assert.AreEqual(part2, part2Actual, $"Incorrect answer for Day {day.DayNumber} Part2");
         }
     }
-}
+}
\ No newline at end of file
diff --git a/aoc2019/Day08.cs b/aoc2019/Day08.cs
index 80ba7b0..6a7bba4 100644
--- a/aoc2019/Day08.cs
+++ b/aoc2019/Day08.cs
@@ -21,7 +21,7 @@ namespace aoc2019
 
         public override string Part2()
         {
-            return Enumerable.Range(0, 25 * 6)
+            return "\n" + Enumerable.Range(0, 25 * 6)
                 .Select(p => Enumerable.Range(0, photo.Count)
                     .Select(l => photo[l][p])
                     .Aggregate('2', (acc, next) =>
diff --git a/aoc2019/Day11.cs b/aoc2019/Day11.cs
index 31a5d88..2a299cb 100644
--- a/aoc2019/Day11.cs
+++ b/aoc2019/Day11.cs
@@ -85,15 +85,15 @@ namespace aoc2019
         public override string Part2()
         {
             var map = PaintShip(1);
-            var minX = (int) map.Keys.Select(x => x.x).Min();
-            var maxX = (int) map.Keys.Select(x => x.x).Max();
-            var minY = (int) map.Keys.Select(x => x.y).Min();
-            var maxY = (int) map.Keys.Select(x => x.y).Max();
+            var minX = (int) map.Keys.Select(i => i.x).Min();
+            var maxX = (int) map.Keys.Select(i => i.x).Max();
+            var minY = (int) map.Keys.Select(i => i.y).Min();
+            var maxY = (int) map.Keys.Select(i => i.y).Max();
 
-            return Enumerable.Range(minY, maxY - minY + 1)
-                .Select(y =>
+            return "\n" + Enumerable.Range(minY, maxY - minY + 1)
+                .Select(j =>
                     Enumerable.Range(minX, maxX - minX + 1)
-                        .Select(x => map.GetValueOrDefault((x, y)) == 0 ? ' ' : '#')
+                        .Select(i => map.GetValueOrDefault((x: i, y: j)) == 0 ? ' ' : '#')
                         .ToDelimitedString()
                 )
                 .Reverse()
diff --git a/aoc2019/Day17.cs b/aoc2019/Day17.cs
index 557881e..0a3b550 100644
--- a/aoc2019/Day17.cs
+++ b/aoc2019/Day17.cs
@@ -7,8 +7,7 @@ namespace aoc2019
 {
     public sealed class Day17 : Day
     {
-        private const bool Verbose = false;
-
+        private readonly bool Verbose = false;
         private readonly IntCodeVM vm;
 
         public Day17() : base(17, "Set and Forget")
diff --git a/aoc2019/Day19.cs b/aoc2019/Day19.cs
index 3ce8ee9..fe6b435 100644
--- a/aoc2019/Day19.cs
+++ b/aoc2019/Day19.cs
@@ -1,13 +1,13 @@
-using System;
-using System.Linq;
+using System.Linq;
 using aoc2019.lib;
 
 namespace aoc2019
 {
     public sealed class Day19 : Day
     {
-        private readonly IntCodeVM vm;
         private readonly long[,] grid;
+        private readonly IntCodeVM vm;
+
         public Day19() : base(19, "Tractor Beam")
         {
             vm = new IntCodeVM(Input.First());
@@ -17,12 +17,12 @@ namespace aoc2019
         public override string Part1()
         {
             for (var x = 0; x < 50; x++)
-                for (var y = 0; y < 50; y++)
-                {
-                    vm.Reset();
-                    vm.Run(x, y);
-                    grid[x, y] = vm.Result;
-                }
+            for (var y = 0; y < 50; y++)
+            {
+                vm.Reset();
+                vm.Run(x, y);
+                grid[x, y] = vm.Result;
+            }
 
             return $"{grid.Cast<long>().Sum()}";
         }
@@ -46,4 +46,4 @@ namespace aoc2019
             }
         }
     }
-}
+}
\ No newline at end of file
diff --git a/aoc2019/Day21.cs b/aoc2019/Day21.cs
new file mode 100644
index 0000000..e2bc3fb
--- /dev/null
+++ b/aoc2019/Day21.cs
@@ -0,0 +1,27 @@
+using System.Linq;
+using aoc2019.lib;
+
+namespace aoc2019
+{
+    public sealed class Day21 : Day
+    {
+        private readonly IntCodeVM vm;
+
+        public Day21() : base(21, "Springdroid Adventure")
+        {
+            vm = new IntCodeVM(Input.First());
+        }
+
+        public override string Part1()
+        {
+            vm.Reset();
+            var halt = vm.Run();
+            return "";
+        }
+
+        public override string Part2()
+        {
+            return "";
+        }
+    }
+}
\ No newline at end of file
diff --git a/aoc2019/Day23.cs b/aoc2019/Day23.cs
new file mode 100644
index 0000000..53c840c
--- /dev/null
+++ b/aoc2019/Day23.cs
@@ -0,0 +1,90 @@
+using System.Linq;
+using aoc2019.lib;
+
+namespace aoc2019
+{
+    public sealed class Day23 : Day
+    {
+        public Day23() : base(23, "Category Six")
+        {
+        }
+
+        public override string Part1()
+        {
+            var vms = Enumerable.Range(0, 50)
+                .Select((s, i) =>
+                {
+                    var vm = new IntCodeVM(Input.First());
+                    vm.Run(i);
+                    return vm;
+                }).ToList();
+
+            while (true)
+                foreach (var vm in vms)
+                {
+                    while (vm.output.Count > 0)
+                    {
+                        var destination = (int) vm.Result;
+                        var x = vm.Result;
+                        var y = vm.Result;
+
+                        if (destination == 255) return $"{y}";
+
+                        vms[destination].Run(x, y);
+                    }
+
+                    vm.Run(-1);
+                }
+        }
+
+        public override string Part2()
+        {
+            var vms = Enumerable.Range(0, 50)
+                .Select((s, i) =>
+                {
+                    var vm = new IntCodeVM(Input.First());
+                    vm.Run(i);
+                    return vm;
+                }).ToList();
+
+            long natX = 0, natY = 0, lastYSent = -1;
+
+            while (true)
+            {
+                var numIdle = 0;
+                foreach (var vm in vms)
+                {
+                    var isIdle = true;
+                    while (vm.output.Count > 0)
+                    {
+                        var destination = (int) vm.Result;
+                        var x = vm.Result;
+                        var y = vm.Result;
+
+                        if (destination == 255)
+                        {
+                            natX = x;
+                            natY = y;
+                        }
+                        else
+                        {
+                            vms[destination].Run(x, y);
+                        }
+
+                        isIdle = false;
+                    }
+
+                    vm.Run(-1);
+                    if (isIdle) numIdle++;
+                }
+
+                if (numIdle == 50)
+                {
+                    if (natY == lastYSent) return $"{natY}";
+                    vms[0].Run(natX, natY);
+                    lastYSent = natY;
+                }
+            }
+        }
+    }
+}
\ No newline at end of file