Add solution for "Day 19: Linen Layout", part 1
This commit is contained in:
101
src/extra/LinenTowelPatterns.cpp
Normal file
101
src/extra/LinenTowelPatterns.cpp
Normal 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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user