move project to subdirectory and add unit testing
continuous-integration/drone/push Build is failing Details

day 13 is removed from the test so it doesn't take 4 years
This commit is contained in:
Ben Harris 2020-12-16 18:06:36 -05:00
parent 837527d487
commit cb10768fa1
52 changed files with 190 additions and 107 deletions

View File

@ -3,7 +3,8 @@ kind: pipeline
name: run
steps:
- name: run
image: mcr.microsoft.com/dotnet/sdk:latest
commands:
- dotnet run
- name: run
image: mcr.microsoft.com/dotnet/sdk:latest
commands:
- dotnet test
- dotnet run --project aoc2019/aoc2019.csproj

View File

@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29519.181
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "aoc2019", "aoc2019.csproj", "{0F1DADD9-2DC3-4035-BE2A-1CC9BBB623A9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "aoc2019", "aoc2019\aoc2019.csproj", "{D99F8F27-0CF3-4220-95F5-B8F92D02F17D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoc2019.test", "aoc2019.test\aoc2019.test.csproj", "{D168189E-28FC-440F-B64C-02C6D9FBBEE0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -11,10 +13,14 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0F1DADD9-2DC3-4035-BE2A-1CC9BBB623A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0F1DADD9-2DC3-4035-BE2A-1CC9BBB623A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F1DADD9-2DC3-4035-BE2A-1CC9BBB623A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F1DADD9-2DC3-4035-BE2A-1CC9BBB623A9}.Release|Any CPU.Build.0 = Release|Any CPU
{D99F8F27-0CF3-4220-95F5-B8F92D02F17D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D99F8F27-0CF3-4220-95F5-B8F92D02F17D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D99F8F27-0CF3-4220-95F5-B8F92D02F17D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D99F8F27-0CF3-4220-95F5-B8F92D02F17D}.Release|Any CPU.Build.0 = Release|Any CPU
{D168189E-28FC-440F-B64C-02C6D9FBBEE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D168189E-28FC-440F-B64C-02C6D9FBBEE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D168189E-28FC-440F-B64C-02C6D9FBBEE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D168189E-28FC-440F-B64C-02C6D9FBBEE0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

56
aoc2019.test/Tests.cs Normal file
View File

@ -0,0 +1,56 @@
using System;
using System.Diagnostics;
using aoc2019;
using aoc2019.lib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace aoc2019.test
{
[TestClass]
public class Tests
{
[DataTestMethod]
[DataRow(typeof(Day01), "3394106", "5088280")]
[DataRow(typeof(Day02), "3085697", "9425")]
[DataRow(typeof(Day03), "1195", "91518")]
[DataRow(typeof(Day04), "1079", "699")]
[DataRow(typeof(Day05), "7692125", "14340395")]
[DataRow(typeof(Day06), "145250", "274")]
[DataRow(typeof(Day07), "19650", "35961106")]
[DataRow(typeof(Day08), "2413", "xxx xx xxx xxxx xxx \r\nx x x x x x x x x \r\nxxx x x x x xxx \r\nx x x xxx x x x \r\nx x x x x x x x \r\nxxx xx x xxxx xxx ")]
[DataRow(typeof(Day09), "3409270027", "82760")]
[DataRow(typeof(Day10), "260", "608")]
[DataRow(typeof(Day11), "2054", " # # ### #### #### ## ## # # ### \r\n # # # # # # # # # # # # # \r\n ## # # # ### # # # #### ### \r\n # # ### # # #### # # # # # \r\n # # # # # # # # # # # # # # \r\n # # # # #### #### # # ## # # ### ")]
[DataRow(typeof(Day12), "10635", "583523031727256")]
[DataRow(typeof(Day13), "361", "after 7133 moves, the score is: 17590")]
[DataRow(typeof(Day14), "397771", "3126714")]
[DataRow(typeof(Day15), "280", "400")]
[DataRow(typeof(Day16), "90744714", "82994322")]
[DataRow(typeof(Day17), "2804", "")]
public void TestAllDays(Type dayType, string part1, string part2)
{
// create day instance
var s = Stopwatch.StartNew();
var day = (Day) Activator.CreateInstance(dayType);
s.Stop();
Assert.IsNotNull(day, "failed to create day object");
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");
Assert.AreEqual(part1, part1Actual, $"Incorrect answer for Day {day.DayNumber} Part1");
// part 2
s.Reset();
s.Start();
var part2Actual = day.Part2();
s.Stop();
Console.WriteLine($"{s.ScaleMilliseconds()}ms elapsed in part2");
Assert.AreEqual(part2, part2Actual, $"Incorrect answer for Day {day.DayNumber} Part2");
}
}
}

View File

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<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>
<ProjectReference Include="..\aoc2019\aoc2019.csproj" />
</ItemGroup>
</Project>

View File

@ -43,7 +43,7 @@ namespace aoc2019
Console.WriteLine();
}
protected abstract string Part1();
protected abstract string Part2();
public abstract string Part1();
public abstract string Part2();
}
}

