advent2024

Advent of Code 2024
git clone git://bsandro.tech/advent2024
Log | Files | Refs

commit f4934645b1133917a1f27ec042a2b338a0e19099
parent 2cd8129387019cd02e58cc2fb6c774b059f2588e
Author: bsandro <email@bsandro.tech>
Date:   Sun,  8 Dec 2024 19:53:30 +0200

Day 08 p2

Diffstat:
Mday08/main.cpp | 61++++++++++++++++++++++++++++++++++++++++++++-----------------
Aday08/test2.txt | 10++++++++++
2 files changed, 54 insertions(+), 17 deletions(-)

diff --git a/day08/main.cpp b/day08/main.cpp @@ -15,7 +15,7 @@ typedef std::map<char, std::vector<Vec2>> Data; // I got too lazy these days [[ maybe_unused ]] static int sFieldSize = 0; -std::vector<Vec2> calc_an(Vec2 a1, Vec2 a2) { +std::vector<Vec2> calc_an1(Vec2 a1, Vec2 a2) { std::vector<Vec2> out; Vec2 d1(a1.first-a2.first, a1.second-a2.second); Vec2 d2(-d1.first, -d1.second); @@ -30,14 +30,43 @@ std::vector<Vec2> calc_an(Vec2 a1, Vec2 a2) { return out; } -std::set<Vec2> calc_antinodes(const std::vector<Vec2> &antennas) { +std::set<Vec2> calc_antinodes1(const std::vector<Vec2> &antennas) { std::set<Vec2> out; for (auto it1=antennas.begin(); it1!=antennas.end(); it1++) { for (auto it2=it1+1; it2!=antennas.end(); it2++) { - auto [x1, y1] = *it1; - auto [x2, y2] = *it2; - std::printf("%d:%d -> %d:%d\n", x1, y1, x2, y2); - std::vector<Vec2> an = calc_an(*it1, *it2); + std::vector<Vec2> an = calc_an1(*it1, *it2); + for (auto &v:an) out.insert(v); + } + } + return out; +} + +std::vector<Vec2> calc_an2(Vec2 a1, Vec2 a2) { + std::vector<Vec2> out; + out.push_back(a1); + out.push_back(a2); + Vec2 d1(a1.first-a2.first, a1.second-a2.second); + Vec2 d2(-d1.first, -d1.second); + Vec2 p1(a1.first+d1.first, a1.second+d1.second); + Vec2 p2(a2.first+d2.first, a2.second+d2.second); + while (p1.first>=0&&p1.second>=0&&p1.first<sFieldSize&&p1.second<sFieldSize) { + out.push_back(p1); + p1.first+=d1.first; + p1.second+=d1.second; + } + while (p2.first>=0&&p2.second>=0&&p2.first<sFieldSize&&p2.second<sFieldSize) { + out.push_back(p2); + p2.first+=d2.first; + p2.second+=d2.second; + } + return out; +} + +std::set<Vec2> calc_antinodes2(const std::vector<Vec2> &antennas) { + std::set<Vec2> out; + for (auto it1=antennas.begin(); it1!=antennas.end(); it1++) { + for (auto it2=it1+1; it2!=antennas.end(); it2++) { + std::vector<Vec2> an = calc_an2(*it1, *it2); for (auto &v:an) out.insert(v); } } @@ -69,25 +98,23 @@ T read_file(const std::string &path) { int part1(Data &input [[ maybe_unused ]]) { std::set<Vec2> out; - std::cout << std::endl; for (auto &[k, v] : input) { - std::cout << "key:" << k << std::endl; - for (auto &p : v) { - std::cout << "[" << p.first << "," << p.second << "]"; - } - std::cout << std::endl; - std::set<Vec2> an = calc_antinodes(v); - std::printf("%ld antinodes:", an.size()); + std::set<Vec2> an = calc_antinodes1(v); for (auto &n:an) { - std::printf("[%d,%d]", n.first, n.second); out.insert(n); } - std::cout << std::endl; } return (int)out.size(); } int part2(Data &input [[ maybe_unused ]]) { - return 0; + std::set<Vec2> out; + for (auto &[k, v] : input) { + std::set<Vec2> an = calc_antinodes2(v); + for (auto &n:an) { + out.insert(n); + } + } + return (int)out.size(); } int main(int argc, char **argv) { diff --git a/day08/test2.txt b/day08/test2.txt @@ -0,0 +1,10 @@ +T......... +...T...... +.T........ +.......... +.......... +.......... +.......... +.......... +.......... +..........