Add solution for "Day 22: Monkey Market", part 1

This commit is contained in:
Stefan Müller 2025-06-10 19:46:06 +02:00
parent 83c66682ef
commit f025896e84
4 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,31 @@
// 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 <aoc/framework/Solver-types.hpp>
class MonkeyMarket
: public Solver<int64_t, int64_t>
{
public:
virtual const std::string getPuzzleName() const override;
virtual const int getPuzzleDay() const override;
virtual void processDataLine(const std::string& line) override;
virtual void finish() override;
private:
static constexpr int getNSecretNumbers();
static constexpr uint64_t getPruneValue();
};

52
src/MonkeyMarket.cpp Normal file
View File

@ -0,0 +1,52 @@
// 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/MonkeyMarket.hpp>
const std::string MonkeyMarket::getPuzzleName() const
{
return "Monkey Market";
}
const int MonkeyMarket::getPuzzleDay() const
{
return 22;
}
void MonkeyMarket::processDataLine(const std::string& line)
{
uint64_t n{ std::stoull(line) };
for (int i = 0; i < getNSecretNumbers(); i++)
{
n = ((n << 6) ^ n) & getPruneValue();
n = (n >> 5) ^ n;
n = ((n << 11) ^ n) & getPruneValue();
}
part1 += n;
}
void MonkeyMarket::finish()
{
}
constexpr int MonkeyMarket::getNSecretNumbers()
{
return 2000;
}
constexpr uint64_t MonkeyMarket::getPruneValue()
{
return 16777215;
}

View File

@ -42,6 +42,7 @@
#include <aoc/LinenLayout.hpp> #include <aoc/LinenLayout.hpp>
#include <aoc/RaceCondition.hpp> #include <aoc/RaceCondition.hpp>
#include <aoc/KeypadConundrum.hpp> #include <aoc/KeypadConundrum.hpp>
#include <aoc/MonkeyMarket.hpp>
#include <aoc/LanParty.hpp> #include <aoc/LanParty.hpp>
void Program::run() void Program::run()
@ -81,6 +82,7 @@ void Program::runSolvers()
runSolver<LinenLayout>(solverEngine); runSolver<LinenLayout>(solverEngine);
runSolver<RaceCondition>(solverEngine); runSolver<RaceCondition>(solverEngine);
runSolver<KeypadConundrum>(solverEngine); runSolver<KeypadConundrum>(solverEngine);
runSolver<MonkeyMarket>(solverEngine);
runSolver<LanParty>(solverEngine); runSolver<LanParty>(solverEngine);
} }

View File

@ -39,6 +39,7 @@
#include <aoc/LinenLayout.hpp> #include <aoc/LinenLayout.hpp>
#include <aoc/RaceCondition.hpp> #include <aoc/RaceCondition.hpp>
#include <aoc/KeypadConundrum.hpp> #include <aoc/KeypadConundrum.hpp>
#include <aoc/MonkeyMarket.hpp>
#include <aoc/LanParty.hpp> #include <aoc/LanParty.hpp>
#define REQUIRE_MESSAGE(cond, msg) if (!(cond)) { INFO(msg); REQUIRE(cond); } #define REQUIRE_MESSAGE(cond, msg) if (!(cond)) { INFO(msg); REQUIRE(cond); }
@ -383,6 +384,19 @@ TEST_CASE("[KeypadConundrumTests]")
} }
} }
TEST_CASE("[MonkeyMarketTests]")
{
TestContext test;
SECTION("FullData")
{
test.runFull(std::make_unique<MonkeyMarket>(), 19854248602, 0);
}
SECTION("ExampleData")
{
test.runExample(std::make_unique<MonkeyMarket>(), 37327623, 0);
}
}
TEST_CASE("[LanPartyTests]") TEST_CASE("[LanPartyTests]")
{ {
TestContext test; TestContext test;