From b3de92a6e179e717d65cfee26ee72e7fff61fed8 Mon Sep 17 00:00:00 2001 From: Jonathan George Date: Tue, 19 Feb 2019 11:33:26 +0000 Subject: [PATCH] Day 11 --- AoC2018.Runner/AoC2018.Runner.csproj | 3 + AoC2018.Runner/Input/day11.txt | 1 + AoC2018.Runner/Properties/launchSettings.json | 2 +- AoC2018.Solutions/Day11/Part01.cs | 63 +++++++++++++++ AoC2018.Solutions/Day11/Part02.cs | 79 +++++++++++++++++++ AoC2018.Tests/AoCTestCases.cs | 4 + 6 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 AoC2018.Runner/Input/day11.txt create mode 100644 AoC2018.Solutions/Day11/Part01.cs create mode 100644 AoC2018.Solutions/Day11/Part02.cs diff --git a/AoC2018.Runner/AoC2018.Runner.csproj b/AoC2018.Runner/AoC2018.Runner.csproj index 0a408ef..22e69c9 100644 --- a/AoC2018.Runner/AoC2018.Runner.csproj +++ b/AoC2018.Runner/AoC2018.Runner.csproj @@ -28,6 +28,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/AoC2018.Runner/Input/day11.txt b/AoC2018.Runner/Input/day11.txt new file mode 100644 index 0000000..581e146 --- /dev/null +++ b/AoC2018.Runner/Input/day11.txt @@ -0,0 +1 @@ +8868 \ No newline at end of file diff --git a/AoC2018.Runner/Properties/launchSettings.json b/AoC2018.Runner/Properties/launchSettings.json index f991d3b..77e4882 100644 --- a/AoC2018.Runner/Properties/launchSettings.json +++ b/AoC2018.Runner/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "AoC2018.Runner": { "commandName": "Project", - "commandLineArgs": "5 2" + "commandLineArgs": "11 2" } } } \ No newline at end of file diff --git a/AoC2018.Solutions/Day11/Part01.cs b/AoC2018.Solutions/Day11/Part01.cs new file mode 100644 index 0000000..4704e16 --- /dev/null +++ b/AoC2018.Solutions/Day11/Part01.cs @@ -0,0 +1,63 @@ +namespace AoC2018.Solutions.Day11 +{ + using System.Collections.Generic; + using System.Linq; + + public class Part01 : ISolution + { + public string Solve(string input) + { + int serialNumber = int.Parse(input); + + // Grid is represented as a 1d array because it's easier to work with. + // Remember in this form, the grid is 0 based but the question expectes 1 based... + int[] grid = Enumerable.Range(0, 90000).Select(x => this.PowerLevel(x, serialNumber)).ToArray(); + + // Now we have the grid, we need to find the most powerful 3x3 bank of cells + (int X, int Y, int PowerLevel) bestGridSoFar = (-1, -1, int.MinValue); + + for (int x = 0; x < 297; x++) + { + for (int y = 0; y < 297; y++) + { + int bankPower = grid[this.ToIndex(x, y)] + + grid[this.ToIndex(x + 1, y)] + + grid[this.ToIndex(x + 2, y)] + + grid[this.ToIndex(x, y + 1)] + + grid[this.ToIndex(x + 1, y + 1)] + + grid[this.ToIndex(x + 2, y + 1)] + + grid[this.ToIndex(x, y + 2)] + + grid[this.ToIndex(x + 1, y + 2)] + + grid[this.ToIndex(x + 2, y + 2)]; + + if (bankPower > bestGridSoFar.PowerLevel) + { + bestGridSoFar = (x, y, bankPower); + } + } + } + + return string.Concat(bestGridSoFar.X + 1, ",", bestGridSoFar.Y + 1); + } + + private int ToIndex(int x, int y) + { + return x + (y * 300); + } + + private int PowerLevel(int cell, int serialNumber) + { + int x = (cell % 300) + 1; + int y = (cell / 300) + 1; + + int rackId = x + 10; + int startPower = rackId * y; + int power = startPower += serialNumber; + power *= rackId; + + int hundreds = (power % 1000) / 100; + + return hundreds - 5; + } + } +} diff --git a/AoC2018.Solutions/Day11/Part02.cs b/AoC2018.Solutions/Day11/Part02.cs new file mode 100644 index 0000000..235bc0a --- /dev/null +++ b/AoC2018.Solutions/Day11/Part02.cs @@ -0,0 +1,79 @@ +namespace AoC2018.Solutions.Day11 +{ + using System; + using System.Linq; + + public class Part02 : ISolution + { + public string Solve(string input) + { + int serialNumber = int.Parse(input); + + // Grid is represented as a 1d array because it's easier to work with. + // Remember in this form, the grid is 0 based but the question expectes 1 based... + int[] grid = Enumerable.Range(0, 90000).Select(x => this.PowerLevel(x, serialNumber)).ToArray(); + + // Now we have the grid, we need to find the most powerful 3x3 bank of cells + (int X, int Y, int Size, int PowerLevel) bestGridSoFar = (-1, -1, -1, int.MinValue); + + for (int x = 0; x < 300; x++) + { + for (int y = 0; y < 300; y++) + { + int sizeMax = Math.Min(300 - x, 300 - y); + int previousSizePower = 0; + + // This loop is calculating the power of a bank of every + // possible size starting at x, y. It does this by using the + // power for the previous bank size and just adding the edge + // values to get the new total + for (int i = 0; i < sizeMax; i++) + { + int additionalPower = 0; + + // Add the column to the right of the previous bank + for (int dy = 0; dy < i; dy++) + { + additionalPower += grid[this.ToIndex(x + i, y + dy)]; + } + + // Now add the row to the bottom of the previous bank + for (int dx = 0; dx < (i + 1); dx++) + { + additionalPower += grid[this.ToIndex(x + dx, y + i)]; + } + + previousSizePower += additionalPower; + + if (previousSizePower > bestGridSoFar.PowerLevel) + { + bestGridSoFar = (x, y, i, previousSizePower); + } + } + } + } + + return string.Concat(bestGridSoFar.X + 1, ",", bestGridSoFar.Y + 1, ",", bestGridSoFar.Size + 1); + } + + private int ToIndex(int x, int y) + { + return x + (y * 300); + } + + private int PowerLevel(int cell, int serialNumber) + { + int x = (cell % 300) + 1; + int y = (cell / 300) + 1; + + int rackId = x + 10; + int startPower = rackId * y; + int power = startPower += serialNumber; + power *= rackId; + + int hundreds = (power % 1000) / 100; + + return hundreds - 5; + } + } +} diff --git a/AoC2018.Tests/AoCTestCases.cs b/AoC2018.Tests/AoCTestCases.cs index f963df3..2260109 100644 --- a/AoC2018.Tests/AoCTestCases.cs +++ b/AoC2018.Tests/AoCTestCases.cs @@ -33,6 +33,10 @@ public class AoCTestCases [TestCase(7, 1, "Step C must be finished before step A can begin.\r\nStep C must be finished before step F can begin.\r\nStep A must be finished before step B can begin.\r\nStep A must be finished before step D can begin.\r\nStep B must be finished before step E can begin.\r\nStep D must be finished before step E can begin.\r\nStep F must be finished before step E can begin.", "CABDFE")] [TestCase(8, 1, "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2", "138")] [TestCase(8, 2, "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2", "66")] + [TestCase(11, 1, "18", "33,45")] + [TestCase(11, 1, "42", "21,61")] + [TestCase(11, 2, "18", "90,269,16")] + [TestCase(11, 2, "42", "232,251,12")] public void Tests(int day, int part, string input, string expectedResult) { ISolution solution = SolutionFactory.GetSolution(day, part);