-
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
264 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,50 @@ | ||
.............C.7..................G..0...y........ | ||
..................7................C.............. | ||
....................................0......W....y. | ||
.......................................D..W....... | ||
..........u....................................... | ||
..................................4.......D0...j.. | ||
.....................................D............ | ||
................O.....C................G.......... | ||
............F.....................C............... | ||
......u..........F.................4.......y...... | ||
..........X..........5....4...........1........... | ||
..........F...........5X...................3...... | ||
.............F.............................j.3.... | ||
.................u..............X................. | ||
............................7..................... | ||
.................................................. | ||
..........................5.....j2.........4...... | ||
....d.....................y...................j1.. | ||
.................................................. | ||
............................Y.e................... | ||
.................d...X...............J...........e | ||
.............d.................................... | ||
..............................Y..............1.... | ||
.........................................Y........ | ||
......................W......8..f...J.........3... | ||
.......w.............J............................ | ||
...................................U.....f......e. | ||
.................................Of....e....t...1. | ||
.......g..........d......s........................ | ||
................G................f................ | ||
.....................................O............ | ||
...g........................T.....U............... | ||
......................s..........T.............G.. | ||
................................s.......8......... | ||
.....9........g...........o...U............E...... | ||
............g............................t....o... | ||
...........................................6....E. | ||
.....................s......x........6....E....... | ||
..........w.9................x............t....... | ||
...........9........w...........J.....6o.......... | ||
.............................................o.... | ||
..........S................U...................... | ||
.......S..2..........c........T.O....t............ | ||
.....2...S.....c...................T.............. | ||
..................x.......................8....... | ||
....9............................................. | ||
...wS.....................................6....... | ||
................2........................8........ | ||
.................................................. | ||
.................x....c........................E.. |
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,143 @@ | ||
using System.Numerics; | ||
using AdventOfCode.Utilities; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace AdventOfCode.Year2024; | ||
|
||
public class AoC2024Day08 | ||
{ | ||
// input is negative when line is empty | ||
private readonly char[,] map; | ||
|
||
public AoC2024Day08(string? customInput = null) | ||
{ | ||
var content = (customInput ?? EmbeddedInput.ReadAllText("Year2024/2024_08_input.txt")); | ||
var lines = content.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); | ||
map = new char[lines.Length, lines[0].Length]; | ||
for (int i = 0; i < lines.Length; i++) | ||
{ | ||
var line = lines[i]; | ||
for (int j = 0; j < line.Length; j++) | ||
{ | ||
map[i, j] = line[j]; | ||
} | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public long Solution1() | ||
{ | ||
var antennasPerType = new Dictionary<char, List<(int x, int y)>>(); | ||
for (int x = 0; x < map.GetLength(0); x++) | ||
{ | ||
for (int y = 0; y < map.GetLength(1); y++) | ||
{ | ||
var antennaType = map[x, y]; | ||
if (antennaType == '.') | ||
{ | ||
continue; | ||
} | ||
|
||
if (!antennasPerType.TryGetValue(antennaType, out var antennas)) | ||
{ | ||
antennas = new List<(int x, int y)>(); | ||
antennasPerType[antennaType] = antennas; | ||
} | ||
|
||
antennas.Add((x, y)); | ||
} | ||
} | ||
|
||
var antiNodes = new HashSet<(int x, int y)>(); | ||
|
||
foreach (var (_, coords) in antennasPerType) | ||
{ | ||
foreach (var coordA in coords) | ||
{ | ||
foreach (var coordB in coords) | ||
{ | ||
if (coordA == coordB) | ||
{ | ||
continue; | ||
} | ||
|
||
var diffX = coordA.x - coordB.x; | ||
var diffY = coordA.y - coordB.y; | ||
var antiNode = (x: coordA.x + diffX, y: coordA.y + diffY); | ||
|
||
|
||
if (antiNode.x < 0 || antiNode.x >= map.GetLength(0) || antiNode.y < 0 || | ||
antiNode.y >= map.GetLength(1)) | ||
{ | ||
continue; | ||
} | ||
|
||
antiNodes.Add(antiNode); | ||
} | ||
} | ||
} | ||
|
||
return antiNodes.Count; | ||
} | ||
|
||
[Benchmark] | ||
public long Solution2() | ||
{ | ||
var antennasPerType = new Dictionary<char, List<(int x, int y)>>(); | ||
for (int x = 0; x < map.GetLength(0); x++) | ||
{ | ||
for (int y = 0; y < map.GetLength(1); y++) | ||
{ | ||
var antennaType = map[x, y]; | ||
if (antennaType == '.') | ||
{ | ||
continue; | ||
} | ||
|
||
if (!antennasPerType.TryGetValue(antennaType, out var antennas)) | ||
{ | ||
antennas = new List<(int x, int y)>(); | ||
antennasPerType[antennaType] = antennas; | ||
} | ||
|
||
antennas.Add((x, y)); | ||
} | ||
} | ||
|
||
var antiNodes = new HashSet<(int x, int y)>(); | ||
foreach (var (_, coords) in antennasPerType) | ||
{ | ||
if (coords.Count == 1) | ||
{ | ||
continue; | ||
} | ||
|
||
foreach (var coordA in coords) | ||
{ | ||
antiNodes.Add(coordA); | ||
foreach (var coordB in coords) | ||
{ | ||
if (coordA == coordB) | ||
{ | ||
continue; | ||
} | ||
|
||
var diffX = coordA.x - coordB.x; | ||
var diffY = coordA.y - coordB.y; | ||
|
||
var antiNode = (x: coordA.x + diffX, y: coordA.y + diffY); | ||
|
||
while (antiNode.x >= 0 && antiNode.x < map.GetLength(0) && antiNode.y >= 0 && | ||
antiNode.y < map.GetLength(1)) | ||
{ | ||
antiNodes.Add(antiNode); | ||
antiNode = (x: antiNode.x + diffX, y: antiNode.y + diffY); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
return antiNodes.Count; | ||
} | ||
} |
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,71 @@ | ||
using AdventOfCode.Year2024; | ||
using System; | ||
using NUnit.Framework; | ||
|
||
namespace AdventOfCodeTests.Year2024; | ||
|
||
[TestFixture] | ||
public class AoC2024Day08Tests { | ||
[Test] | ||
public void Solution1Examples() | ||
{ | ||
string input = """ | ||
............ | ||
........0... | ||
.....0...... | ||
.......0.... | ||
....0....... | ||
......A..... | ||
............ | ||
............ | ||
........A... | ||
.........A.. | ||
............ | ||
............ | ||
"""; | ||
|
||
long expect = 14; | ||
|
||
var instance = new AoC2024Day08(input); | ||
Assert.That(instance.Solution1(), Is.EqualTo(expect)); | ||
} | ||
|
||
[Test] | ||
public void Solution1Test() { | ||
var instance = new AoC2024Day08(); | ||
var result = instance.Solution1(); | ||
Assert.That(result, Is.EqualTo(222)); | ||
Console.WriteLine($"Result: {result}"); | ||
} | ||
|
||
[Test] | ||
public void Solution2Examples() { | ||
string input = """ | ||
............ | ||
........0... | ||
.....0...... | ||
.......0.... | ||
....0....... | ||
......A..... | ||
............ | ||
............ | ||
........A... | ||
.........A.. | ||
............ | ||
............ | ||
"""; | ||
|
||
long expect = 34; | ||
|
||
var instance = new AoC2024Day08(input); | ||
Assert.That(instance.Solution2(), Is.EqualTo(expect)); | ||
} | ||
|
||
[Test] | ||
public void Solution2Test() { | ||
var instance = new AoC2024Day08(); | ||
var result = instance.Solution2(); | ||
Assert.That(result, Is.EqualTo(884)); | ||
Console.WriteLine($"Result: {result}"); | ||
} | ||
} |