Add solution for "Day 8: Resonant Collinearity", part 1

This commit is contained in:
Stefan Müller 2025-01-29 22:04:12 +01:00
parent c4b2684d66
commit 44f2a0e0ec
6 changed files with 132 additions and 0 deletions

View File

@ -34,6 +34,7 @@ class Point2
Point2(const int x, const int y); 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; 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 Point2& rhs) const; Point2 operator-(const Point2& rhs) const;
Point2 operator*(const int rhs) const; Point2 operator*(const int rhs) const;

View File

@ -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 <http://www.gnu.org/licenses/>.
#pragma once
#include <map>
#include <set>
#include <vector>
#include <aoc/LinesSolver.hpp>
#include <aoc/Point2.hpp>
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<char, std::vector<Point2>> antennas_{};
std::set<Point2> antinodes_{};
void addNewAntenna(Point2&& newPosition);
void addNewAntinode(Point2&& newPosition);
};

View File

@ -47,6 +47,11 @@ bool Point2::operator!=(const Point2& rhs) const
return !(*this == rhs); 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 Point2 Point2::operator+(const Point2& rhs) const
{ {
return Point2(x + rhs.x, y + rhs.y); return Point2(x + rhs.x, y + rhs.y);

View File

@ -28,6 +28,7 @@
#include <aoc/PrintQueue.hpp> #include <aoc/PrintQueue.hpp>
#include <aoc/GuardGallivant.hpp> #include <aoc/GuardGallivant.hpp>
#include <aoc/BridgeRepair.hpp> #include <aoc/BridgeRepair.hpp>
#include <aoc/ResonantCollinearity.hpp>
#include <aoc/HoofIt.hpp> #include <aoc/HoofIt.hpp>
#include <aoc/PlutonianPebbles.hpp> #include <aoc/PlutonianPebbles.hpp>
#include <aoc/LanParty.hpp> #include <aoc/LanParty.hpp>
@ -55,6 +56,7 @@ void Program::runSolvers()
runSolver<PrintQueue>(solverEngine); runSolver<PrintQueue>(solverEngine);
runSolver<GuardGallivant>(solverEngine); runSolver<GuardGallivant>(solverEngine);
runSolver<BridgeRepair>(solverEngine); runSolver<BridgeRepair>(solverEngine);
runSolver<ResonantCollinearity>(solverEngine);
runSolver<HoofIt>(solverEngine); runSolver<HoofIt>(solverEngine);
runSolver<PlutonianPebbles>(solverEngine); runSolver<PlutonianPebbles>(solverEngine);
runSolver<LanParty>(solverEngine); runSolver<LanParty>(solverEngine);

View File

@ -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 <http://www.gnu.org/licenses/>.
#include <aoc/ResonantCollinearity.hpp>
#include <iostream>
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);
}
}

View File

@ -22,6 +22,7 @@
#include <aoc/PrintQueue.hpp> #include <aoc/PrintQueue.hpp>
#include <aoc/GuardGallivant.hpp> #include <aoc/GuardGallivant.hpp>
#include <aoc/BridgeRepair.hpp> #include <aoc/BridgeRepair.hpp>
#include <aoc/ResonantCollinearity.hpp>
#include <aoc/HoofIt.hpp> #include <aoc/HoofIt.hpp>
#include <aoc/PlutonianPebbles.hpp> #include <aoc/PlutonianPebbles.hpp>
#include <aoc/LanParty.hpp> #include <aoc/LanParty.hpp>
@ -120,6 +121,19 @@ TEST_CASE("[BridgeRepairTests]")
} }
} }
TEST_CASE("[ResonantCollinearityTests]")
{
TestContext test;
SECTION("FullData")
{
test.run(std::make_unique<ResonantCollinearity>(), 426, 0, test.getInputPaths());
}
SECTION("ExampleData")
{
test.run(std::make_unique<ResonantCollinearity>(), 14, 0, test.getExampleInputPaths());
}
}
TEST_CASE("[HoofItTests]") TEST_CASE("[HoofItTests]")
{ {
TestContext test; TestContext test;