Skip to content

Commit

Permalink
Day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
jongeorge1 committed Feb 19, 2019
1 parent 48bbb30 commit b3de92a
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 1 deletion.
3 changes: 3 additions & 0 deletions AoC2018.Runner/AoC2018.Runner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<None Update="Input\day04.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Input\day11.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Input\day10.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
1 change: 1 addition & 0 deletions AoC2018.Runner/Input/day11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8868
2 changes: 1 addition & 1 deletion AoC2018.Runner/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"AoC2018.Runner": {
"commandName": "Project",
"commandLineArgs": "5 2"
"commandLineArgs": "11 2"
}
}
}
63 changes: 63 additions & 0 deletions AoC2018.Solutions/Day11/Part01.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
79 changes: 79 additions & 0 deletions AoC2018.Solutions/Day11/Part02.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
4 changes: 4 additions & 0 deletions AoC2018.Tests/AoCTestCases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b3de92a

Please sign in to comment.