tidy up test output, apply formatting/naming
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Ben Harris 2021-12-01 14:31:42 -05:00
parent ffb4a8e7a4
commit 212278cc64
12 changed files with 93 additions and 89 deletions

View File

@ -6,5 +6,5 @@ some advent of code solutions for 2019
starting out with c#, we'll see how it goes this year 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`

View File

@ -1,7 +1,3 @@
using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace aoc2019.test; namespace aoc2019.test;
[TestClass] [TestClass]
@ -22,29 +18,36 @@ public class Tests
[DataRow(typeof(Day11), "2054", [DataRow(typeof(Day11), "2054",
"\n # # ### #### #### ## ## # # ### \n # # # # # # # # # # # # # \n ## # # # ### # # # #### ### \n # # ### # # #### # # # # # \n # # # # # # # # # # # # # # \n # # # # #### #### # # ## # # ### ")] "\n # # ### #### #### ## ## # # ### \n # # # # # # # # # # # # # \n ## # # # ### # # # #### ### \n # # ### # # #### # # # # # \n # # # # # # # # # # # # # # \n # # # # #### #### # # ## # # ### ")]
[DataRow(typeof(Day12), "10635", "583523031727256")] [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(Day14), "397771", "3126714")]
[DataRow(typeof(Day15), "280", "400")] [DataRow(typeof(Day15), "280", "400")]
[DataRow(typeof(Day16), "90744714", "82994322")] [DataRow(typeof(Day16), "90744714", "82994322")]
[DataRow(typeof(Day17), "2804", "")] [DataRow(typeof(Day17), "2804", "")]
//[DataRow(typeof(Day18), "", "")]
[DataRow(typeof(Day19), "114", "10671712")] [DataRow(typeof(Day19), "114", "10671712")]
[DataRow(typeof(Day21), "", "")] //[DataRow(typeof(Day20), "", "")]
//[DataRow(typeof(Day21), "", "")]
//[DataRow(typeof(Day22), "", "")]
[DataRow(typeof(Day23), "23626", "19019")] [DataRow(typeof(Day23), "23626", "19019")]
//[DataRow(typeof(Day24), "", "")]
//[DataRow(typeof(Day25), "", "")]
public void TestAllDays(Type dayType, string part1, string part2) public void TestAllDays(Type dayType, string part1, string part2)
{ {
// create day instance
var s = Stopwatch.StartNew(); var s = Stopwatch.StartNew();
var day = (Day)Activator.CreateInstance(dayType); var day = Activator.CreateInstance(dayType) as Day;
s.Stop(); s.Stop();
Assert.IsNotNull(day, "failed to create day object"); Assert.IsNotNull(day, "failed to instantiate day object");
Console.WriteLine($"{s.ScaleMilliseconds()}ms elapsed in constructor"); 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 // part 1
s.Reset(); s.Reset();
s.Start(); s.Start();
var part1Actual = day.Part1(); var part1Actual = day.Part1();
s.Stop(); 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"); Assert.AreEqual(part1, part1Actual, $"Incorrect answer for Day {day.DayNumber} Part1");
// part 2 // part 2
@ -52,7 +55,8 @@ public class Tests
s.Start(); s.Start();
var part2Actual = day.Part2(); var part2Actual = day.Part2();
s.Stop(); 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"); Assert.AreEqual(part2, part2Actual, $"Incorrect answer for Day {day.DayNumber} Part2");
} }
} }

View File

@ -1,20 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<IsPackable>false</IsPackable> <ItemGroup>
</PropertyGroup> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1"/>
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1"/>
<PackageReference Include="MSTest.TestFramework" Version="2.1.1"/>
<PackageReference Include="coverlet.collector" Version="1.3.0"/>
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" /> <ProjectReference Include="..\aoc2019\aoc2019.csproj"/>
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" /> </ItemGroup>
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
<PackageReference Include="coverlet.collector" Version="1.3.0" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\aoc2019\aoc2019.csproj" /> <Using Include="System.Diagnostics"/>
</ItemGroup> <Using Include="Microsoft.VisualStudio.TestTools.UnitTesting"/>
</ItemGroup>
</Project> </Project>

View File

@ -16,7 +16,7 @@ public abstract class Day
protected virtual IEnumerable<string> Input => protected virtual IEnumerable<string> Input =>
File.ReadLines(FileName); File.ReadLines(FileName);
protected string FileName => public string FileName =>
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"input/day{DayNumber,2:00}.in"); Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"input/day{DayNumber,2:00}.in");
public void AllParts(bool verbose = true) public void AllParts(bool verbose = true)
@ -41,4 +41,4 @@ public abstract class Day
public abstract string Part1(); public abstract string Part1();
public abstract string Part2(); public abstract string Part2();
} }

View File