View File

@ -3,7 +3,7 @@ using System.Linq;
namespace aoc2019
{
internal sealed class Day01 : Day
public sealed class Day01 : Day
{
private readonly IEnumerable<int> masses;
@ -30,12 +30,12 @@ namespace aoc2019
return total;
}
protected override string Part1()
public override string Part1()
{
return $"{masses.Sum(FuelCost)}";
}
protected override string Part2()
public override string Part2()
{
return $"{masses.Sum(FullCost)}";
}

View File

@ -3,7 +3,7 @@ using System.Linq;
namespace aoc2019
{
internal sealed class Day02 : Day
public sealed class Day02 : Day
{
private readonly IEnumerable<int> input;
@ -28,12 +28,12 @@ namespace aoc2019
return v[0];
}
protected override string Part1()
public override string Part1()
{
return $"{RunIntCode(12, 2)}";
}
protected override string Part2()
public override string Part2()
{
for (var i = 0; i < 100; i++)
for (var j = 0; j < 100; j++)

View File

@ -4,7 +4,7 @@ using System.Linq;
namespace aoc2019
{
internal sealed class Day03 : Day
public sealed class Day03 : Day
{
private readonly IEnumerable<(int, int)> intersections;
private readonly List<Dictionary<(int, int), int>> wires;
@ -15,12 +15,12 @@ namespace aoc2019
intersections = wires[0].Keys.Intersect(wires[1].Keys);
}
protected override string Part1()
public override string Part1()
{
return $"{intersections.Min(x => Math.Abs(x.Item1) + Math.Abs(x.Item2))}";
}
protected override string Part2()
public override string Part2()
{
// add 2 to count (0, 0) on both lines
return $"{intersections.Min(x => wires[0][x] + wires[1][x]) + 2}";

View File

@ -2,7 +2,7 @@
namespace aoc2019
{
internal sealed class Day04 : Day
public sealed class Day04 : Day
{
private readonly int end;
@ -36,12 +36,12 @@ namespace aoc2019
return IsValid(i) && s.Select(c => s.Count(j => j == c)).Any(c => c == 2);
}
protected override string Part1()
public override string Part1()
{
return $"{Enumerable.Range(start, end).Count(IsValid)}";
}
protected override string Part2()
public override string Part2()
{
return $"{Enumerable.Range(start, end).Count(HasOnePair)}";
}

View File

@ -3,7 +3,7 @@ using System.Linq;
namespace aoc2019
{
internal sealed class Day05 : Day
public sealed class Day05 : Day
{
private readonly IEnumerable<int> tape;
@ -63,13 +63,13 @@ namespace aoc2019
}
}
protected override string Part1()
public override string Part1()
{
RunIntCode(tape.ToList(), 1);
return $"{output}";
}
protected override string Part2()
public override string Part2()
{
RunIntCode(tape.ToList(), 5);
return $"{output}";

View File

@ -3,7 +3,7 @@ using System.Linq;
namespace aoc2019
{
internal sealed class Day06 : Day
public sealed class Day06 : Day
{
private readonly Dictionary<string, string> input;
@ -21,12 +21,12 @@ namespace aoc2019
return res;
}
protected override string Part1()
public override string Part1()
{
return $"{input.Keys.Sum(o => GetParents(o).Count - 1)}";
}
protected override string Part2()
public override string Part2()
{
var you = GetParents("YOU");
var san = GetParents("SAN");

View File

@ -4,7 +4,7 @@ using aoc2019.lib;
namespace aoc2019
{
internal sealed class Day07 : Day
public sealed class Day07 : Day
{
private readonly IntCodeVM[] Amplifiers = new IntCodeVM[5];
@ -13,7 +13,7 @@ namespace aoc2019
for (var i = 0; i < 5; i++) Amplifiers[i] = new IntCodeVM(Input.First());
}
protected override string Part1()
public override string Part1()
{
long i, largest = 0;
@ -34,7 +34,7 @@ namespace aoc2019
return $"{largest}";
}
protected override string Part2()
public override string Part2()
{
long i, largest = 0;

View File

@ -5,7 +5,7 @@ using aoc2019.lib;
namespace aoc2019
{
internal sealed class Day08 : Day
public sealed class Day08 : Day
{
private readonly List<List<char>> photo;
@ -14,13 +14,13 @@ namespace aoc2019
photo = Input.First().Chunk(25 * 6).Select(s => s.ToList()).ToList();
}
protected override string Part1()
public override string Part1()
{
var l = photo.OrderBy(layer => layer.Count(pixel => pixel == '0')).First();
return $"{l.Count(p => p == '1') * l.Count(p => p == '2')}";
}
protected override string Part2()
public override string Part2()
{
return Enumerable.Range(0, 25 * 6)
.Select(p => Enumerable.Range(0, photo.Count)

View File

@ -3,7 +3,7 @@ using aoc2019.lib;
namespace aoc2019
{
internal sealed class Day09 : Day
public sealed class Day09 : Day
{
private readonly IntCodeVM vm;
@ -12,14 +12,14 @@ namespace aoc2019
vm = new IntCodeVM(Input.First());
}
protected override string Part1()
public override string Part1()
{
vm.Reset();
vm.Run(1);
return $"{vm.output.ToDelimitedString(",")}";
}
protected override string Part2()
public override string Part2()
{
vm.Reset();
vm.Run(2);

View File

@ -5,7 +5,7 @@ using aoc2019.lib;
namespace aoc2019
{
internal sealed class Day10 : Day
public sealed class Day10 : Day
{
private readonly HashSet<(int x, int y)> asteroids;
private (int x, int y) best = (x: -1, y: -1);
@ -21,7 +21,7 @@ namespace aoc2019
.ToHashSet();
}
protected override string Part1()
public override string Part1()
{
foreach (var asteroid in asteroids)
{
@ -41,7 +41,7 @@ namespace aoc2019
return $"{bestCanSee}";
}
protected override string Part2()
public override string Part2()
{
static IEnumerable<(int x, int y, double angle, double dist)> GetValue(
Queue<(int x, int y, double angle, double dist)> q)

View File

@ -5,7 +5,7 @@ using aoc2019.lib;
namespace aoc2019
{
internal sealed class Day11 : Day
public sealed class Day11 : Day
{
private readonly IntCodeVM vm;
private Direction heading;
@ -78,12 +78,12 @@ namespace aoc2019
return map;
}
protected override string Part1()
public override string Part1()
{
return $"{PaintShip(0).Count}";
}
protected override string Part2()
public override string Part2()
{
var map = PaintShip(1);
var minX = (int) map.Keys.Select(x => x.x).Min();

View File

@ -4,7 +4,7 @@ using System.Linq;
namespace aoc2019
{
internal sealed class Day12 : Day
public sealed class Day12 : Day
{
private readonly List<Position> moons;
private int step;
@ -48,7 +48,7 @@ namespace aoc2019
step++;
}
protected override string Part1()
public override string Part1()
{
while (step < 1000)
Step();
@ -56,7 +56,7 @@ namespace aoc2019
return $"{moons.Sum(p => p.TotalEnergy)}";
}
protected override string Part2()
public override string Part2()
{
int cycleX = 0, cycleY = 0, cycleZ = 0;

View File

@ -5,7 +5,7 @@ using aoc2019.lib;
namespace aoc2019
{
internal sealed class Day13 : Day
public sealed class Day13 : Day
{
private readonly Dictionary<(int x, int y), int> board;
@ -52,14 +52,14 @@ namespace aoc2019
}
}
protected override string Part1()
public override string Part1()
{
vm.Reset();
vm.Run();
return $"{vm.output.Where((v, i) => (i + 1) % 3 == 0 && v == 2).Count()}";
}
protected override string Part2()
public override string Part2()
{
vm.Reset();
vm.memory[0] = 2;

View File

@ -4,7 +4,7 @@ using System.Linq;
namespace aoc2019
{
internal sealed class Day14 : Day
public sealed class Day14 : Day
{
private readonly Dictionary<string, Reaction> reactions;
@ -48,14 +48,14 @@ namespace aoc2019
return true;
}
protected override string Part1()
public override string Part1()
{
available = new Dictionary<string, long> {{"ORE", long.MaxValue}};
Consume("FUEL", 1);
return $"{long.MaxValue - available["ORE"]}";
}
protected override string Part2()
public override string Part2()
{
const long capacity = 1_000_000_000_000;
available = new Dictionary<string, long> {{"ORE", capacity}};

View File

@ -5,7 +5,7 @@ using aoc2019.lib;
namespace aoc2019
{
internal sealed class Day15 : Day
public sealed class Day15 : Day
{
private readonly bool verbose = false;
private readonly IntCodeVM vm;
@ -15,7 +15,7 @@ namespace aoc2019
vm = new IntCodeVM(Input.First());
}
protected override string Part1()
public override string Part1()
{
vm.Reset();
var currentLocation = new Location(0, 0);
@ -109,7 +109,7 @@ namespace aoc2019
return "";
}
protected override string Part2()
public override string Part2()
{
var changed = true;
while (changed)

View File

@ -5,7 +5,7 @@ using aoc2019.lib;
namespace aoc2019
{
internal sealed class Day16 : Day
public sealed class Day16 : Day
{
private static readonly int[] BasePattern = {0, 1, 0, -1};
private readonly int[] initialList;
@ -15,7 +15,7 @@ namespace aoc2019
initialList = Input.First().Select(c => int.Parse($"{c}")).ToArray();
}
protected override string Part1()
public override string Part1()
{
const int phaseCount = 100;
var signal0 = initialList.ToArray();
@ -29,7 +29,7 @@ namespace aoc2019
.ToArray());
}
protected override string Part2()
public override string Part2()
{
const int phaseCount = 100;
var messageOffset = initialList.Take(7).Aggregate((n, i) => n * 10 + i);

View File

@ -5,7 +5,7 @@ using aoc2019.lib;
namespace aoc2019
{
internal sealed class Day17 : Day
public sealed class Day17 : Day
{
private const bool Verbose = false;
@ -16,7 +16,7 @@ namespace aoc2019
vm = new IntCodeVM(Input.First());
}
protected override string Part1()
public override string Part1()
{
vm.Reset();
vm.Run();
@ -39,7 +39,7 @@ namespace aoc2019
return $"{sum}";
}
protected override string Part2()
public override string Part2()
{
//vm.Reset();
//vm.memory[0] = 2;

View File

@ -1,32 +1,32 @@
using System;
using System.Linq;
using System.Reflection;
namespace aoc2019
{
internal static class Program
{
private static void Main(string[] args)
{
var days =
Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.BaseType == typeof(Day))
.Select(t => (Day) Activator.CreateInstance(t))
.OrderBy(d => d.DayNumber);
if (args.Length == 1 && int.TryParse(args[0], out var dayNum))
{
var day = days.FirstOrDefault(d => d.DayNumber == dayNum);
if (day != null)
day.AllParts();
else
Console.WriteLine($"{dayNum} invalid or not yet implemented");
}
else
{
foreach (var d in days) d.AllParts();
}
}
}
using System;
using System.Linq;
using System.Reflection;
namespace aoc2019
{
internal static class Program
{
private static void Main(string[] args)
{
var days =
Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.BaseType == typeof(Day))
.Select(t => (Day) Activator.CreateInstance(t))
.OrderBy(d => d.DayNumber);
if (args.Length == 1 && int.TryParse(args[0], out var dayNum))
{
var day = days.FirstOrDefault(d => d.DayNumber == dayNum);
if (day != null)
day.AllParts();
else
Console.WriteLine($"{dayNum} invalid or not yet implemented");
}
else
{
foreach (var d in days) d.AllParts();
}
}
}
}

View File

@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Update="input\day*.in">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Update="input\day*.in">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>