diff --git a/AOC.Test/Test2015.cs b/AOC.Test/Test2015.cs index 6843069..c5a5f28 100644 --- a/AOC.Test/Test2015.cs +++ b/AOC.Test/Test2015.cs @@ -11,6 +11,7 @@ public class Test2015 [DataRow(typeof(Day03), "2081", "2341")] // [DataRow(typeof(Day04), "346386", "9958218")] [DataRow(typeof(Day05), "258", "53")] + // [DataRow(typeof(Day06), "543903", "14687245")] public void TestAllDays(Type dayType, string part1, string part2) { Common.CheckDay(dayType, part1, part2); @@ -22,6 +23,7 @@ public class Test2015 [DataRow(typeof(Day03), "2", "11")] // [DataRow(typeof(Day04), "609043", "6742839")] [DataRow(typeof(Day05), "1", "1")] + [DataRow(typeof(Day06), "1000000", "1000000")] public void CheckTestInputs(Type dayType, string part1, string part2) { Day.UseTestInput = true; diff --git a/AOC2015/Day06.cs b/AOC2015/Day06.cs index d9baddb..83a5f4b 100644 --- a/AOC2015/Day06.cs +++ b/AOC2015/Day06.cs @@ -3,13 +3,105 @@ /// /// Day 6: /// -public sealed class Day06 : Day +public sealed partial class Day06 : Day { - public Day06() : base(2015, 6, "Puzzle Name") + [GeneratedRegex(@"(\d+),(\d+) through (\d+),(\d+)")] + private static partial Regex Coords(); + + private readonly Dictionary<(int x, int y), bool> _lightGrid = new(); + private readonly Dictionary<(int x, int y), int> _brightnessGrid = new(); + + public Day06() : base(2015, 6, "Probably a Fire Hazard") { } - public override object Part1() => ""; + public override object Part1() + { + foreach (var line in Input) + { + var d = Coords().Match(line).Groups.Values.Skip(1).Select(v => int.Parse(v.Value)).ToList(); + int x1 = d[0], y1 = d[1], x2 = d[2], y2 = d[3]; + int xStart = Math.Min(x1, x2), xEnd = Math.Max(x1, x2), yStart = Math.Min(y1, y2), yEnd = Math.Max(y1, y2); - public override object Part2() => ""; -} + for (var x = xStart; x <= xEnd; x++) + { + for (var y = yStart; y <= yEnd; y++) + { + if (line.StartsWith("toggle")) + { + if (_lightGrid.ContainsKey((x, y))) + { + _lightGrid[(x, y)] = !_lightGrid[(x, y)]; + } + else + { + _lightGrid[(x, y)] = true; + } + } + else if (line.StartsWith("turn on")) + { + _lightGrid[(x, y)] = true; + } + else if (line.StartsWith("turn off")) + { + _lightGrid[(x, y)] = false; + } + } + } + } + + return _lightGrid.Count(v => v.Value); + } + + public override object Part2() + { + foreach (var line in Input) + { + var d = Coords().Match(line).Groups.Values.Skip(1).Select(v => int.Parse(v.Value)).ToList(); + int x1 = d[0], y1 = d[1], x2 = d[2], y2 = d[3]; + int xStart = Math.Min(x1, x2), xEnd = Math.Max(x1, x2), yStart = Math.Min(y1, y2), yEnd = Math.Max(y1, y2); + + for (var x = xStart; x <= xEnd; x++) + { + for (var y = yStart; y <= yEnd; y++) + { + if (line.StartsWith("toggle")) + { + if (_brightnessGrid.ContainsKey((x, y))) + { + _brightnessGrid[(x, y)] += 2; + } + else + { + _brightnessGrid[(x, y)] = 2; + } + } + else if (line.StartsWith("turn on")) + { + if (_brightnessGrid.ContainsKey((x, y))) + { + _brightnessGrid[(x, y)]++; + } + else + { + _brightnessGrid[(x, y)] = 1; + } + } + else if (line.StartsWith("turn off")) + { + if (_brightnessGrid.ContainsKey((x, y)) && _brightnessGrid[(x, y)] > 0) + { + _brightnessGrid[(x, y)]--; + } + else + { + _brightnessGrid[(x, y)] = 0; + } + } + } + } + } + + return _brightnessGrid.Sum(v => v.Value); + } +} \ No newline at end of file diff --git a/AOC2015/input2015/test06.in b/AOC2015/input2015/test06.in new file mode 100644 index 0000000..8a28f03 --- /dev/null +++ b/AOC2015/input2015/test06.in @@ -0,0 +1 @@ +turn on 0,0 through 999,999