@ -2,21 +2,21 @@ namespace aoc2019;
public sealed class Day07 : Day 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") 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() public override string Part1()
{ {
long i, largest = 0; var largest = 0L;
foreach (var phaseSeq in Enumerable.Range(0, 5).Permute()) foreach (var phaseSeq in Enumerable.Range(0, 5).Permute())
{ {
i = 0; var i = 0L;
foreach (var (vm, phase) in Amplifiers.Zip(phaseSeq)) foreach (var (vm, phase) in amplifiers.Zip(phaseSeq))
{ {
vm.Reset(); vm.Reset();
vm.Run(phase, i); vm.Run(phase, i);
@ -32,18 +32,18 @@ public sealed class Day07 : Day
public override string Part2() public override string Part2()
{ {
long i, largest = 0; var largest = 0L;
foreach (var phaseSeq in Enumerable.Range(5, 5).Permute()) foreach (var phaseSeq in Enumerable.Range(5, 5).Permute())
{ {
i = 0; var i = 0L;
foreach (var (vm, phase) in Amplifiers.Zip(phaseSeq)) foreach (var (vm, phase) in amplifiers.Zip(phaseSeq))
{ {
vm.Reset(); vm.Reset();
vm.AddInput(phase); vm.AddInput(phase);
} }
var vms = new Queue<IntCodeVM>(Amplifiers); var vms = new Queue<IntCodeVM>(amplifiers);
while (vms.Count > 0) while (vms.Count > 0)
{ {
var vm = vms.Dequeue(); var vm = vms.Dequeue();

View File

@ -13,13 +13,13 @@ public sealed class Day09 : Day
{ {
vm.Reset(); vm.Reset();
vm.Run(1); vm.Run(1);
return $"{vm.output.ToDelimitedString(",")}"; return $"{vm.Output.ToDelimitedString(",")}";
} }
public override string Part2() public override string Part2()
{ {
vm.Reset(); vm.Reset();
vm.Run(2); vm.Run(2);
return $"{vm.output.ToDelimitedString(",")}"; return $"{vm.Output.ToDelimitedString(",")}";
} }
} }

View File

@ -51,13 +51,13 @@ public sealed class Day13 : Day
{ {
vm.Reset(); vm.Reset();
vm.Run(); 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() public override string Part2()
{ {
vm.Reset(); vm.Reset();
vm.memory[0] = 2; vm.Memory[0] = 2;
var printBoard = false; var printBoard = false;
var gameTicks = 0; var gameTicks = 0;
if (printBoard) Console.Clear(); if (printBoard) Console.Clear();
@ -66,7 +66,7 @@ public sealed class Day13 : Day
while (haltType == IntCodeVM.HaltType.Waiting) while (haltType == IntCodeVM.HaltType.Waiting)
{ {
haltType = vm.Run(); haltType = vm.Run();
UpdateTiles(vm.output); UpdateTiles(vm.Output);
var (ball, _) = board.First(t => t.Value == 4).Key; var (ball, _) = board.First(t => t.Value == 4).Key;
var (paddle, _) = board.First(t => t.Value == 3).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)]}"; return $"after {gameTicks} moves, the score is: {board[(-1, 0)]}";
} }
} }

View File

@ -49,7 +49,7 @@ public sealed class Day15 : Day
currentLocation = vm.Result switch currentLocation = vm.Result switch
{ {
Location.Empty or Location.System => Location.GetLocation(currentLocation.Neighbor(direction)), 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 else

View File

@ -2,7 +2,6 @@ namespace aoc2019;
public sealed class Day17 : Day public sealed class Day17 : Day
{ {
private readonly bool Verbose = false;
private readonly IntCodeVM vm; private readonly IntCodeVM vm;
public Day17() : base(17, "Set and Forget") public Day17() : base(17, "Set and Forget")
@ -15,20 +14,20 @@ public sealed class Day17 : Day
vm.Reset(); vm.Reset();
vm.Run(); vm.Run();
var sb = new StringBuilder(); var sb = new StringBuilder();
while (vm.output.Any()) while (vm.Output.Any())
sb.Append((char)vm.Result); 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 grid = sb.ToString().Trim().Split().Select(s => s.ToCharArray()).ToArray();
var sum = 0; var sum = 0;
for (var y = 1; y < grid.Length - 1; y++) for (var y = 1; y < grid.Length - 1; y++)
for (var x = 1; x < grid[y].Length - 1; x++) for (var x = 1; x < grid[y].Length - 1; x++)
if (grid[y][x] == '#' && if (grid[y][x] == '#' &&
grid[y - 1][x] == '#' && grid[y - 1][x] == '#' &&
grid[y + 1][x] == '#' && grid[y + 1][x] == '#' &&
grid[y][x - 1] == '#' && grid[y][x - 1] == '#' &&
grid[y][x + 1] == '#') grid[y][x + 1] == '#')
sum += x * y; sum += x * y;
return $"{sum}"; return $"{sum}";
} }
@ -44,4 +43,4 @@ public sealed class Day17 : Day
//} //}
return ""; return "";
} }
} }

View File

@ -9,7 +9,7 @@ public sealed class Day23 : Day
public override string Part1() public override string Part1()
{ {
var vms = Enumerable.Range(0, 50) var vms = Enumerable.Range(0, 50)
.Select((s, i) => .Select((_, i) =>
{ {
var vm = new IntCodeVM(Input.First()); var vm = new IntCodeVM(Input.First());
vm.Run(i); vm.Run(i);
@ -19,7 +19,7 @@ public sealed class Day23 : Day
while (true) while (true)
foreach (var vm in vms) foreach (var vm in vms)
{ {
while (vm.output.Count > 0) while (vm.Output.Any())
{ {
var destination = (int)vm.Result; var destination = (int)vm.Result;
var x = vm.Result; var x = vm.Result;
@ -37,7 +37,7 @@ public sealed class Day23 : Day
public override string Part2() public override string Part2()
{ {
var vms = Enumerable.Range(0, 50) var vms = Enumerable.Range(0, 50)
.Select((s, i) => .Select((_, i) =>
{ {
var vm = new IntCodeVM(Input.First()); var vm = new IntCodeVM(Input.First());
vm.Run(i); vm.Run(i);
@ -52,7 +52,7 @@ public sealed class Day23 : Day
foreach (var vm in vms) foreach (var vm in vms)
{ {
var isIdle = true; var isIdle = true;
while (vm.output.Count > 0) while (vm.Output.Any())
{ {
var destination = (int)vm.Result; var destination = (int)vm.Result;
var x = vm.Result; var x = vm.Result;

View File

@ -8,10 +8,12 @@ public class IntCodeVM
Waiting Waiting
} }
private readonly Queue<long> input;
public readonly Queue<long> Output;
private readonly long[] program; private readonly long[] program;
private long i; private long i;
public Queue<long> input, output; public long[] Memory;
public long[] memory;
private long relativeBase; private long relativeBase;
public IntCodeVM(string tape) public IntCodeVM(string tape)
@ -19,20 +21,20 @@ public class IntCodeVM
i = 0; i = 0;
relativeBase = 0; relativeBase = 0;
program = tape.Split(',').Select(long.Parse).ToArray(); program = tape.Split(',').Select(long.Parse).ToArray();
memory = program; Memory = program;
input = new Queue<long>(); input = new Queue<long>();
output = new Queue<long>(); Output = new Queue<long>();
} }
public long Result => output.Dequeue(); public long Result => Output.Dequeue();
public void Reset() public void Reset()
{ {
i = 0; i = 0;
relativeBase = 0; relativeBase = 0;
memory = program; Memory = program;
input.Clear(); input.Clear();
output.Clear(); Output.Clear();
} }
public void AddInput(params long[] values) public void AddInput(params long[] values)
@ -47,15 +49,15 @@ public class IntCodeVM
private long MemGet(long addr) 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) private void MemSet(long addr, long value)
{ {
if (addr < 0) addr = 0; if (addr < 0) addr = 0;
if (addr >= memory.Length) if (addr >= Memory.Length)
Array.Resize(ref memory, (int)addr + 1); Array.Resize(ref Memory, (int)addr + 1);
memory[addr] = value; Memory[addr] = value;
} }
private long Mode(long idx) private long Mode(long idx)
@ -74,7 +76,7 @@ public class IntCodeVM
0 => MemGet(param), 0 => MemGet(param),
1 => param, 1 => param,
2 => MemGet(relativeBase + 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() public HaltType Run()
{ {
while (i < memory.Length) while (i < Memory.Length)
{ {
var op = MemGet(i) % 100; var op = MemGet(i) % 100;
switch (op) switch (op)
@ -122,7 +124,7 @@ public class IntCodeVM
i += 2; i += 2;
break; break;
case 4: case 4:
output.Enqueue(Get(1)); Output.Enqueue(Get(1));
i += 2; i += 2;
break; break;
case 5: case 5:

View File

@ -1,14 +1,9 @@
using System.Reflection; using System.Reflection;
using aoc2019;
namespace aoc2019; var days =
Assembly.GetExecutingAssembly().GetTypes()
internal static class Program .Where(t => t.BaseType == typeof(Day))
{
private static void Main(string[] args)
{
var days =
Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.BaseType == typeof(Day))
.Select(t => Activator.CreateInstance(t) as Day) .Select(t => Activator.CreateInstance(t) as Day)
.OrderBy(d => d?.DayNumber); .OrderBy(d => d?.DayNumber);
@ -22,8 +17,6 @@ internal static class Program
Console.WriteLine($"{dayNum} invalid or not yet implemented"); Console.WriteLine($"{dayNum} invalid or not yet implemented");
} }
else else
{ {
foreach (var d in days) d?.AllParts(); foreach (var d in days) d?.AllParts();
} }
}
}