-
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
Showing
3 changed files
with
102 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
5093 |
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,64 @@ | ||
namespace AdventOfCode.Solutions._2018; | ||
|
||
/// <summary> | ||
/// Day 11: Chronal Charge | ||
/// https://adventofcode.com/2018/day/11 | ||
/// </summary> | ||
[Description("Chronal Charge")] | ||
public sealed partial class Day11 { | ||
|
||
public static string Part1(string[] input, params object[]? _) => Solution1(input).ToString(); | ||
public static string Part2(string[] input, params object[]? _) => Solution2(input).ToString(); | ||
|
||
private static string Solution1(string[] input) { | ||
int gridSerialNo = input[0].AsInt(); | ||
|
||
int[,] fuelCells = new int[300, 300]; | ||
foreach ((int cellX, int cellY) in fuelCells.Walk2dArray()) { | ||
int value = CalculatePowerValue(gridSerialNo, cellX + 1, cellY + 1); | ||
fuelCells[cellX, cellY] = value; | ||
} | ||
|
||
int maxValue = int.MinValue; | ||
string topLeft = ""; | ||
foreach (Cell<int> fuelCell in fuelCells.Walk2dArrayWithValues()) { | ||
if (fuelCell.X < 298 && fuelCell.Y < 298) { | ||
int value = fuelCell.Value | ||
+ fuelCells[fuelCell.X + 1, fuelCell.Y] | ||
+ fuelCells[fuelCell.X + 2, fuelCell.Y] | ||
+ fuelCells[fuelCell.X + 0, fuelCell.Y + 1] | ||
+ fuelCells[fuelCell.X + 1, fuelCell.Y + 1] | ||
+ fuelCells[fuelCell.X + 2, fuelCell.Y + 1] | ||
+ fuelCells[fuelCell.X + 0, fuelCell.Y + 2] | ||
+ fuelCells[fuelCell.X + 1, fuelCell.Y + 2] | ||
+ fuelCells[fuelCell.X + 2, fuelCell.Y + 2]; | ||
if (value > maxValue) { | ||
maxValue = value; | ||
topLeft = $"{fuelCell.X + 1},{fuelCell.Y + 1}"; | ||
} | ||
} | ||
} | ||
|
||
return topLeft; | ||
} | ||
|
||
private static string Solution2(string[] input) | ||
{ | ||
return "** Solution not written yet **"; | ||
} | ||
|
||
public static int CalculatePowerValue(int gridSerialNo, int x, int y) | ||
{ | ||
int rackId = x + 10; | ||
int value = rackId * y; | ||
value += gridSerialNo; | ||
value *= rackId; | ||
value = value switch | ||
{ | ||
>= 100 => $"{value}"[^3] - '0', | ||
_ => 0, | ||
}; | ||
value -= 5; | ||
return value; | ||
} | ||
} |
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,37 @@ | ||
using static AdventOfCode.Solutions._2018.Day11; | ||
|
||
namespace AdventOfCode.Tests.Year2018; | ||
|
||
public class Tests_11_Chronal_Charge | ||
{ | ||
const int DAY = 11; | ||
|
||
[Theory] | ||
[InlineData("18", 33, 45)] | ||
[InlineData("42", 21, 61)] | ||
public void Part1(string input, int expectedX, int expectedY) | ||
{ | ||
string actual = SolutionRouter.SolveProblem(YEAR, DAY, PART1, input); | ||
string expected = $"{expectedX},{expectedY}"; | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
[Theory] | ||
[InlineData(8, 3, 5, 4)] | ||
[InlineData(57, 122, 79, -5)] | ||
[InlineData(39, 217, 196, 0)] | ||
[InlineData(71, 101, 153, 4)] | ||
public void FuelCellP0werLevel(int gridSerialNo, int x, int y, int expected) | ||
{ | ||
int actual = CalculatePowerValue(gridSerialNo, x, y); | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
//[Theory] | ||
//[InlineData(21, 61)] | ||
//public void Part2(string input, int expected) | ||
//{ | ||
// _ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART2, input, 32), out int actual); | ||
// Assert.Equal(expected, actual); | ||
//} | ||
} |