-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48bbb30
commit b3de92a
Showing
6 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
8868 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters