diff --git a/AOC.Test/Test2015.cs b/AOC.Test/Test2015.cs index 639a4dd..aec4ef0 100644 --- a/AOC.Test/Test2015.cs +++ b/AOC.Test/Test2015.cs @@ -8,6 +8,7 @@ public class Test2015 [DataTestMethod] [DataRow(typeof(Day01), "232", "1783")] [DataRow(typeof(Day02),"1586300", "3737498")] + [DataRow(typeof(Day03), "2081", "2341")] public void TestAllDays(Type dayType, string part1, string part2) { Common.CheckDay(dayType, part1, part2); @@ -15,6 +16,7 @@ public class Test2015 [DataTestMethod] [DataRow(typeof(Day02), "58", "34")] + [DataRow(typeof(Day03), "2", "11")] public void CheckTestInputs(Type dayType, string part1, string part2) { Day.UseTestInput = true; diff --git a/AOC2015/Day03.cs b/AOC2015/Day03.cs index 0ab6e2e..1957ab6 100644 --- a/AOC2015/Day03.cs +++ b/AOC2015/Day03.cs @@ -5,11 +5,70 @@ /// public sealed class Day03 : Day { - public Day03() : base(2015, 3, "Puzzle Name") + public Day03() : base(2015, 3, "Perfectly Spherical Houses in a Vacuum") { } - public override object Part1() => ""; + public override object Part1() + { + int x = 0, y = 0; + Dictionary<(int x, int y), int> map = new() { [(0, 0)] = 1 }; - public override object Part2() => ""; + foreach (var c in Input.First()) + { + switch (c) + { + case '>': x++; break; + case '<': x--; break; + case '^': y++; break; + case 'v': y--; break; + } + + if (map.ContainsKey((x, y))) map[(x, y)]++; + else map[(x, y)] = 1; + } + + return map.Count(m => m.Value >= 1); + } + + public override object Part2() + { + int x1 = 0, y1 = 0, x2 = 0, y2 = 0; + Dictionary<(int x, int y), int> map = new() { [(0, 0)] = 1 }; + var santaTurn = true; + + foreach (var c in Input.First()) + { + if (santaTurn) + { + switch (c) + { + case '>': x1++; break; + case '<': x1--; break; + case '^': y1++; break; + case 'v': y1--; break; + } + + if (map.ContainsKey((x1, y1))) map[(x1, y1)]++; + else map[(x1, y1)] = 1; + } + else + { + switch (c) + { + case '>': x2++; break; + case '<': x2--; break; + case '^': y2++; break; + case 'v': y2--; break; + } + + if (map.ContainsKey((x2, y2))) map[(x2, y2)]++; + else map[(x2, y2)] = 1; + } + + santaTurn = !santaTurn; + } + + return map.Count(m => m.Value >= 1); + } } diff --git a/AOC2015/input2015/test03.in b/AOC2015/input2015/test03.in new file mode 100644 index 0000000..8a6e322 --- /dev/null +++ b/AOC2015/input2015/test03.in @@ -0,0 +1 @@ +^v^v^v^v^v