From ef9892502e05bef12a3bf4589920191421d90e71 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Fri, 24 Jan 2025 10:19:11 +0100 Subject: [PATCH] Add day 8 --- Days/Day8.cs | 169 ++++++++++++++++++++++++++++++++++++++++++++++++ Inputs/Day8.txt | 50 ++++++++++++++ 2 files changed, 219 insertions(+) create mode 100644 Days/Day8.cs create mode 100644 Inputs/Day8.txt diff --git a/Days/Day8.cs b/Days/Day8.cs new file mode 100644 index 0000000..b426507 --- /dev/null +++ b/Days/Day8.cs @@ -0,0 +1,169 @@ +using System.Collections.Frozen; +using System.Numerics; +using Spectre.Console; + +namespace AdventOfCode.Days; + +public class Day8 : Day +{ + public override int Number => 8; + public override string Name => "Resonant Collinearity"; + + private const int GridSize = 50; + + public override void RunPart1(bool display = true) + { + HashSet antinodePositions = []; + + var frequencyAntennas = ParseAntennas(); + + // Find antinodes for each frequency + foreach (var (_, antennas) in frequencyAntennas) + { + // Take antennas by pair to find antinodes + foreach (var firstAntenna in antennas) + { + foreach (var secondAntenna in antennas.Where(a => a != firstAntenna)) + { + var firstAntinode = firstAntenna + (firstAntenna - secondAntenna); + var secondAntinode = secondAntenna + (secondAntenna - firstAntenna); + + if (firstAntinode is { X: >= 0 and < GridSize, Y: >= 0 and < GridSize }) + { + antinodePositions.Add(firstAntinode); + } + + if (secondAntinode is { X: >= 0 and < GridSize, Y: >= 0 and < GridSize }) + { + antinodePositions.Add(secondAntinode); + } + } + } + } + + if (display) + { + AnsiConsole.MarkupLine($"[green]Unique antinode positions count: [yellow]{antinodePositions.Count}[/][/]"); + } + } + + public override void RunPart2(bool display = true) + { + HashSet antinodePositions = []; + + var frequencyAntennas = ParseAntennas(); + + // Find antinodes for each frequency + foreach (var (_, antennas) in frequencyAntennas) + { + // Take antennas by pair to find antinodes + foreach (var firstAntenna in antennas) + { + foreach (var secondAntenna in antennas.Where(a => a != firstAntenna)) + { + // Always add antinodes on antennas if a pair is formed + antinodePositions.Add(firstAntenna); + antinodePositions.Add(secondAntenna); + + // Compute potential antinode positions + var firstAntinodeOffset = firstAntenna - secondAntenna; + var secondAntinodeOffset = secondAntenna - firstAntenna; + + for (var i = 1; ; i++) + { + var firstAntinode = firstAntenna + (i * firstAntinodeOffset); + + if (firstAntinode is { X: >= 0 and < GridSize, Y: >= 0 and < GridSize }) + { + antinodePositions.Add(firstAntinode); + } + else + { + break; + } + } + + for (var i = 1; ; i++) + { + var secondAntinode = secondAntenna + (i * secondAntinodeOffset); + + if (secondAntinode is { X: >= 0 and < GridSize, Y: >= 0 and < GridSize }) + { + antinodePositions.Add(secondAntinode); + } + else + { + break; + } + } + } + } + } + + if (display) + { + AnsiConsole.MarkupLine($"[green]Unique antinode positions count: [yellow]{antinodePositions.Count}[/][/]"); + } + } + + private FrozenDictionary> ParseAntennas() + { + var antennas = new Dictionary>(); + + var y = 0; + foreach (var line in Input.AsSpan().EnumerateLines()) + { + var x = 0; + + foreach (var frequency in line) + { + if (frequency is '.') + { + x++; + + continue; + } + + if (!antennas.TryGetValue(frequency, out var list)) + { + list = []; + antennas[frequency] = list; + } + + list.Add(new Point(x, y)); + + x++; + } + + y++; + } + + return antennas.ToFrozenDictionary(); + } + + private readonly record struct Point(int X, int Y) + : IAdditionOperators, + ISubtractionOperators, + IMultiplyOperators + { + public static Point operator +(Point left, Point right) + { + return new Point(left.X + right.X, left.Y + right.Y); + } + + public static Point operator -(Point left, Point right) + { + return new Point(left.X - right.X, left.Y - right.Y); + } + + public static Point operator *(Point left, int right) + { + return new Point(left.X * right, left.Y * right); + } + + public static Point operator *(int left, Point right) + { + return new Point(right.X * left, right.Y * left); + } + } +} \ No newline at end of file diff --git a/Inputs/Day8.txt b/Inputs/Day8.txt new file mode 100644 index 0000000..9fc6357 --- /dev/null +++ b/Inputs/Day8.txt @@ -0,0 +1,50 @@ +.....wV....q.....................................n +.......w......q.h.....Vn.........................D +............w.S..G.....................DT......... +......S........h......e..T.....y......D........... +......m.......Ae.......T........o................. +....m....S........................................ +...m..........................n........8.......... +.........2...G......................n............. +..2........V.......h................Q............. +............................o..................... +.Z......I..U....e...u.....G....o.................. +...N..G.........................................y. +.....I............q.......h...................s... +......U........qI....o.V..Rz........8........k.... +......d.Z.........................R.......8y...... +.........e..............T.....l................... +.......2.........................u...R............ +.....d.............................Q.............. +...................v.....................s.Q....M. +........2..........4.....................8..7.k... +...........x..N..................A..........k..... +...........ZN...........v...............K......... +...d.......N.....................Ky.6............. +...........................l6..................... +....L....g.................4.......k..K.......0... +..............L...........4R................s..... +U......r..............H.4......................... +.......U.............a.......H.............u...... +......xY...............l.......................... +...................................6..u........... +........Y......L......l............0.............. +......9..L...........A.....v..HEa........K........ +..................v........6.EX.............z..... +d..Y.............m......A......................... +......................a.i......M...........z...... +...................g.......................0...... +...............................H.........i........ +..........3................W........E...i...0..... +.................t.a....g.................5....... +.r...t...........................7.....5.......... +....................................7....5........ +....................g.Y...wMz..................... +9..........O....3................W.7..E..XD...1... +t..............3.x.....9..........W.M............. +...9............W................................. +Z.............x................X.i......5......... +...........3.....................................1 +...................O.......s....X................. +..............r................................... +..........................O.................1..... \ No newline at end of file