diff --git a/2024/day-08/day-08.pro b/2024/day-08/day-08.pro new file mode 100644 index 0000000..5360f1e --- /dev/null +++ b/2024/day-08/day-08.pro @@ -0,0 +1,7 @@ +TEMPLATE = app +CONFIG += console c++20 +CONFIG -= app_bundle +CONFIG -= qt + +SOURCES += \ + main.cpp diff --git a/2024/day-08/input b/2024/day-08/input new file mode 100644 index 0000000..5189c13 --- /dev/null +++ b/2024/day-08/input @@ -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 diff --git a/2024/day-08/main.cpp b/2024/day-08/main.cpp new file mode 100644 index 0000000..5815a33 --- /dev/null +++ b/2024/day-08/main.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include + +using namespace std; + +void readInput(vector &antennaMap, map>> &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 location, const vector &map) +{ + return (location.first >= 0 && location.first < map.size()) + && (location.second >= 0 && location.second < map[0].size()); +} + +vector> getAntinodes(pair antenna1, + pair antenna2, + const vector &antennaMap) +{ + if (antenna1 == antenna2) { + return {}; + } + + pair directionVector = {antenna2.first - antenna1.first, + antenna2.second - antenna1.second}; + + pair antinode1 = {antenna2.first + directionVector.first, + antenna2.second + directionVector.second}; + pair antinode2 = {antenna1.first - directionVector.first, + antenna1.second - directionVector.second}; + + vector> antinodes; + if (isInBounds(antinode1, antennaMap)) { + antinodes.push_back(antinode1); + } + if (isInBounds(antinode2, antennaMap)) { + antinodes.push_back(antinode2); + } + return antinodes; +} + +int main() +{ + vector antennaMap; + map>> antennas; + readInput(antennaMap, antennas); + + set> uniqueAntinodes; + + for (auto antennasWithSameFrequency : antennas) { + for (auto antenna1 : antennasWithSameFrequency.second) { + for (auto antenna2 : antennasWithSameFrequency.second) { + vector> foundAntinodes = getAntinodes(antenna1, antenna2, antennaMap); + for (pair antinode : foundAntinodes) { + uniqueAntinodes.insert(antinode); + } + } + } + } + + // Part one + cout << "Number of unique antinodes: " << uniqueAntinodes.size() << endl; +} diff --git a/2024/day-08/short-input b/2024/day-08/short-input new file mode 100644 index 0000000..78a1e91 --- /dev/null +++ b/2024/day-08/short-input @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............