Add solution for "Day 19: Linen Layout", part 1

This commit is contained in:
Stefan Müller 2025-06-01 23:00:16 +02:00
parent f8f431b61a
commit 12fdc184cf
6 changed files with 239 additions and 0 deletions

View File

@ -0,0 +1,32 @@
// 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/extra/LinenTowelPatterns.hpp>
#include <aoc/framework/Solver-types.hpp>
class LinenLayout
: 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:
LinenTowelPatterns patterns_{};
bool isReadingDesigns_{ false };
};

View File

@ -0,0 +1,32 @@
// 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 <string>
#include <vector>
class LinenTowelPatterns
{
public:
LinenTowelPatterns();
void add(const std::string& towelPattern);
bool isPossible(const std::string& design);
private:
std::vector<std::pair<size_t, bool>> patterns_;
std::map<char, size_t> stripes_;
void addBranch();
};

58
src/LinenLayout.cpp Normal file
View File

@ -0,0 +1,58 @@
// 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/LinenLayout.hpp>
#include <sstream>
const std::string LinenLayout::getPuzzleName() const
{
return "Linen Layout";
}
const int LinenLayout::getPuzzleDay() const
{
return 19;
}
void LinenLayout::processDataLine(const std::string& line)
{
if (isReadingDesigns_)
{
if (patterns_.isPossible(line))
{
part1++;
}
}
else if (line.empty())
{
isReadingDesigns_ = true;
}
else
{
std::istringstream stream{ line };
std::string pattern;
while (std::getline(stream, pattern, ','))
{
patterns_.add(pattern);
// Removes the space between comma and next pattern.
char c = stream.get();
}
}
}
void LinenLayout::finish()
{
}

View File

@ -39,6 +39,7 @@
#include <aoc/ReindeerMaze.hpp>
#include <aoc/ChronospatialComputer.hpp>
#include <aoc/RamRun.hpp>
#include <aoc/LinenLayout.hpp>
#include <aoc/LanParty.hpp>
void Program::run()
@ -75,6 +76,7 @@ void Program::runSolvers()
runSolver<ReindeerMaze>(solverEngine);
runSolver<ChronospatialComputer>(solverEngine);
runSolver<RamRun>(solverEngine);
runSolver<LinenLayout>(solverEngine);
runSolver<LanParty>(solverEngine);
}

View File

@ -0,0 +1,101 @@
// 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/extra/LinenTowelPatterns.hpp>
#include <set>
LinenTowelPatterns::LinenTowelPatterns()
: patterns_{}, stripes_{
{ 'w', 0 },
{ 'u', 1 },
{ 'b', 2 },
{ 'r', 3 },
{ 'g', 4 }
}
{
addBranch();
}
void LinenTowelPatterns::add(const std::string& towelPattern)
{
size_t current{ 0 };
size_t next{ 0 };
for (const char stripe : towelPattern)
{
if (next >= patterns_.size())
{
patterns_[current].first = next;
addBranch();
}
current = next + stripes_[stripe];
next = patterns_[current].first > 0 ? patterns_[current].first : patterns_.size();
}
patterns_[current].second = true;
}
bool LinenTowelPatterns::isPossible(const std::string& design)
{
std::vector<bool> canStartFrom(design.size(), false);
canStartFrom[0] = true;
std::set<size_t> unexploredStart;
unexploredStart.insert(0);
while (!unexploredStart.empty())
{
size_t designPosition = *unexploredStart.rbegin();
unexploredStart.erase(designPosition);
size_t i{ stripes_[design[designPosition]] };
while (++designPosition < design.size() && patterns_[i].first > 0)
{
if (patterns_[i].second && !canStartFrom[designPosition])
{
// The current accumulated stripe pattern exists and it ends at a new position within the design.
canStartFrom[designPosition] = true;
unexploredStart.insert(designPosition);
}
if (patterns_[i].first > 0)
{
// The current accumulated stripe pattern can be extended further.
i = patterns_[i].first + stripes_[design[designPosition]];
}
}
if (patterns_[i].second)
{
if (designPosition == design.size())
{
// The current accumulated stripe pattern exists and finishes the design.
return true;
}
else if (!canStartFrom[designPosition])
{
// The current accumulated stripe pattern exists and it ends at a new position within the design.
canStartFrom[designPosition] = true;
unexploredStart.insert(designPosition);
}
}
}
return false;
}
void LinenTowelPatterns::addBranch()
{
for (size_t i = 0; i < stripes_.size(); i++)
{
patterns_.emplace_back(std::make_pair<size_t, bool>(0, false));
}
}

View File

@ -36,6 +36,7 @@
#include <aoc/ReindeerMaze.hpp>
#include <aoc/ChronospatialComputer.hpp>
#include <aoc/RamRun.hpp>
#include <aoc/LinenLayout.hpp>
#include <aoc/LanParty.hpp>
#define REQUIRE_MESSAGE(cond, msg) if (!(cond)) { INFO(msg); REQUIRE(cond); }
@ -340,6 +341,19 @@ TEST_CASE("[RamRunTests]")
}
}
TEST_CASE("[LinenLayoutTests]")
{
TestContext test;
SECTION("FullData")
{
test.run(std::make_unique<LinenLayout>(), 272, 0, test.getInputPaths());
}
SECTION("ExampleData")
{
test.run(std::make_unique<LinenLayout>(), 6, 0, test.getExampleInputPaths());
}
}
TEST_CASE("[LanPartyTests]")
{
TestContext test;