From 463dc27ec82932aaba952182ad52047a25d45b6d Mon Sep 17 00:00:00 2001 From: Rainer Bernhardt Date: Sun, 15 Dec 2024 18:43:04 +0100 Subject: [PATCH] day 8 2024 --- AdventOfCode/Year2024/2024_08_input.txt | 50 ++++++ AdventOfCode/Year2024/AoC2024Day08.cs | 143 ++++++++++++++++++ .../Year2024/AoC2024Day08Tests.cs | 71 +++++++++ 3 files changed, 264 insertions(+) create mode 100644 AdventOfCode/Year2024/2024_08_input.txt create mode 100644 AdventOfCode/Year2024/AoC2024Day08.cs create mode 100644 AdventOfCodeTests/Year2024/AoC2024Day08Tests.cs diff --git a/AdventOfCode/Year2024/2024_08_input.txt b/AdventOfCode/Year2024/2024_08_input.txt new file mode 100644 index 0000000..cfec3f5 --- /dev/null +++ b/AdventOfCode/Year2024/2024_08_input.txt @@ -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.. \ No newline at end of file diff --git a/AdventOfCode/Year2024/AoC2024Day08.cs b/AdventOfCode/Year2024/AoC2024Day08.cs new file mode 100644 index 0000000..ce9631d --- /dev/null +++ b/AdventOfCode/Year2024/AoC2024Day08.cs @@ -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>(); + 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>(); + 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; + } +} \ No newline at end of file diff --git a/AdventOfCodeTests/Year2024/AoC2024Day08Tests.cs b/AdventOfCodeTests/Year2024/AoC2024Day08Tests.cs new file mode 100644 index 0000000..fc0985d --- /dev/null +++ b/AdventOfCodeTests/Year2024/AoC2024Day08Tests.cs @@ -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}"); + } +} \ No newline at end of file