Skip to content

Commit

Permalink
Adds 13-21.
Browse files Browse the repository at this point in the history
  • Loading branch information
mholland committed Dec 27, 2023
1 parent 42aae1e commit bee5079
Show file tree
Hide file tree
Showing 18 changed files with 2,943 additions and 6 deletions.
4 changes: 4 additions & 0 deletions 2023/AdventOfCode/AdventOfCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<Content Include="./Day*/*.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

<Content Include="xunit.runner.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
10 changes: 4 additions & 6 deletions 2023/AdventOfCode/Day13/Day13.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ private static (int Summary, int Fixed) SummariseTerrain(string input)
return (Summary: result.Sum(r => r.Summary), Fixed: result.Sum(r => r.Fixed));
}


private sealed class Grid
{
private readonly string[] _rows;
Expand Down Expand Up @@ -113,13 +112,12 @@ public static int FindNewSummary(string[] toCheck, int originalSummary, int summ
{
var candidates = new List<(int Position, int Index)>();
for (var p0 = 0; p0 < toCheck.Length; p0++)
for (var p1 = p0 + 1; p1 < toCheck.Length; p1++)
{
for (var p1 = p0 + 1; p1 < toCheck.Length; p1++)
{
var (differs, index) = DiffersByOne(toCheck[p0], toCheck[p1]);
if (differs) candidates.Add((p0, index));
}
var (differs, index) = DiffersByOne(toCheck[p0], toCheck[p1]);
if (differs) candidates.Add((p0, index));
}


foreach (var (pos, index) in candidates.Distinct())
{
Expand Down
122 changes: 122 additions & 0 deletions 2023/AdventOfCode/Day14/Day14.cs
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)));
}
100 changes: 100 additions & 0 deletions 2023/AdventOfCode/Day14/input.txt
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..
112 changes: 112 additions & 0 deletions 2023/AdventOfCode/Day15/Day15.cs
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;
}
}
1 change: 1 addition & 0 deletions 2023/AdventOfCode/Day15/input.txt

Large diffs are not rendered by default.

Loading

0 comments on commit bee5079

Please sign in to comment.