about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2020-12-16 17:17:35 -0500
committerBen Harris <ben@tilde.team>2020-12-16 17:17:35 -0500
commit837527d487c7e232b36dd87c95a15b7852f2e057 (patch)
tree21ddf9a63548ccc8c802fe5f158f3e6743a8e9f1
parentc66d1d6b335e6c2f6b544ae31793f318ba2b99a7 (diff)
refactor to two-digit day names and add puzzlename
-rw-r--r--Day.cs32
-rw-r--r--Day01.cs (renamed from Day1.cs)6
-rw-r--r--Day02.cs (renamed from Day2.cs)6
-rw-r--r--Day03.cs (renamed from Day3.cs)6
-rw-r--r--Day04.cs (renamed from Day4.cs)6
-rw-r--r--Day05.cs (renamed from Day5.cs)6
-rw-r--r--Day06.cs (renamed from Day6.cs)6
-rw-r--r--Day07.cs (renamed from Day7.cs)7
-rw-r--r--Day08.cs (renamed from Day8.cs)6
-rw-r--r--Day09.cs (renamed from Day9.cs)6
-rw-r--r--Day10.cs24
-rw-r--r--Day11.cs4
-rw-r--r--Day12.cs7
-rw-r--r--Day13.cs10
-rw-r--r--Day14.cs4
-rw-r--r--Day15.cs4
-rw-r--r--Day16.cs4
-rw-r--r--Day17.cs19
-rw-r--r--input/day01.in (renamed from input/day1.in)0
-rw-r--r--input/day02.in (renamed from input/day2.in)0
-rw-r--r--input/day03.in (renamed from input/day3.in)0
-rw-r--r--input/day04.in (renamed from input/day4.in)0
-rw-r--r--input/day05.in (renamed from input/day5.in)0
-rw-r--r--input/day06.in (renamed from input/day6.in)0
-rw-r--r--input/day07.in (renamed from input/day7.in)0
-rw-r--r--input/day08.in (renamed from input/day8.in)0
-rw-r--r--input/day09.in (renamed from input/day9.in)0
-rw-r--r--lib/Extensions.cs16
28 files changed, 84 insertions, 95 deletions
diff --git a/Day.cs b/Day.cs
index f08e7cf..eb3ef49 100644
--- a/Day.cs
+++ b/Day.cs
@@ -2,32 +2,44 @@
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
+using aoc2019.lib;
 
 namespace aoc2019
 {
     public abstract class Day
     {
-        public abstract int DayNumber { get; }
+        protected Day(int dayNumber, string puzzleName)
+        {
+            DayNumber = dayNumber;
+            PuzzleName = puzzleName;
+        }
+
+        public int DayNumber { get; }
+        public string PuzzleName { get; }
 
         protected virtual IEnumerable<string> Input =>
-            File.ReadLines($"input/day{DayNumber}.in");
+            File.ReadLines(FileName);
+
+        protected string FileName =>
+            Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"input/day{DayNumber,2:00}.in");
 
-        public virtual void AllParts(bool verbose = false)
+        public void AllParts(bool verbose = true)
         {
-            Console.WriteLine($"Day {DayNumber}:");
-            var s = new Stopwatch();
-            s.Start();
+            Console.WriteLine($"Day {DayNumber,2}: {PuzzleName}");
+            var s = Stopwatch.StartNew();
             var part1 = Part1();
             s.Stop();
-            if (verbose) Console.WriteLine($"part 1 elapsed ticks: {s.ElapsedTicks}");
-            Console.WriteLine(part1);
+            Console.Write($"Part1: {part1,-15} ");
+            Console.WriteLine(verbose ? $"{s.ScaleMilliseconds()}ms elapsed" : "");
 
             s.Reset();
+
             s.Start();
             var part2 = Part2();
             s.Stop();
-            if (verbose) Console.WriteLine($"part 2 elapsed ticks: {s.ElapsedTicks}");
-            Console.WriteLine(part2);
+            Console.Write($"Part2: {part2,-15} ");
+            Console.WriteLine(verbose ? $"{s.ScaleMilliseconds()}ms elapsed" : "");
+
             Console.WriteLine();
         }
 
diff --git a/Day1.cs b/Day01.cs
index 1e9abef..d991917 100644
--- a/Day1.cs
+++ b/Day01.cs
@@ -3,17 +3,15 @@ using System.Linq;
 
 namespace aoc2019
 {
-    internal sealed class Day1 : Day
+    internal sealed class Day01 : Day
     {
         private readonly IEnumerable<int> masses;
 
-        public Day1()
+        public Day01() : base(1, "The Tyranny of the Rocket Equation")
         {
             masses = Input.Select(int.Parse);
         }
 
-        public override int DayNumber => 1;
-
         private static int FuelCost(int weight)
         {
             return weight / 3 - 2;
diff --git a/Day2.cs b/Day02.cs
index 1022cfe..3a79970 100644
--- a/Day2.cs
+++ b/Day02.cs
@@ -3,17 +3,15 @@ using System.Linq;
 
 namespace aoc2019
 {
-    internal sealed class Day2 : Day
+    internal sealed class Day02 : Day
     {
         private readonly IEnumerable<int> input;
 
-        public Day2()
+        public Day02() : base(2, "1202 Program Alarm")
         {
             input = Input.First().Split(',').Select(int.Parse);
         }
 
-        public override int DayNumber => 2;
-
         public int RunIntCode(int noun, int verb)
         {
             var v = input.ToList();
diff --git a/Day3.cs b/Day03.cs
index 03f7379..e339c27 100644
--- a/Day3.cs
+++ b/Day03.cs
@@ -4,19 +4,17 @@ using System.Linq;
 
 namespace aoc2019
 {
-    internal sealed class Day3 : Day
+    internal sealed class Day03 : Day
     {
         private readonly IEnumerable<(int, int)> intersections;
         private readonly List<Dictionary<(int, int), int>> wires;
 
-        public Day3()
+        public Day03() : base(3, "Crossed Wires")
         {
             wires = Input.Select(ParseWire).ToList();
             intersections = wires[0].Keys.Intersect(wires[1].Keys);
         }
 
-        public override int DayNumber => 3;
-
         protected override string Part1()
         {
             return $"{intersections.Min(x => Math.Abs(x.Item1) + Math.Abs(x.Item2))}";
diff --git a/Day4.cs b/Day04.cs
index 60b23b3..f4ddcf9 100644
--- a/Day4.cs
+++ b/Day04.cs
@@ -2,21 +2,19 @@
 
 namespace aoc2019
 {
-    internal sealed class Day4 : Day
+    internal sealed class Day04 : Day
     {
         private readonly int end;
 
         private readonly int start;
 
-        public Day4()
+        public Day04() : base(4, "Secure Container")
         {
             var range = Input.First().Split('-').Select(int.Parse).ToList();
             start = range[0];
             end = range[1];
         }
 
-        public override int DayNumber => 4;
-
         private bool IsValid(int i)
         {
             var prev = 0;
diff --git a/Day5.cs b/Day05.cs
index e599d16..763d0f8 100644
--- a/Day5.cs
+++ b/Day05.cs
@@ -3,19 +3,17 @@ using System.Linq;
 
 namespace aoc2019
 {
-    internal sealed class Day5 : Day
+    internal sealed class Day05 : Day
     {
         private readonly IEnumerable<int> tape;
 
         private int output;
 
-        public Day5()
+        public Day05() : base(5, "Sunny with a Chance of Asteroids")
         {
             tape = Input.First().Split(',').Select(int.Parse);
         }
 
-        public override int DayNumber => 5;
-
         public void RunIntCode(List<int> v, int input)
         {
             var i = 0;
diff --git a/Day6.cs b/Day06.cs
index 3d5fd3a..7ef3d0a 100644
--- a/Day6.cs
+++ b/Day06.cs
@@ -3,17 +3,15 @@ using System.Linq;
 
 namespace aoc2019
 {
-    internal sealed class Day6 : Day
+    internal sealed class Day06 : Day
     {
         private readonly Dictionary<string, string> input;
 
-        public Day6()
+        public Day06() : base(6, "Universal Orbit Map")
         {
             input = Input.ToDictionary(i => i.Split(')')[1], i => i.Split(')')[0]);
         }
 
-        public override int DayNumber => 6;
-
         private List<string> GetParents(string obj)
         {
             var res = new List<string>();
diff --git a/Day7.cs b/Day07.cs
index e600361..f26b8a6 100644
--- a/Day7.cs
+++ b/Day07.cs
@@ -4,18 +4,15 @@ using aoc2019.lib;
 
 namespace aoc2019
 {
-    internal sealed class Day7 : Day
+    internal sealed class Day07 : Day
     {
         private readonly IntCodeVM[] Amplifiers = new IntCodeVM[5];
 
-        public Day7()
+        public Day07() : base(7, "Amplification Circuit")
         {
             for (var i = 0; i < 5; i++) Amplifiers[i] = new IntCodeVM(Input.First());
         }
 
-        public override int DayNumber => 7;
-
-
         protected override string Part1()
         {
             long i, largest = 0;
diff --git a/Day8.cs b/Day08.cs
index cdb3ef6..94cd461 100644
--- a/Day8.cs
+++ b/Day08.cs
@@ -5,17 +5,15 @@ using aoc2019.lib;
 
 namespace aoc2019
 {
-    internal sealed class Day8 : Day
+    internal sealed class Day08 : Day
     {
         private readonly List<List<char>> photo;
 
-        public Day8()
+        public Day08() : base(8, "Space Image Format")
         {
             photo = Input.First().Chunk(25 * 6).Select(s => s.ToList()).ToList();
         }
 
-        public override int DayNumber => 8;
-
         protected override string Part1()
         {
             var l = photo.OrderBy(layer => layer.Count(pixel => pixel == '0')).First();
diff --git a/Day9.cs b/Day09.cs
index a50fdd9..0f45dfd 100644
--- a/Day9.cs
+++ b/Day09.cs
@@ -3,17 +3,15 @@ using aoc2019.lib;
 
 namespace aoc2019
 {
-    internal sealed class Day9 : Day
+    internal sealed class Day09 : Day
     {
         private readonly IntCodeVM vm;
 
-        public Day9()
+        public Day09() : base(9, "Sensor Boost")
         {
             vm = new IntCodeVM(Input.First());
         }
 
-        public override int DayNumber => 9;
-
         protected override string Part1()
         {
             vm.Reset();
diff --git a/Day10.cs b/Day10.cs
index 4cdaa49..376e427 100644
--- a/Day10.cs
+++ b/Day10.cs
@@ -7,11 +7,11 @@ namespace aoc2019
 {
     internal sealed class Day10 : Day
     {
-        private readonly HashSet<(int x, int y)> asteroids = new HashSet<(int x, int y)>();
+        private readonly HashSet<(int x, int y)> asteroids;
         private (int x, int y) best = (x: -1, y: -1);
-        private int bestcansee;
+        private int bestCanSee;
 
-        public Day10()
+        public Day10() : base(10, "Monitoring Station")
         {
             asteroids = Input
                 .Select((r, y) => r.Select((c, x) => (x, y, isAsteroid: c == '#')).ToArray())
@@ -21,26 +21,24 @@ namespace aoc2019
                 .ToHashSet();
         }
 
-        public override int DayNumber => 10;
-
         protected override string Part1()
         {
             foreach (var asteroid in asteroids)
             {
-                var cansee = asteroids
+                var canSee = asteroids
                     .Except(new[] {asteroid})
                     .Select(a => (x: a.x - asteroid.x, y: a.y - asteroid.y))
                     .GroupBy(a => Math.Atan2(a.y, a.x))
                     .Count();
 
-                if (cansee > bestcansee)
+                if (canSee > bestCanSee)
                 {
                     best = asteroid;
-                    bestcansee = cansee;
+                    bestCanSee = canSee;
                 }
             }
 
-            return $"{bestcansee}";
+            return $"{bestCanSee}";
         }
 
         protected override string Part2()
@@ -55,10 +53,10 @@ namespace aoc2019
                 .Where(a => a != best)
                 .Select(a =>
                 {
-                    var xdist = a.x - best.x;
-                    var ydist = a.y - best.y;
-                    var angle = Math.Atan2(xdist, ydist);
-                    return (a.x, a.y, angle, dist: Math.Sqrt(xdist * xdist + ydist * ydist));
+                    var xDist = a.x - best.x;
+                    var yDist = a.y - best.y;
+                    var angle = Math.Atan2(xDist, yDist);
+                    return (a.x, a.y, angle, dist: Math.Sqrt(xDist * xDist + yDist * yDist));
                 })
                 .ToLookup(a => a.angle)
                 .OrderByDescending(a => a.Key)
diff --git a/Day11.cs b/Day11.cs
index 6900d48..e45f6ce 100644
--- a/Day11.cs
+++ b/Day11.cs
@@ -11,13 +11,11 @@ namespace aoc2019
         private Direction heading;
         private long x, y;
 
-        public Day11()
+        public Day11() : base(11, "Space Police")
         {
             vm = new IntCodeVM(Input.First());
         }
 
-        public override int DayNumber => 11;
-
         private void Move()
         {
             switch (heading)
diff --git a/Day12.cs b/Day12.cs
index 7d61b63..312c705 100644
--- a/Day12.cs
+++ b/Day12.cs
@@ -7,10 +7,9 @@ namespace aoc2019
     internal sealed class Day12 : Day
     {
         private readonly List<Position> moons;
-        private readonly List<Position> startingPositions;
         private int step;
 
-        public Day12()
+        public Day12() : base(12, "The N-Body Problem")
         {
             moons = Input
                 .Select(moon =>
@@ -25,12 +24,8 @@ namespace aoc2019
 
             foreach (var moon in moons)
                 moon.SetSiblings(moons);
-
-            startingPositions = moons;
         }
 
-        public override int DayNumber => 12;
-
         public static long LCM(long a, long b)
         {
             return a * b / GCD(a, b);
diff --git a/Day13.cs b/Day13.cs
index 681126d..4dff712 100644
--- a/Day13.cs
+++ b/Day13.cs
@@ -11,14 +11,12 @@ namespace aoc2019
 
         private readonly IntCodeVM vm;
 
-        public Day13()
+        public Day13() : base(13, "Care Package")
         {
             vm = new IntCodeVM(Input.First());
             board = new Dictionary<(int, int), int>();
         }
 
-        public override int DayNumber => 13;
-
         private void UpdateTiles(IEnumerable<long> queue)
         {
             var input = queue.Select(i => (int) i).ToList();
@@ -65,9 +63,9 @@ namespace aoc2019
         {
             vm.Reset();
             vm.memory[0] = 2;
-            var printboard = false;
+            var printBoard = false;
             var gameTicks = 0;
-            if (printboard) Console.Clear();
+            if (printBoard) Console.Clear();
 
             var haltType = IntCodeVM.HaltType.Waiting;
             while (haltType == IntCodeVM.HaltType.Waiting)
@@ -80,7 +78,7 @@ namespace aoc2019
                 vm.AddInput(ball > paddle ? 1 : ball < paddle ? -1 : 0);
 
                 gameTicks++;
-                if (printboard) PrintBoard();
+                if (printBoard) PrintBoard();
             }
 
             return $"after {gameTicks} moves, the score is: {board[(-1, 0)]}";
diff --git a/Day14.cs b/Day14.cs
index 873d74f..a73a858 100644
--- a/Day14.cs
+++ b/Day14.cs
@@ -10,15 +10,13 @@ namespace aoc2019
 
         private Dictionary<string, long> available;
 
-        public Day14()
+        public Day14() : base(14, "Space Stoichiometry")
         {
             reactions = Input
                 .Select(Reaction.Parse)
                 .ToDictionary(r => r.product.Name);
         }
 
-        public override int DayNumber => 14;
-
         private bool Consume(string chem, long quantity)
         {
             if (quantity <= 0)
diff --git a/Day15.cs b/Day15.cs
index f10b547..0b2ca88 100644
--- a/Day15.cs
+++ b/Day15.cs
@@ -10,13 +10,11 @@ namespace aoc2019
         private readonly bool verbose = false;
         private readonly IntCodeVM vm;
 
-        public Day15()
+        public Day15() : base(15, "Oxygen System")
         {
             vm = new IntCodeVM(Input.First());
         }
 
-        public override int DayNumber => 15;
-
         protected override string Part1()
         {
             vm.Reset();
diff --git a/Day16.cs b/Day16.cs
index 6c90520..e2958bd 100644
--- a/Day16.cs
+++ b/Day16.cs
@@ -10,13 +10,11 @@ namespace aoc2019
         private static readonly int[] BasePattern = {0, 1, 0, -1};
         private readonly int[] initialList;
 
-        public Day16()
+        public Day16() : base(16, "Flawed Frequency Transmission")
         {
             initialList = Input.First().Select(c => int.Parse($"{c}")).ToArray();
         }
 
-        public override int DayNumber => 16;
-
         protected override string Part1()
         {
             const int phaseCount = 100;
diff --git a/Day17.cs b/Day17.cs
index f9da08a..2438ee9 100644
--- a/Day17.cs
+++ b/Day17.cs
@@ -1,5 +1,4 @@
 using System;
-using System.ComponentModel;
 using System.Linq;
 using System.Text;
 using aoc2019.lib;
@@ -12,13 +11,11 @@ namespace aoc2019
 
         private readonly IntCodeVM vm;
 
-        public Day17()
+        public Day17() : base(17, "Set and Forget")
         {
             vm = new IntCodeVM(Input.First());
         }
 
-        public override int DayNumber => 17;
-
         protected override string Part1()
         {
             vm.Reset();
@@ -44,13 +41,13 @@ namespace aoc2019
 
         protected override string Part2()
         {
-            vm.Reset();
-            vm.memory[0] = 2;
-            var halt = IntCodeVM.HaltType.Waiting;
-            while (halt == IntCodeVM.HaltType.Waiting)
-            {
-                halt = vm.Run();
-            }
+            //vm.Reset();
+            //vm.memory[0] = 2;
+            //var halt = IntCodeVM.HaltType.Waiting;
+            //while (halt == IntCodeVM.HaltType.Waiting)
+            //{
+            //    halt = vm.Run();
+            //}
             return "";
         }
     }
diff --git a/input/day1.in b/input/day01.in
index fb4481d..fb4481d 100644
--- a/input/day1.in
+++ b/input/day01.in
diff --git a/input/day2.in b/input/day02.in
index c3ac93e..c3ac93e 100644
--- a/input/day2.in
+++ b/input/day02.in
diff --git a/input/day3.in b/input/day03.in
index d8a6b32..d8a6b32 100644
--- a/input/day3.in
+++ b/input/day03.in
diff --git a/input/day4.in b/input/day04.in
index 6190bcb..6190bcb 100644
--- a/input/day4.in
+++ b/input/day04.in
diff --git a/input/day5.in b/input/day05.in
index 4d8fafe..4d8fafe 100644
--- a/input/day5.in
+++ b/input/day05.in
diff --git a/input/day6.in b/input/day06.in
index 2868a3f..2868a3f 100644
--- a/input/day6.in
+++ b/input/day06.in
diff --git a/input/day7.in b/input/day07.in
index 0128ef0..0128ef0 100644
--- a/input/day7.in
+++ b/input/day07.in
diff --git a/input/day8.in b/input/day08.in
index 9100e5b..9100e5b 100644
--- a/input/day8.in
+++ b/input/day08.in
diff --git a/input/day9.in b/input/day09.in
index 0e05ddc..0e05ddc 100644
--- a/input/day9.in
+++ b/input/day09.in
diff --git a/lib/Extensions.cs b/lib/Extensions.cs
index a1ffbcc..eb8c3cb 100644
--- a/lib/Extensions.cs
+++ b/lib/Extensions.cs
@@ -1,4 +1,5 @@
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Linq;
 
 namespace aoc2019.lib
@@ -28,5 +29,20 @@ namespace aoc2019.lib
                 foreach (var item in sequence)
                     yield return item;
         }
+
+        /// <summary>
+        ///     increased accuracy for stopwatch based on frequency.
+        ///     <see
+        ///         href="http://geekswithblogs.net/BlackRabbitCoder/archive/2012/01/12/c.net-little-pitfalls-stopwatch-ticks-are-not-timespan-ticks.aspx">
+        ///         blog
+        ///         details here
+        ///     </see>
+        /// </summary>
+        /// <param name="stopwatch"></param>
+        /// <returns></returns>
+        public static double ScaleMilliseconds(this Stopwatch stopwatch)
+        {
+            return 1_000 * stopwatch.ElapsedTicks / (double) Stopwatch.Frequency;
+        }
     }
 }
\ No newline at end of file