Solve 2024 day 8 part 1

This commit is contained in:
SebastianStork 2025-01-02 22:48:11 +01:00
parent a9afc371bb
commit ffc564f048
4 changed files with 148 additions and 0 deletions

7
2024/day-08/day-08.pro Normal file
View file

@ -0,0 +1,7 @@
TEMPLATE = app
CONFIG += console c++20
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp

50
2024/day-08/input Normal file
View file

@ -0,0 +1,50 @@
........................E...j......W..........L...
............................O........E.........L..
..q......O...........l....................K.......
............q...................HM......W.........
................................1..H...........IW.
....................5.............................
..........k........M...wl............6............
.....O.......w...k.....5.8..l......K.........o.6..
.......k....w.........5.........R.....o........K..
.....q..X..............j........E...I.........K...
............O..........E........................H.
................Mn.h2.w.p....................H....
..................p.......a............j.....L....
.....X...l.p.....................m.........W..6...
..Xq................A..................R..m.......
.........................i..........a..........R..
...........u.....................a........I.....2.
k..............A..n.........R.................o...
................n.................Qo..............
..........u.A.........h........2..................
...5.......Y.....p...............iN...............
1...x.....................i.......................
........M..............2.....Qi...................
...............................I..e...............
......u......A...........m..........h.............
.......1...........U.............Qm.......j.......
.......X.......................................9..
.....u........U.......Y...........................
.............................h.e..................
..................4....e......Q.....L....N........
.1..................4.......................y8....
.........Y................................8.N.....
............P.0J...........3..........8y..........
....V3P..........J................................
............U..P...7x...........e.................
....................J...............r...9.........
.........0.V......Y...............................
...............V.4................................
..........V..........................n............
..............v........7..........................
...........U..........J.......7...................
.....v........7..........................a........
.......................................r..........
...........0.......x................y.............
............6..v.x.....................N..........
...........P......................................
........3.......................r......4..........
..............3......................y............
................................................9.
.................................................9

79
2024/day-08/main.cpp Normal file
View file

@ -0,0 +1,79 @@
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
void readInput(vector<string> &antennaMap, map<char, set<pair<int, int>>> &antennas)
{
string line;
for (size_t row = 0; getline(cin, line); row++) {
for (size_t col = 0; col < line.size(); col++) {
char frequency = line[col];
if (frequency != '.') {
if (antennas.count(frequency)) {
antennas.at(frequency).insert({row, col});
} else {
antennas.insert({frequency, {{row, col}}});
}
}
}
antennaMap.push_back(line);
}
}
bool isInBounds(pair<int, int> location, const vector<string> &map)
{
return (location.first >= 0 && location.first < map.size())
&& (location.second >= 0 && location.second < map[0].size());
}
vector<pair<int, int>> getAntinodes(pair<int, int> antenna1,
pair<int, int> antenna2,
const vector<string> &antennaMap)
{
if (antenna1 == antenna2) {
return {};
}
pair<int, int> directionVector = {antenna2.first - antenna1.first,
antenna2.second - antenna1.second};
pair<int, int> antinode1 = {antenna2.first + directionVector.first,
antenna2.second + directionVector.second};
pair<int, int> antinode2 = {antenna1.first - directionVector.first,
antenna1.second - directionVector.second};
vector<pair<int, int>> antinodes;
if (isInBounds(antinode1, antennaMap)) {
antinodes.push_back(antinode1);
}
if (isInBounds(antinode2, antennaMap)) {
antinodes.push_back(antinode2);
}
return antinodes;
}
int main()
{
vector<string> antennaMap;
map<char, set<pair<int, int>>> antennas;
readInput(antennaMap, antennas);
set<pair<int, int>> uniqueAntinodes;
for (auto antennasWithSameFrequency : antennas) {
for (auto antenna1 : antennasWithSameFrequency.second) {
for (auto antenna2 : antennasWithSameFrequency.second) {
vector<pair<int, int>> foundAntinodes = getAntinodes(antenna1, antenna2, antennaMap);
for (pair<int, int> antinode : foundAntinodes) {
uniqueAntinodes.insert(antinode);
}
}
}
}
// Part one
cout << "Number of unique antinodes: " << uniqueAntinodes.size() << endl;
}

12
2024/day-08/short-input Normal file
View file

@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............