diff --git a/README.md b/README.md index 0e6fdc8..f73ae59 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,5 @@ some advent of code solutions for 2019 starting out with c#, we'll see how it goes this year -using .net core 3.0, run all solutions with `dotnet run` +using ~~.net core 3.0~~ .net 6.0, run all solutions with `dotnet run` diff --git a/aoc2019.test/Tests.cs b/aoc2019.test/Tests.cs index 22566e0..4b4499f 100644 --- a/aoc2019.test/Tests.cs +++ b/aoc2019.test/Tests.cs @@ -1,7 +1,3 @@ -using System; -using System.Diagnostics; -using Microsoft.VisualStudio.TestTools.UnitTesting; - namespace aoc2019.test; [TestClass] @@ -22,29 +18,36 @@ public class Tests [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(Day13), "361", "after 7133 moves, the score is: 17590")] // this one takes a LONG time to run [DataRow(typeof(Day14), "397771", "3126714")] [DataRow(typeof(Day15), "280", "400")] [DataRow(typeof(Day16), "90744714", "82994322")] [DataRow(typeof(Day17), "2804", "")] + //[DataRow(typeof(Day18), "", "")] [DataRow(typeof(Day19), "114", "10671712")] - [DataRow(typeof(Day21), "", "")] + //[DataRow(typeof(Day20), "", "")] + //[DataRow(typeof(Day21), "", "")] + //[DataRow(typeof(Day22), "", "")] [DataRow(typeof(Day23), "23626", "19019")] + //[DataRow(typeof(Day24), "", "")] + //[DataRow(typeof(Day25), "", "")] public void TestAllDays(Type dayType, string part1, string part2) { - // create day instance var s = Stopwatch.StartNew(); - var day = (Day)Activator.CreateInstance(dayType); + var day = Activator.CreateInstance(dayType) as Day; s.Stop(); - Assert.IsNotNull(day, "failed to create day object"); - Console.WriteLine($"{s.ScaleMilliseconds()}ms elapsed in constructor"); + Assert.IsNotNull(day, "failed to instantiate day object"); + Assert.IsTrue(File.Exists(day!.FileName)); + Console.Write($"Day {day.DayNumber,2}: {day.PuzzleName,-15} "); + Console.WriteLine($"{s.ScaleMilliseconds()} ms elapsed in constructor"); // part 1 s.Reset(); s.Start(); var part1Actual = day.Part1(); s.Stop(); - Console.WriteLine($"{s.ScaleMilliseconds()}ms elapsed in part1"); + Console.Write($"Part 1: {part1Actual,-15} "); + Console.WriteLine($"{s.ScaleMilliseconds()} ms elapsed"); Assert.AreEqual(part1, part1Actual, $"Incorrect answer for Day {day.DayNumber} Part1"); // part 2 @@ -52,7 +55,8 @@ public class Tests s.Start(); var part2Actual = day.Part2(); s.Stop(); - Console.WriteLine($"{s.ScaleMilliseconds()}ms elapsed in part2"); + Console.Write($"Part 2: {part2Actual,-15} "); + Console.WriteLine($"{s.ScaleMilliseconds()} ms elapsed"); Assert.AreEqual(part2, part2Actual, $"Incorrect answer for Day {day.DayNumber} Part2"); } -} +} \ No newline at end of file diff --git a/aoc2019.test/aoc2019.test.csproj b/aoc2019.test/aoc2019.test.csproj index 656cbb3..b7f3ae7 100644 --- a/aoc2019.test/aoc2019.test.csproj +++ b/aoc2019.test/aoc2019.test.csproj @@ -1,20 +1,26 @@ - - net6.0 + + net6.0 + enable + enable + false + - false - + + + + + + - - - - - - + + + - - - + + + + diff --git a/aoc2019/Day.cs b/aoc2019/Day.cs index 6bff284..1c09a84 100644 --- a/aoc2019/Day.cs +++ b/aoc2019/Day.cs @@ -16,7 +16,7 @@ public abstract class Day protected virtual IEnumerable Input => File.ReadLines(FileName); - protected string FileName => + public string FileName => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"input/day{DayNumber,2:00}.in"); public void AllParts(bool verbose = true) @@ -41,4 +41,4 @@ public abstract class Day public abstract string Part1(); public abstract string Part2(); -} +} \ No newline at end of file diff --git a/aoc2019/Day07.cs b/aoc2019/Day07.cs index 4a45842..8beedb2 100644 --- a/aoc2019/Day07.cs +++ b/aoc2019/Day07.cs @@ -2,21 +2,21 @@ namespace aoc2019; public sealed class Day07 : Day { - private readonly IntCodeVM[] Amplifiers = new IntCodeVM[5]; + private readonly IntCodeVM[] amplifiers = new IntCodeVM[5]; public Day07() : base(7, "Amplification Circuit") { - for (var i = 0; i < 5; i++) Amplifiers[i] = new IntCodeVM(Input.First()); + for (var i = 0; i < 5; i++) amplifiers[i] = new IntCodeVM(Input.First()); } public override string Part1() { - long i, largest = 0; + var largest = 0L; foreach (var phaseSeq in Enumerable.Range(0, 5).Permute()) { - i = 0; - foreach (var (vm, phase) in Amplifiers.Zip(phaseSeq)) + var i = 0L; + foreach (var (vm, phase) in amplifiers.Zip(phaseSeq)) { vm.Reset(); vm.Run(phase, i); @@ -32,18 +32,18 @@ public sealed class Day07 : Day public override string Part2() { - long i, largest = 0; + var largest = 0L; foreach (var phaseSeq in Enumerable.Range(5, 5).Permute()) { - i = 0; - foreach (var (vm, phase) in Amplifiers.Zip(phaseSeq)) + var i = 0L; + foreach (var (vm, phase) in amplifiers.Zip(phaseSeq)) { vm.Reset(); vm.AddInput(phase); } - var vms = new Queue(Amplifiers); + var vms = new Queue(amplifiers); while (vms.Count > 0) { var vm = vms.Dequeue(); diff --git a/aoc2019/Day09.cs b/aoc2019/Day09.cs index 9e57ab2..951e7eb 100644 --- a/aoc2019/Day09.cs +++ b/aoc2019/Day09.cs @@ -13,13 +13,13 @@ public sealed class Day09 : Day { vm.Reset(); vm.Run(1); - return $"{vm.output.ToDelimitedString(",")}"; + return $"{vm.Output.ToDelimitedString(",")}"; } public override string Part2() { vm.Reset(); vm.Run(2); - return $"{vm.output.ToDelimitedString(",")}"; + return $"{vm.Output.ToDelimitedString(",")}"; } -} +} \ No newline at end of file diff --git a/aoc2019/Day13.cs b/aoc2019/Day13.cs index 11af0a3..35e2dd7 100644 --- a/aoc2019/Day13.cs +++ b/aoc2019/Day13.cs @@ -51,13 +51,13 @@ public sealed class Day13 : Day { vm.Reset(); vm.Run(); - return $"{vm.output.Where((v, i) => (i + 1) % 3 == 0 && v == 2).Count()}"; + return $"{vm.Output.Where((v, i) => (i + 1) % 3 == 0 && v == 2).Count()}"; } public override string Part2() { vm.Reset(); - vm.memory[0] = 2; + vm.Memory[0] = 2; var printBoard = false; var gameTicks = 0; if (printBoard) Console.Clear(); @@ -66,7 +66,7 @@ public sealed class Day13 : Day while (haltType == IntCodeVM.HaltType.Waiting) { haltType = vm.Run(); - UpdateTiles(vm.output); + UpdateTiles(vm.Output); var (ball, _) = board.First(t => t.Value == 4).Key; var (paddle, _) = board.First(t => t.Value == 3).Key; @@ -78,4 +78,4 @@ public sealed class Day13 : Day return $"after {gameTicks} moves, the score is: {board[(-1, 0)]}"; } -} +} \ No newline at end of file diff --git a/aoc2019/Day15.cs b/aoc2019/Day15.cs index 2c080a7..f68533d 100644 --- a/aoc2019/Day15.cs +++ b/aoc2019/Day15.cs @@ -49,7 +49,7 @@ public sealed class Day15 : Day currentLocation = vm.Result switch { Location.Empty or Location.System => Location.GetLocation(currentLocation.Neighbor(direction)), - _ => throw new Exception($"Unknown or unexpected response for previous room: {vm.Result}"), + _ => throw new Exception($"Unknown or unexpected response for previous room: {vm.Result}") }; } else diff --git a/aoc2019/Day17.cs b/aoc2019/Day17.cs index 376a80f..8d97693 100644 --- a/aoc2019/Day17.cs +++ b/aoc2019/Day17.cs @@ -2,7 +2,6 @@ namespace aoc2019; public sealed class Day17 : Day { - private readonly bool Verbose = false; private readonly IntCodeVM vm; public Day17() : base(17, "Set and Forget") @@ -15,20 +14,20 @@ public sealed class Day17 : Day vm.Reset(); vm.Run(); var sb = new StringBuilder(); - while (vm.output.Any()) + while (vm.Output.Any()) sb.Append((char)vm.Result); - if (Verbose) Console.Write(sb); + // Console.Write(sb); var grid = sb.ToString().Trim().Split().Select(s => s.ToCharArray()).ToArray(); var sum = 0; for (var y = 1; y < grid.Length - 1; y++) - for (var x = 1; x < grid[y].Length - 1; x++) - if (grid[y][x] == '#' && - grid[y - 1][x] == '#' && - grid[y + 1][x] == '#' && - grid[y][x - 1] == '#' && - grid[y][x + 1] == '#') - sum += x * y; + for (var x = 1; x < grid[y].Length - 1; x++) + if (grid[y][x] == '#' && + grid[y - 1][x] == '#' && + grid[y + 1][x] == '#' && + grid[y][x - 1] == '#' && + grid[y][x + 1] == '#') + sum += x * y; return $"{sum}"; } @@ -44,4 +43,4 @@ public sealed class Day17 : Day //} return ""; } -} +} \ No newline at end of file diff --git a/aoc2019/Day23.cs b/aoc2019/Day23.cs index 29da6a7..43f4c1d 100644 --- a/aoc2019/Day23.cs +++ b/aoc2019/Day23.cs @@ -9,7 +9,7 @@ public sealed class Day23 : Day public override string Part1() { var vms = Enumerable.Range(0, 50) - .Select((s, i) => + .Select((_, i) => { var vm = new IntCodeVM(Input.First()); vm.Run(i); @@ -19,7 +19,7 @@ public sealed class Day23 : Day while (true) foreach (var vm in vms) { - while (vm.output.Count > 0) + while (vm.Output.Any()) { var destination = (int)vm.Result; var x = vm.Result; @@ -37,7 +37,7 @@ public sealed class Day23 : Day public override string Part2() { var vms = Enumerable.Range(0, 50) - .Select((s, i) => + .Select((_, i) => { var vm = new IntCodeVM(Input.First()); vm.Run(i); @@ -52,7 +52,7 @@ public sealed class Day23 : Day foreach (var vm in vms) { var isIdle = true; - while (vm.output.Count > 0) + while (vm.Output.Any()) { var destination = (int)vm.Result; var x = vm.Result; diff --git a/aoc2019/IntCodeVM.cs b/aoc2019/IntCodeVM.cs index d6f1214..74e433d 100644 --- a/aoc2019/IntCodeVM.cs +++ b/aoc2019/IntCodeVM.cs @@ -8,10 +8,12 @@ public class IntCodeVM Waiting } + private readonly Queue input; + public readonly Queue Output; + private readonly long[] program; private long i; - public Queue input, output; - public long[] memory; + public long[] Memory; private long relativeBase; public IntCodeVM(string tape) @@ -19,20 +21,20 @@ public class IntCodeVM i = 0; relativeBase = 0; program = tape.Split(',').Select(long.Parse).ToArray(); - memory = program; + Memory = program; input = new Queue(); - output = new Queue(); + Output = new Queue(); } - public long Result => output.Dequeue(); + public long Result => Output.Dequeue(); public void Reset() { i = 0; relativeBase = 0; - memory = program; + Memory = program; input.Clear(); - output.Clear(); + Output.Clear(); } public void AddInput(params long[] values) @@ -47,15 +49,15 @@ public class IntCodeVM private long MemGet(long addr) { - return addr < memory.Length ? memory[addr] : 0; + return addr < Memory.Length ? Memory[addr] : 0; } private void MemSet(long addr, long value) { if (addr < 0) addr = 0; - if (addr >= memory.Length) - Array.Resize(ref memory, (int)addr + 1); - memory[addr] = value; + if (addr >= Memory.Length) + Array.Resize(ref Memory, (int)addr + 1); + Memory[addr] = value; } private long Mode(long idx) @@ -74,7 +76,7 @@ public class IntCodeVM 0 => MemGet(param), 1 => param, 2 => MemGet(relativeBase + param), - _ => throw new Exception("invalid parameter mode"), + _ => throw new Exception("invalid parameter mode") }; } @@ -102,7 +104,7 @@ public class IntCodeVM public HaltType Run() { - while (i < memory.Length) + while (i < Memory.Length) { var op = MemGet(i) % 100; switch (op) @@ -122,7 +124,7 @@ public class IntCodeVM i += 2; break; case 4: - output.Enqueue(Get(1)); + Output.Enqueue(Get(1)); i += 2; break; case 5: diff --git a/aoc2019/Program.cs b/aoc2019/Program.cs index ca7eec7..68b0636 100644 --- a/aoc2019/Program.cs +++ b/aoc2019/Program.cs @@ -1,14 +1,9 @@ using System.Reflection; +using aoc2019; -namespace aoc2019; - -internal static class Program -{ - private static void Main(string[] args) - { - var days = - Assembly.GetExecutingAssembly().GetTypes() - .Where(t => t.BaseType == typeof(Day)) +var days = + Assembly.GetExecutingAssembly().GetTypes() + .Where(t => t.BaseType == typeof(Day)) .Select(t => Activator.CreateInstance(t) as Day) .OrderBy(d => d?.DayNumber); @@ -22,8 +17,6 @@ internal static class Program Console.WriteLine($"{dayNum} invalid or not yet implemented"); } else - { - foreach (var d in days) d?.AllParts(); - } - } -} +{ + foreach (var d in days) d?.AllParts(); +} \ No newline at end of file