Day 8. Set enableResonantHarmonics to true for part 2
parent
24b7f4d848
commit
3ce503b8ce
|
@ -0,0 +1,104 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection.Emit;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Advent24
|
||||
{
|
||||
internal class Day8
|
||||
{
|
||||
public Day8()
|
||||
{
|
||||
String fileData = System.IO.File.ReadAllText(@"..\..\..\inputd8.txt");
|
||||
|
||||
// set this to false for part 1
|
||||
Boolean enableResonantHarmonics = true;
|
||||
|
||||
// get width of line to be able to navigate the map block in
|
||||
// both dimensions
|
||||
Int32 mapWidth = fileData.IndexOf('\n');
|
||||
Int32 mapWidthIncludingNewline = mapWidth + 1;
|
||||
String pattern = @"[A-Za-z0-9]";
|
||||
|
||||
Regex r = new(pattern, RegexOptions.Multiline, TimeSpan.FromMilliseconds(150));
|
||||
|
||||
Match m = r.Match(fileData);
|
||||
|
||||
// list of linear-integer locations of antennas indexed by antenna type (a single alphanumeric character)
|
||||
Dictionary<Char, List<Int32>> antennaLocationsMap = [];
|
||||
|
||||
while (m.Success)
|
||||
{
|
||||
if (!antennaLocationsMap.TryGetValue(m.Groups[0].Value[0], out List<int>? antennaLocations))
|
||||
antennaLocationsMap.Add(m.Groups[0].Value[0], [m.Groups[0].Index]);
|
||||
else
|
||||
antennaLocationsMap[m.Groups[0].Value[0]].Add(m.Groups[0].Index);
|
||||
|
||||
// Console.WriteLine("antennaLocationsMap[{0:d}] = {1:d}", m.Groups[0].Value[0], antennaLocationsMap[m.Groups[0].Value[0]].Last());
|
||||
|
||||
m = m.NextMatch();
|
||||
}
|
||||
|
||||
// set of linear-integer locations of antinodes indexed by antenna type (single-character alphanumeric)
|
||||
Dictionary<Char, HashSet<Int32>> antinodeLocationsMap = [];
|
||||
|
||||
// set of linear-integer locations of antinodes overall
|
||||
HashSet<Int32> overallAntinodeLocations = [];
|
||||
|
||||
foreach (Char antennaType in antennaLocationsMap.Keys)
|
||||
{
|
||||
if (!antinodeLocationsMap.TryGetValue(antennaType, out HashSet<int>? antinodeLocations))
|
||||
antinodeLocationsMap.Add(antennaType, []);
|
||||
|
||||
// for each (ordered) pair of antennaLocations of the same antenna type, we'll construct an antinode
|
||||
// and then add it to the antinodeLocationsMap (keyed by antenna type)
|
||||
foreach(Int32 antennaLocation1 in antennaLocationsMap[antennaType])
|
||||
foreach(Int32 antennaLocation2 in antennaLocationsMap[antennaType])
|
||||
{
|
||||
if (antennaLocation1 == antennaLocation2)
|
||||
continue;
|
||||
|
||||
Int32 antennaLocationDifference = antennaLocation2 - antennaLocation1;
|
||||
Int32 antennaLocationXDifference = (antennaLocation2 % mapWidthIncludingNewline) - (antennaLocation1 % mapWidthIncludingNewline);
|
||||
Int32 antinodeLocation = antennaLocation2;
|
||||
do
|
||||
{
|
||||
Int32 antinodePreviousLocationX = antinodeLocation % mapWidthIncludingNewline;
|
||||
antinodeLocation += antennaLocationDifference;
|
||||
Int32 antinodeLocationXDifference = (antinodeLocation % mapWidthIncludingNewline) - antinodePreviousLocationX;
|
||||
|
||||
// bounds checking
|
||||
if (
|
||||
antinodeLocation < 0 ||
|
||||
antinodeLocation >= fileData.Length ||
|
||||
fileData[antinodeLocation] == '\n' ||
|
||||
// x-direction having changed implies we crossed the right/left boundary
|
||||
((antennaLocationXDifference > 0) != (antinodeLocationXDifference > 0))
|
||||
)
|
||||
break;
|
||||
|
||||
antinodeLocationsMap[antennaType].Add(antinodeLocation);
|
||||
} while (enableResonantHarmonics);
|
||||
|
||||
if(enableResonantHarmonics)
|
||||
{
|
||||
antinodeLocationsMap[antennaType].Add(antennaLocation1);
|
||||
antinodeLocationsMap[antennaType].Add(antennaLocation2);
|
||||
}
|
||||
}
|
||||
|
||||
// adding to the overall antinode location set
|
||||
overallAntinodeLocations = [.. overallAntinodeLocations, .. antinodeLocationsMap[antennaType]];
|
||||
}
|
||||
|
||||
Int32 numberOfUniqueAntinodeLocations = overallAntinodeLocations.Count;
|
||||
Console.WriteLine("numberOfUniqueAntinodeLocations = {0:d}", numberOfUniqueAntinodeLocations);
|
||||
|
||||
// to keep the console window from closing
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -13,7 +13,8 @@ namespace Advent24
|
|||
//_ = new Day4();
|
||||
//_ = new Day5();
|
||||
//_ = new Day6();
|
||||
_ = new Day7();
|
||||
//_ = new Day7();
|
||||
_ = new Day8();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
.....................U.........w..................
|
||||
l.................................................
|
||||
...........o.a................U...w...............
|
||||
............................................W.....
|
||||
..........T....................s.............7....
|
||||
.............................................W....
|
||||
.........T..............4....n.d.H.........5......
|
||||
......T.....oj...U.....n...w......H...........z...
|
||||
.G..x..........................E.....V..H.........
|
||||
.........a....................d....s.......7w.....
|
||||
...j....r.............o.............V.......d...W.
|
||||
.......r..J.Goa.U...............n................z
|
||||
.........Jj.........M..........Pv.................
|
||||
...J...........t..3..M..............sLV...........
|
||||
...................t................n.............
|
||||
....r...........X...........M........v............
|
||||
...x....t......I......a.PM...............W........
|
||||
...........1.Bj....I........vO.h.dL...............
|
||||
.........6....Rr......B...X........h..5v.L..z.....
|
||||
......1G...........x.....3B.......5...............
|
||||
.................B....0..........4..E.............
|
||||
.....................X.....5..h....P....f.....D...
|
||||
.......1........J.....eK..........................
|
||||
..................I....R....K...........k.........
|
||||
......G..................O........................
|
||||
...........H...9...............K8.P.4..k..E.......
|
||||
............1....3.............8.F.............f..
|
||||
.........................4........................
|
||||
.l...........X............9.......................
|
||||
....N.................R...t.e.....................
|
||||
...g............3..R.........e....h.........f.....
|
||||
...........................e......i...............
|
||||
................2...I.7..9..O.....s.........k.....
|
||||
....................6...9E.............F..O.......
|
||||
........................KN........................
|
||||
.......g......................Z.........F..f...Y..
|
||||
...........................A....i.................
|
||||
...........6g...b........8.......y.....S..........
|
||||
..l.....6.....m...............8...................
|
||||
....u..m...b...............p...A..................
|
||||
..............b.p........................k........
|
||||
....m......2...........Z..y....i..................
|
||||
........g2.....b.........i....D..ZF...............
|
||||
......2.0...........p............N..........A.....
|
||||
...m.............S...y........A...Z...N...........
|
||||
..S..l..........................................Y.
|
||||
........S....0u.................y......DY.........
|
||||
...........0.........................D............
|
||||
.................u...................p...Y........
|
||||
.......u..........................................
|
|
@ -0,0 +1,12 @@
|
|||
............
|
||||
........0...
|
||||
.....0......
|
||||
.......0....
|
||||
....0.......
|
||||
......A.....
|
||||
............
|
||||
............
|
||||
........A...
|
||||
.........A..
|
||||
............
|
||||
............
|
Loading…
Reference in New Issue