From c60fe81fb10281db5a75c538d097ead37465f6f1 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Thu, 23 Nov 2023 11:35:08 -0500 Subject: [PATCH] 2016 day 2 --- AOC.Common/Day.cs | 4 +-- AOC.Test/Test2016.cs | 8 ++++++ AOC2016/Day02.cs | 54 ++++++++++++++++++++++++++++++++++--- AOC2016/input2016/test02.in | 4 +++ 4 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 AOC2016/input2016/test02.in diff --git a/AOC.Common/Day.cs b/AOC.Common/Day.cs index 011b05c..3ddb3e9 100644 --- a/AOC.Common/Day.cs +++ b/AOC.Common/Day.cs @@ -64,8 +64,8 @@ public abstract class Day(int year, int day, string puzzleName) throw new ApplicationException("no days found"); } - UseTestInput = args is [_, "--test"] or ["--test", ..]; - var verbose = args is [_, "--verbose"] or ["--verbose", ..]; + UseTestInput = args.Contains("--test"); + var verbose = args.Contains("--verbose"); if (args.Length > 0 && int.TryParse(args[0], out var dayNum)) { diff --git a/AOC.Test/Test2016.cs b/AOC.Test/Test2016.cs index 986d3f6..5e7bea5 100644 --- a/AOC.Test/Test2016.cs +++ b/AOC.Test/Test2016.cs @@ -7,8 +7,16 @@ public class Test2016 { [DataTestMethod] [DataRow(typeof(Day01), "300", "159")] + [DataRow(typeof(Day02), "76792", "A7AC3")] public void CheckAllDays(Type dayType, string part1, string part2) { Common.CheckDay(dayType, part1, part2); } + + [DataTestMethod] + [DataRow(typeof(Day02), "1985", "5DB3")] + public void CheckTestInputs(Type dayType, string part1, string part2) + { + Common.CheckDay(dayType, part1, part2, true); + } } diff --git a/AOC2016/Day02.cs b/AOC2016/Day02.cs index 174fe3c..a915a77 100644 --- a/AOC2016/Day02.cs +++ b/AOC2016/Day02.cs @@ -9,7 +9,55 @@ public sealed class Day02() : Day(2016, 2, "Puzzle Name") { } - public override object Part1() => ""; + public override object Part1() + { + List answer = new(); + var location = (x: 1, y: 1); - public override object Part2() => ""; -} + foreach (var line in Input) + { + location = line.Aggregate(location, (c, instruction) => instruction switch + { + 'U' => c with { y = c.y == 0 ? c.y : c.y - 1 }, + 'D' => c with { y = c.y == 2 ? c.y : c.y + 1 }, + 'L' => c with { x = c.x == 0 ? c.x : c.x - 1 }, + 'R' => c with { x = c.x == 2 ? c.x : c.x + 1 }, + _ => throw new ArgumentException("invalid direction", nameof(instruction)) + }); + + answer.Add(1 + location.x + location.y * 3); + } + + return string.Join("", answer); + } + + public override object Part2() + { + var keyPad = new[,] + { + { '\0', '\0', '1', '\0', '\0' }, + { '\0', '2', '3', '4', '\0' }, + { '5', '6', '7', '8', '9' }, + { '\0', 'A', 'B', 'C', '\0' }, + { '\0', '\0', 'D', '\0', '\0' }, + }; + var location = (x: 0, y: 2); + List answer = new(); + + foreach (var line in Input) + { + location = line.Aggregate(location, (c, instruction) => instruction switch + { + 'U' => c with { y = c.y == 0 || keyPad[c.y - 1, c.x] == '\0' ? c.y : c.y - 1 }, + 'D' => c with { y = c.y == 4 || keyPad[c.y + 1, c.x] == '\0' ? c.y : c.y + 1 }, + 'L' => c with { x = c.x == 0 || keyPad[c.y, c.x - 1] == '\0' ? c.x : c.x - 1 }, + 'R' => c with { x = c.x == 4 || keyPad[c.y, c.x + 1] == '\0' ? c.x : c.x + 1 }, + _ => throw new ArgumentException("invalid direction", nameof(instruction)) + }); + + answer.Add(keyPad[location.y, location.x]); + } + + return string.Join("", answer); + } +} \ No newline at end of file diff --git a/AOC2016/input2016/test02.in b/AOC2016/input2016/test02.in new file mode 100644 index 0000000..5139196 --- /dev/null +++ b/AOC2016/input2016/test02.in @@ -0,0 +1,4 @@ +ULL +RRDDD +LURDL +UUUUD