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
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;
[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");
}
}
}

View File

@ -1,20 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<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>
<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>
<ProjectReference Include="..\aoc2019\aoc2019.csproj"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\aoc2019\aoc2019.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="System.Diagnostics"/>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting"/>
</ItemGroup>
</Project>

View File

@ -16,7 +16,7 @@ public abstract class Day
protected virtual IEnumerable<string> 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();
}
}

View File

@ -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<IntCodeVM>(Amplifiers);
var vms = new Queue<IntCodeVM>(amplifiers);
while (vms.Count > 0)
{
var vm = vms.Dequeue();

View File

@ -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(",")}";
}
}
}

View File

@ -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)]}";
}
}
}

View File

@ -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

View File

@ -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 "";
}
}
}

View File

@ -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;

View File

@ -8,10 +8,12 @@ public class IntCodeVM
Waiting
}
private readonly Queue<long> input;
public readonly Queue<long> Output;
private readonly long[] program;
private long i;
public Queue<long> 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<long>();
output = new Queue<long>();
Output = new Queue<long>();
}
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:

View File

@ -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();
}