-
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
18 changed files
with
2,943 additions
and
6 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
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,122 @@ | ||
using Grid = char[][]; | ||
|
||
namespace AdventOfCode.Day14; | ||
|
||
public sealed class Day14(ITestOutputHelper output) : TestBase(output) | ||
{ | ||
protected override string Day => "Day14"; | ||
|
||
private readonly string[] _example = | ||
[ | ||
"O....#....", // 10 | ||
"O.OO#....#", // 9 | ||
".....##...", // 8 | ||
"OO.#O....O", // 7 | ||
".O.....O#.", // 6 | ||
"O.#..O.#.#", // 5 | ||
"..O..#O..O", // 4 | ||
".......O..", // 3 | ||
"#....###..", // 2 | ||
"#OO..#...." // 1 | ||
]; | ||
|
||
[Fact] | ||
public void ExampleOne() => CalculateInitialLoadSum(_example).Should().Be(136); | ||
|
||
[Fact] | ||
public void PartOne() => WriteOutput(CalculateInitialLoadSum(Input)); | ||
|
||
[Fact] | ||
public void ExampleTwo() => StressLoad(_example).Should().Be(64); | ||
|
||
[Fact] | ||
public void PartTwo() => WriteOutput(StressLoad(Input)); | ||
|
||
private static int StressLoad(string[] input) | ||
{ | ||
var states = new Dictionary<string, int>(); | ||
var grid = input.Select(i => i.ToCharArray()).ToArray(); | ||
var maxCycles = 1_000_000_000; | ||
var cycle = 1; | ||
while (cycle <= maxCycles) | ||
{ | ||
for (var i = 0; i < 4; i++) | ||
{ | ||
grid = Tilt(grid); | ||
grid = Rotate(grid); | ||
} | ||
if (states.TryGetValue(grid.Hash(), out var g)) | ||
{ | ||
var cycleLength = cycle - g; | ||
var numCycles = (maxCycles - g) / cycleLength; | ||
cycle = g + (numCycles * cycleLength); | ||
} | ||
states[grid.Hash()] = cycle; | ||
cycle++; | ||
} | ||
|
||
return CalculateLoad(grid); | ||
} | ||
|
||
private int CalculateInitialLoadSum(string[] input) | ||
{ | ||
var grid = input.Select(i => i.ToCharArray()).ToArray(); | ||
grid = Tilt(grid); | ||
return CalculateLoad(grid); | ||
} | ||
|
||
public static int CalculateLoad(Grid grid) | ||
{ | ||
var load = 0; | ||
for (var y = 0; y < grid.Length; y++) | ||
for (var x = 0; x < grid[y].Length; x++) | ||
{ | ||
if (grid[y][x] == 'O') | ||
load += grid.Length - y; | ||
} | ||
|
||
return load; | ||
} | ||
|
||
public static Grid Rotate(Grid grid) | ||
{ | ||
var newGrid = Enumerable.Range(0, grid.Length) | ||
.Select(y => new char[grid[y].Length]) | ||
.ToArray(); | ||
var yy = grid.Length; | ||
for (var y = 0; y < grid.Length; y++) | ||
{ | ||
var xx = grid[y].Length; | ||
for (var x = 0; x < xx; x++) | ||
{ | ||
newGrid[x][yy - y - 1] = grid[y][x]; | ||
} | ||
} | ||
|
||
return newGrid; | ||
} | ||
|
||
public static Grid Tilt(Grid grid) | ||
{ | ||
for (var y = 0; y < grid.Length; y++) | ||
for (var x = 0; x < grid[y].Length; x++) | ||
{ | ||
if (grid[y][x] != 'O') continue; | ||
var yy = y; | ||
while (yy > 0 && grid[yy - 1][x] == '.') | ||
{ | ||
grid[yy][x] = '.'; | ||
grid[yy - 1][x] = 'O'; | ||
yy--; | ||
} | ||
} | ||
|
||
return grid; | ||
} | ||
} | ||
|
||
public static class GridExtensions | ||
{ | ||
public static string Hash(this Grid grid) => | ||
string.Join(Environment.NewLine, grid.Select(g => string.Join("", g))); | ||
} |
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,100 @@ | ||
.O#O..##...#O#.#.......##.....O..#.O##.O#......O.........#....O.O..........#...O...#O.#.OO...OO..... | ||
..............O.#OO..#.#..O..#O..OOO...O......O......O.....O.##.#O.#.O...#....O.#O.O......#OO.O....# | ||
O.#.O.#..O..OO.#O.#.#.O.......O#.O#..#..#..#......O#..O.#...#O..O.........................#..O..#.## | ||
....OO..O......OO...O.O.#...###.#.OO.#.OO.#O.....O#....#..#....O....##.O....#O...O...#.#.O#.#O.O#... | ||
......O#...O..........#..#O...O...O...#.#.#.......O....#....#O....O..O#..O.#..O##..O..#....#..O.O#.. | ||
....O...O..O.#...O.O.....#..#....O.O.##O.#..O......OO..#......O.#O.#O..#.###...#O#..........O....OO# | ||
#......#.O....#O.#.#O.#.#.O......O##O.##O#.#.......OO.#......O.##.O.###.#......#..###.....O.O.OOO#.. | ||
.#.#.#........#........O..O.O#O.O.O#OO....O#.O..O.....O.O....O#O.O##....#...O#.......#.#O...#..#O..O | ||
.O.#.O.#...O...O#.OO.........#...O#O..OO..O#..O.O.O#..O#...O...OO#O...#..O...OO...OO.O#.....#....#.. | ||
...#...#............OO##O#...O.#O##O..#.#....#.O.O.O#...O.O..O.O.###..#.O.##.#O....##....#.......O.O | ||
.#O....OO..OO....O.O.....#.....#...OO.O..#..#.#.OOO...#.O.#...OOO........#....OO#OOO...............O | ||
.OO.#....O.#O.OO..#.OO.........O.#.O......#O......O#...###.#OO.#.#..........O.#O.#O..O#..#..#.O..OOO | ||
#.......O.....#.#.##.#.#.#.....O....#.#..O#OO.OO..O#..O......OO#.O..#...OOO#.O..........O.OO.#..O#.O | ||
....OO....O#OO...O#O#..O#.O.....O.O......#O.#O#......#.....#O.......#.#O....OO......##..##O#O.#..#.. | ||
..#...OO.O.#.#..OO...OO........##........O...OO...O....#.O...OO..O.O.OO.O.#....OOO..#..#.O#.OO..O.#O | ||
#.#.#.#O..OOO..OOO..O#O.O..O..#O..OO....O#....O#..O.O...O#.....#.#..OO...#...O##O.#O.O.O#..OO....... | ||
#..O....O....OO..O.....O....O....#O..........#O.O.O#..#...O...##.#.O.#O..#.O.##..OO.O..O..#...O.#..O | ||
......#..O.#......O.....O#.OO#O.#.O.O.....O...#..O.#OO.......O....O........O.O.#.O.....O#.....##.... | ||
.#...#O..#.O...O#O...O.#..#.#OO...O.O.#O..###..O..OOO...O..#..O.O..O.#...OO.OO.#.O....#....#O.#...#O | ||
.#OOOO..............#O##OO.....#O#.......O...##...O....O#O.......OOO.#O#OO.#......OOOOOO...##..O..#. | ||
O....OO.OO.O.#.O#.#.O..OO......OO#..#O..#.#O.O..#OO.#.....#.O...O........OO..O#......OOOO..O.OO...O. | ||
...OO.##.O.....O...#.O.OOO..O.....O#......#O#O#...#....O..#O...OOO#..O.#.O....O#.....#...O.O##O...#O | ||
..O........#..###O...O##....O..#.O.##..O.O...O##O.O..O...#O..#OOO###O.O###O#O...#.#......O.OOO.#..O# | ||
....O...O...#.##....#...OO.#O#O...O###..O...O..OO.O.O..#OO.O#.O...O#O.#..#.......#..O.##O.##.OO...#O | ||
OO.#...O..O...OO.##..........OO.......#O#O.OO.#O.....O..OO...#O#O...##.O..##O.O.O.OO.OO.#.#O#O...O.# | ||
......O##.#.#..O#........O....O............O##..#...#..#..#O......O...#...#O..O......#.#..O...O...O# | ||
O.#..#.#..#..##..#........O#..#O.O....#..O...OOO......#......OO...O.O.#O..#........O....#....O#..#O. | ||
.......OO.....O......#....OO.O.#O.O....O#..#...O.O.......#...OO.....#.......#...O..#...##.#.O.#..O#. | ||
O###.........O........O.O..O..O...O#.#....#.OOO..O.O.#..O..##....O....O..#O.O..#........O.#O...O...O | ||
#..........##....O.#O..#....OO..#...O.O.O..O..OO#..##..#O.OO..O.#..OO.........#.O....O..O........... | ||
....#...#.....#..OO.O.##...OOO##O..#........OO..O.#.......#.OO#..O....O##.#.O.#.O.###....#..##O..#O# | ||
..#..........O........#....OO.....O...#.##.#.....#....O..##..............O........#....#..O#...O.O#O | ||
O...OO.O.......O....O...#...OO#O.....O.O....O#O.......#.#..O.....O...#.....#.#....#O..OOOOO.O...O.O. | ||
#...O....O.#........#OO.O..O##.#.........O#.O##...#O#..O...#O.#..#...#O..#......O..........O....OO#O | ||
O.....#O.........O#.O#..OO#...#O.O.....O.#..#....#.O..O..O..O#....O......O.O...O#O......#.O...#..O#O | ||
#......O.....O#.O.#OO...........O.O...O#..O...OO.O###O...#O.....#..O...OO.O#.....#O.O.#....#..O..#.. | ||
....#.##..O..O.....OO#....O.........OOO###..O.OO.O..O.##.O.OO..#.#O..#..#.#.##......#...##....#.O..O | ||
.......OO..........O..OO.#..#O.OO#.........O..O#.O..O.#O.O.#......O.....O#....OO..O...O....O..O#OO.. | ||
.O.OO.#OO#......O#O#OO...O#O....##OO.....O#........O....O..O.O..O..O..#....O....OO#..O....OO####O..O | ||
.....O.O#.#.#.O#.OO.#...O#..OOO....O#O...OO......O.##.OO.......O.O....#...O....O....O..#..#..O.#O... | ||
...O...O.......O...#.O...O...O....#...OO##..#.O....O.....#.#..O....O.O.....#..OOO....OO..#.###....#. | ||
....#.O....##....O.#.......O#..###.##O.O.O.......#O#.#O.....O...O.#.O..#.#.....O#.O#......O#O..#O... | ||
.#.O....O...#.#...O#.....#......O.O.......#....OO....O...O#....O.#......#.##OO...#..#O#...O#.O.##..O | ||
O.#.#.....O....O....O.O...O..O.#....O....#O.......#.........#.......O.OO.....O.#.OO.#.#.O.OO..O..... | ||
##..#..O.#...O......#..#.O.O..O...O..#.....##.#...#.O.O##.O....#OOOOOO....##....##.#O#..........#.O. | ||
.#..#...O.O.O........OO.O..OOO....O#..##..#O..OO.........OO.........#....#.O#..O#..#.#..#O..O.O#.... | ||
..#..OO##..#.....O..##......O..OOO.#.....O.#..#O....#O#O.....#.O..OO.#...O..#...#O...#.........#.... | ||
.........#..O....OO..#OO..O.........#..#O..O..OO....O..##..#O..O.O.O...#O##OOO#.........O.......#... | ||
...#...OO.....#.O..#........#.#...O.O....OOO.O#O....###..#.#.......#.O......O..#O....###.O##.#..OO.# | ||
..##O#.#.........OO#O#..OO..O..##.O.O.O.O...O#..OO...#..O...O.O.#.O...#.O.O#....##...O.....#...O..O. | ||
..#O..#..OOO....#.......#O.....#...O#..O##OOO#.#.....O#O..#O...#....#O.O#......#.OO..#.#.O.......O.# | ||
O.O.....#O.#.OOOOOO..###..O.......O.#.O.OO...#..O..##.##.#..O..O..O...#O.....OO....O...O..#...#O.... | ||
..O.OO....#.O..O..#.#OO..#.O.......O.OO#....O..O...O#...O...O#...O...#.#O...#..OO....#.O........O... | ||
.#....#.O#.#....O#O#.##.....O.#.....O..OO....O..O.#.....#OO##.O..O..O..O.......#...OO..#.O.......O.. | ||
O...O#.#O.#.#.#..O..O......#.O..OO..#.O##.#.....#O#.....#O#.....#...#.#O....O.O.....#.......#....#.. | ||
#.O...O.O.....O.O..O...O#O...OO#..##.....O.......#....O..#...O....#..O..OO#.....O..O.O...#....O..O#. | ||
O..O....O..O##..#....OO.#OO........#O.........O............O.#..#O.O.O##..#.###O..O...O#...#..#..... | ||
O.......O........O...O...O.###.#.#OO.O.#.#.O.#O.OO....O..O.###....O.O.#.#O..OO##.O.#O#....#.....#... | ||
..O..O#..OOO..O##O....O#.O.O.O#.#.O.##..O.....#.#..#O...O..O...O....#.......O.O......O#..O.#....#OO. | ||
.#..O..##....O#.......#....O#..O.###.O.#..O...#OO...O#.....O.O....O#...O#...##O...O..O..#....O.O.O.. | ||
.O.......O#O...OO...O...#.OO#OO#.#O..#OO..#O.#OO....#..#...O#O.OO#...OOO..O..OO..#..#.....O.....#... | ||
..O..##.##..O#.O..#.O.O.......#O.....OO..OO#.....O#.O.OOO....O...#..O#O#.#OO...#....#...O........... | ||
.O.#.#...O.O.O.O.#.#O.O..#.OOOOOO.#.OO..#..###..#.#...#.O.O............OOO#O#.O..O....#...O...#..... | ||
..#..OO..#O.....#.##..O...O.OO........#...OO..#...O....O..O....#O.........O...#.O.....O.#...O.....#. | ||
#O..#..#O.O#.O...O...O....#O#O.#O#OO...O#.O........O#....#.....#O#...O..O..#O..OOO##.#...O#...#..O.. | ||
.....#O....O.OOO....#..#O......O.#..O.##.O.O.O...OO.O#...O..OO..#.#........##.....#............O.O.. | ||
.O..O..#.O#.OO.O#.....O.O..#O..O.O.....#...OO...O#.##O........#.O.........O#...OOO...#O###...O..#... | ||
OO.#O....O.....#...#.O...O...O..#....O#....##....O.OOO.O...#.OO.O...O.O#O.....#.......O........O..O. | ||
..O#O........O.O..O..#.##......#..#.OO......#.....#.O...#.#.OO#O.#..O....O#.O#..O.#..O#...O.O...O#.. | ||
O##..O.#..#...###.###O##....#OOO..O.#.O....O.OO#O...O..O.........O##....OO.O....#....OO...O......... | ||
OOO..O.#.O.O.O...#.##..#.##.#...O#O..O.#.....#..O.#....O..O....O.....O...O..#.O.##.O#..O.....O...O.. | ||
#.###.OO.....O..O..O...O.O#..#....####..#.......#.#.....O#O...O..O##O.#..#OOO.O#...O..O....#O.#....# | ||
.O#O.....O.#.#..OO....#.....#......#.....O.............O#..O..OOO#O#.OO#..O...O##..O.##O#...O#O.#..# | ||
.#...#.O.#..O..#.....OO..#O#....#.O.#.#..O#...#...#..OO#....O#..##O.............O...O..#O..O..O.#O.. | ||
.....##OO.O..#..O#...O...O.#.#.##.O....OOO.#O..O.O.O##....O.O.#.O...OO.O...###O#.O.OO#..O....O...... | ||
.O#O..#.O...#.#O...O##....O..#....O.O#.OO#O#O..O...O.#...O.O.O...O#..OO.O....#..#...OO..O.O.......#. | ||
O.O........OO...#.##..O........O....OO...O#..O.#OO.....O...O.O.......O....#O#O..O.OO............O..# | ||
...#O...#O.O...O.##..O....OO#.##.....#...O....O.....#O..OO.#.O...O.....OO...#OO#......#O##O..O....OO | ||
..#...O#.O..O.....#.....#...#..#..O....O...#......#..#..O#OO.....#O...O...#......O.#.###..#....O..#. | ||
.O.....#O.O......O#.O..O.##.O...O#OO..........O..###..#....O....O.....#..O#.O.....###......#..###..O | ||
.O..#.....#..##.##.O......O..O.O...O.O.......#.O..#....#.....O.#.O............OO#...O##...O.O#.O.... | ||
.#.O.O#.#.#O.O..#....O.O..#.....#.#OO.O...##.OO..O.O......##.......O.O###.O..O.....#..OO#...#....#.. | ||
....#.#...#...#.OO..#..........OO......O#..#O##...OO.#..O.#..O.O.OO.....O#O.#..####.OO...#.O..#O..O. | ||
O.O..O..OOOO....O..O.#.........O....O.#O.#O...O.#....#..O.#..OO.O.#.O#...#....##..#...........#.O#.O | ||
.......O....#OOO.#.O.......#O#O....#.....OOO.#..#OO#..O.#O##O.O........O.#.O.OOO.....O......#.#....O | ||
..O.....OO.#.#.O.....O##....O...OO......##..O#O.#O.O..#O.#.#....O...O.#.#..O.#.#OO..##....O......O.. | ||
..O#.#O.O.O..#...#O....O#O...O.........#.O...O#..O###O.....O..#.O....O#OO...O..O....OO#..#...#.....O | ||
.O.#....OO..O.O...O....#..........OO.##OO##OOO.##OOO..O...#...##....O..O#...O.#O..#.#O.O.....O.##.#. | ||
....OO#O.........OO.#.O.O.O.#O.O.##..............OO..........O...O.......O..O##.O..#..#...O........O | ||
O.....#.....O#.#..#.O...O....##O#O.#.##...#..#........O#...#....##.O...#..O......O..OO#....OO.....#. | ||
...O...#.#O.#..O...........O.O###.O.O...#..#O..O.OO..O..##O..#..#O#.O.OO.#O......O.O....#O.#..O...O# | ||
..O......#...##.#..OOO....#..#.#.OOO....#.#OO#.#O.#.O.#...#.O.OOO.#...#..O..O..#..#.O....O##..O..#.. | ||
.##..OO...#...#O#.O.....O#...#....O...............#..O#.#.#..OO.#..O.O#.O.....#OO.O.#.O..##...#.O#.O | ||
..#.O.#...O..##.O#.O....O.O.O#O#.OO....#...#.O.......O#...#O........O.O.O.O.OO.#O........O..OO.O#... | ||
...#..#OO#...#..#OO.#..#.O.#..OO..O.O....O##..#...OO..O....O#O...#....#...#O#.........O#OO.O...O.#.. | ||
.O#OOO..#....O..#.O..#..OO##..OOO#O#O#O..#.O#.#......#.........##O..O...O#.....O.......###O#O.....OO | ||
O..OO....O.#..O#.#O#....O.O...##O...#O#O#...O.O...O.O#O.......#..#...OO...#O.#...O#...#.........O##. | ||
...O.#O.#...O............#...O......##.......O..##O.O.OO.O.O....#O#..#O...#........OO.O..##.O.#.#O.# | ||
OO..O...#.....O..#OO..OO..#....#....O.O.#O.O.#O.OO...#..#OO..O#OO#.OO.....O...OO.O.O.....OO.#OO...O# | ||
.#...O...O.#OOO....O#...O.OO.##O...##.##.....O.O..#.....#O......O#...###O.#OO.#O.O.O...#.....#.OOO.. |
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,112 @@ | ||
using System.Text; | ||
using Lens = (string Label, int FocalLength); | ||
|
||
namespace AdventOfCode.Day15; | ||
|
||
public sealed class Day15(ITestOutputHelper output) : TestBase(output) | ||
{ | ||
protected override string Day => "Day15"; | ||
|
||
private readonly string[] _example = | ||
[ | ||
"rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7" | ||
]; | ||
|
||
[Fact] | ||
public void ExampleOne() => CalculateHASH(_example).Should().Be(1320); | ||
|
||
[Fact] | ||
public void ExampleTwo() => CalculateHASHMAP(_example).Should().Be(145); | ||
|
||
[Fact] | ||
public void PartOne() => WriteOutput(CalculateHASH(Input)); | ||
|
||
[Fact] | ||
public void PartTwo() => WriteOutput(CalculateHASHMAP(Input)); | ||
|
||
private static int CalculateHASHMAP(string[] input) | ||
{ | ||
var boxes = Enumerable.Range(0, 256) | ||
.Select(i => new Box(i + 1)) | ||
.ToArray(); | ||
var steps = input.SelectMany(i => i.Split(",")); | ||
foreach (var step in steps) | ||
{ | ||
var label = string.Empty; | ||
if (step.Contains('-')) | ||
{ | ||
label = step[0..^1]; | ||
var hash = Hash(label); | ||
boxes[hash].RemoveLens(label); | ||
continue; | ||
} | ||
|
||
var split = step.Split('='); | ||
label = split[0]; | ||
var focalLength = int.Parse(split[1]); | ||
var h = Hash(label); | ||
boxes[h].AddLens(label, focalLength); | ||
} | ||
|
||
return boxes.Select(b => b.FocusingPower()).Sum(); | ||
} | ||
|
||
private static int CalculateHASH(string[] input) => | ||
input | ||
.SelectMany(i => i.Split(',')) | ||
.Select(Hash) | ||
.Sum(); | ||
|
||
private static int Hash(string step) | ||
{ | ||
var current = 0; | ||
foreach (var character in Encoding.ASCII.GetBytes(step)) | ||
{ | ||
current = (current + character) * 17 % 256; | ||
} | ||
return current; | ||
} | ||
|
||
private sealed class Box(int number) | ||
{ | ||
public int Number { get; } = number; | ||
public LinkedList<Lens> Lenses { get; } = []; | ||
|
||
public void AddLens(string label, int focalLength) | ||
{ | ||
var lens = Lenses.FindFirst(l => l.Label == label); | ||
if (lens is not null) | ||
{ | ||
lens.Value = (label, focalLength); | ||
return; | ||
} | ||
Lenses.AddLast(new LinkedListNode<Lens>((label, focalLength))); | ||
} | ||
|
||
public int FocusingPower() => | ||
Lenses.Select((l, i) => Number * (i + 1) * l.FocalLength).Sum(); | ||
|
||
public void RemoveLens(string label) | ||
{ | ||
var lens = Lenses.FindFirst(l => l.Label == label); | ||
if (lens is not null) | ||
Lenses.Remove(lens); | ||
} | ||
} | ||
} | ||
|
||
public static class LinkedListExtensions | ||
{ | ||
public static LinkedListNode<T>? FindFirst<T>(this LinkedList<T> linkedList, Func<T, bool> predicate) | ||
{ | ||
var cur = linkedList.First; | ||
while (cur is not null) | ||
{ | ||
if (predicate.Invoke(cur.Value)) | ||
return cur; | ||
cur = cur.Next; | ||
} | ||
|
||
return null; | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.