Add solution for "Day 8: Resonant Collinearity", part 1
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <aoc/PrintQueue.hpp>
|
||||
#include <aoc/GuardGallivant.hpp>
|
||||
#include <aoc/BridgeRepair.hpp>
|
||||
#include <aoc/ResonantCollinearity.hpp>
|
||||
#include <aoc/HoofIt.hpp>
|
||||
#include <aoc/PlutonianPebbles.hpp>
|
||||
#include <aoc/LanParty.hpp>
|
||||
@@ -55,6 +56,7 @@ void Program::runSolvers()
|
||||
runSolver<PrintQueue>(solverEngine);
|
||||
runSolver<GuardGallivant>(solverEngine);
|
||||
runSolver<BridgeRepair>(solverEngine);
|
||||
runSolver<ResonantCollinearity>(solverEngine);
|
||||
runSolver<HoofIt>(solverEngine);
|
||||
runSolver<PlutonianPebbles>(solverEngine);
|
||||
runSolver<LanParty>(solverEngine);
|
||||
|
||||
72
src/ResonantCollinearity.cpp
Normal file
72
src/ResonantCollinearity.cpp
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user