From 44f2a0e0ec60436fd7d44263f620c949e4fdd596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Wed, 29 Jan 2025 22:04:12 +0100 Subject: [PATCH] Add solution for "Day 8: Resonant Collinearity", part 1 --- include/aoc/Point2.hpp | 1 + include/aoc/ResonantCollinearity.hpp | 38 +++++++++++++++ src/Point2.cpp | 5 ++ src/Program.cpp | 2 + src/ResonantCollinearity.cpp | 72 ++++++++++++++++++++++++++++ tests/src/TestCases.cpp | 14 ++++++ 6 files changed, 132 insertions(+) create mode 100644 include/aoc/ResonantCollinearity.hpp create mode 100644 src/ResonantCollinearity.cpp diff --git a/include/aoc/Point2.hpp b/include/aoc/Point2.hpp index 8e3caf3..538c801 100644 --- a/include/aoc/Point2.hpp +++ b/include/aoc/Point2.hpp @@ -34,6 +34,7 @@ class Point2 Point2(const int x, const int y); bool operator==(const Point2& rhs) const; bool operator!=(const Point2& rhs) const; + bool operator<(const Point2& rhs) const; Point2 operator+(const Point2& rhs) const; Point2 operator-(const Point2& rhs) const; Point2 operator*(const int rhs) const; diff --git a/include/aoc/ResonantCollinearity.hpp b/include/aoc/ResonantCollinearity.hpp new file mode 100644 index 0000000..64270a1 --- /dev/null +++ b/include/aoc/ResonantCollinearity.hpp @@ -0,0 +1,38 @@ +// Solutions to the Advent Of Code 2024. +// Copyright (C) 2025 Stefan Müller +// +// This program is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation, either version 3 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// this program. If not, see . + +#pragma once + +#include +#include +#include + +#include +#include + +class ResonantCollinearity + : public LinesSolver +{ +public: + virtual const std::string getPuzzleName() const override; + virtual const std::string getInputFileName() const override; + virtual void finish() override; +private: + static constexpr char getEmptyChar(); + std::map> antennas_{}; + std::set antinodes_{}; + void addNewAntenna(Point2&& newPosition); + void addNewAntinode(Point2&& newPosition); +}; diff --git a/src/Point2.cpp b/src/Point2.cpp index 1103915..f3a6de3 100644 --- a/src/Point2.cpp +++ b/src/Point2.cpp @@ -47,6 +47,11 @@ bool Point2::operator!=(const Point2& rhs) const return !(*this == rhs); } +bool Point2::operator<(const Point2& rhs) const +{ + return x < rhs.x || (x == rhs.x && y < rhs.y); +} + Point2 Point2::operator+(const Point2& rhs) const { return Point2(x + rhs.x, y + rhs.y); diff --git a/src/Program.cpp b/src/Program.cpp index 5978213..a2358b0 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -55,6 +56,7 @@ void Program::runSolvers() runSolver(solverEngine); runSolver(solverEngine); runSolver(solverEngine); + runSolver(solverEngine); runSolver(solverEngine); runSolver(solverEngine); runSolver(solverEngine); diff --git a/src/ResonantCollinearity.cpp b/src/ResonantCollinearity.cpp new file mode 100644 index 0000000..9dea232 --- /dev/null +++ b/src/ResonantCollinearity.cpp @@ -0,0 +1,72 @@ +// Solutions to the Advent Of Code 2024. +// Copyright (C) 2025 Stefan Müller +// +// This program is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation, either version 3 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// this program. If not, see . + +#include + +#include + +const std::string ResonantCollinearity::getPuzzleName() const +{ + return "Day 8: Resonant Collinearity"; +} + +const std::string ResonantCollinearity::getInputFileName() const +{ + return "resonant_collinearity.txt"; +} + +void ResonantCollinearity::finish() +{ + for (int j = 0; j < lines.size(); j++) + { + for (int i = 0; i < lines[j].size(); i++) + { + if (lines[j][i] != getEmptyChar()) + { + addNewAntenna({ i, j }); + } + } + } + + part1 = antinodes_.size(); +} + +constexpr char ResonantCollinearity::getEmptyChar() +{ + return '.'; +} + +void ResonantCollinearity::addNewAntenna(Point2&& newPosition) +{ + const auto [it, success] = antennas_.insert({ getPosition(newPosition), { newPosition } }); + if (!success) + { + for (const auto& oldPosition : it->second) + { + addNewAntinode(newPosition * 2 - oldPosition); + addNewAntinode(oldPosition * 2 - newPosition); + } + + it->second.push_back(newPosition); + } +} + +void ResonantCollinearity::addNewAntinode(Point2&& newPosition) +{ + if (isInBounds(newPosition)) + { + antinodes_.insert(newPosition); + } +} diff --git a/tests/src/TestCases.cpp b/tests/src/TestCases.cpp index 13f491e..00174c0 100644 --- a/tests/src/TestCases.cpp +++ b/tests/src/TestCases.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -120,6 +121,19 @@ TEST_CASE("[BridgeRepairTests]") } } +TEST_CASE("[ResonantCollinearityTests]") +{ + TestContext test; + SECTION("FullData") + { + test.run(std::make_unique(), 426, 0, test.getInputPaths()); + } + SECTION("ExampleData") + { + test.run(std::make_unique(), 14, 0, test.getExampleInputPaths()); + } +} + TEST_CASE("[HoofItTests]") { TestContext test;