Skip to content

Commit

Permalink
2018 Day11 Part1 Solved
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Nov 11, 2023
1 parent de4f19d commit f48f1c6
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions Data/2018_11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5093
64 changes: 64 additions & 0 deletions Solutions/2018/Day11.cs
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;
}
}
37 changes: 37 additions & 0 deletions Tests/2018/Tests_11.cs
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);
//}
}

0 comments on commit f48f1c6

Please sign in to comment.