Add solutions for "Day 3: Mull It Over"

This commit is contained in:
Stefan Müller 2024-12-03 18:06:04 +01:00
parent 034f60b678
commit d810f304ff
26 changed files with 884 additions and 2 deletions

View File

@ -130,20 +130,41 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="AdventOfCode2024.cpp" /> <ClCompile Include="AdventOfCode2024.cpp" />
<ClCompile Include="HistorianHysteria.cpp" /> <ClCompile Include="HistorianHysteria.cpp" />
<ClCompile Include="MullDataState.cpp" />
<ClCompile Include="MullToggleCloseState.cpp" />
<ClCompile Include="MullDoOpenState.cpp" />
<ClCompile Include="MullEntryState.cpp" />
<ClCompile Include="MullData.cpp" />
<ClCompile Include="MullFactorState.cpp" />
<ClCompile Include="MullItOver.cpp" />
<ClCompile Include="MullStates.cpp" />
<ClCompile Include="MullCharState.cpp" />
<ClCompile Include="Program.cpp" /> <ClCompile Include="Program.cpp" />
<ClCompile Include="RedNosedReportData.cpp" />
<ClCompile Include="RedNosedReports.cpp" /> <ClCompile Include="RedNosedReports.cpp" />
<ClCompile Include="Solver.cpp" /> <ClCompile Include="Solver.cpp" />
<ClCompile Include="SolverEngine.cpp" /> <ClCompile Include="SolverEngine.cpp" />
<ClCompile Include="StringState.cpp" />
<ClCompile Include="StringStateMachine.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="HistorianHysteria.h" /> <ClInclude Include="HistorianHysteria.h" />
<ClInclude Include="MullDataState.h" />
<ClInclude Include="MullToggleCloseState.h" />
<ClInclude Include="MullDoOpenState.h" />
<ClInclude Include="MullEntryState.h" />
<ClInclude Include="MullData.h" />
<ClInclude Include="MullFactorState.h" />
<ClInclude Include="MullItOver.h" />
<ClInclude Include="MullStates.h" />
<ClInclude Include="MullCharState.h" />
<ClInclude Include="Program.h" /> <ClInclude Include="Program.h" />
<ClInclude Include="RedNosedReportData.h" /> <ClInclude Include="RedNosedReportData.h" />
<ClInclude Include="RedNosedReports.h" /> <ClInclude Include="RedNosedReports.h" />
<ClInclude Include="Slope.h" /> <ClInclude Include="Slope.h" />
<ClInclude Include="Solver.h" /> <ClInclude Include="Solver.h" />
<ClInclude Include="SolverEngine.h" /> <ClInclude Include="SolverEngine.h" />
<ClInclude Include="StringState.h" />
<ClInclude Include="StringStateMachine.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -33,7 +33,37 @@
<ClCompile Include="RedNosedReports.cpp"> <ClCompile Include="RedNosedReports.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="RedNosedReportData.cpp"> <ClCompile Include="MullItOver.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="StringStateMachine.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="StringState.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MullEntryState.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MullData.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MullCharState.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MullFactorState.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MullDoOpenState.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MullToggleCloseState.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MullDataState.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MullStates.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
@ -53,11 +83,44 @@
<ClInclude Include="RedNosedReports.h"> <ClInclude Include="RedNosedReports.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="MullItOver.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="StringStateMachine.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="StringState.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MullEntryState.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MullData.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MullCharState.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MullFactorState.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MullDoOpenState.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MullToggleCloseState.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="RedNosedReportData.h"> <ClInclude Include="RedNosedReportData.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Slope.h"> <ClInclude Include="Slope.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="MullStates.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MullDataState.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,37 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullCharState.h"
MullCharState::MullCharState(const char expected)
: expected_{ expected }, successState_{}, failState_{} {}
void MullCharState::next(StringStateMachine* stateMachine)
{
if (stateMachine->getCurrent() == expected_)
{
stateMachine->setState(*successState_);
}
else
{
stateMachine->setState(*failState_);
}
}
void MullCharState::setTransitions(StringState& successState, StringState& failState)
{
successState_ = &successState;
failState_ = &failState;
}

View File

@ -0,0 +1,33 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "StringState.h"
class MullCharState :
public StringState
{
public:
MullCharState(const char expected);
void enter(StringStateMachine* stateMachine) override {};
void next(StringStateMachine* stateMachine) override;
void exit(StringStateMachine* stateMachine) override {};
void setTransitions(StringState& successState, StringState& failState);
private:
char expected_;
StringState* successState_;
StringState* failState_;
};

View File

@ -0,0 +1,66 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullData.h"
MullData::MullData()
: isEnabled_{ true }, factor1_{ 0 }, factor2_{ 0 }, part1_{ 0 }, part2_{ 0 } {};
bool MullData::getIsEnabled()
{
return isEnabled_;
}
void MullData::setIsEnabled(const bool value)
{
isEnabled_ = value;
}
int MullData::getFactor(const int index)
{
return index == 1 ? factor1_ : factor2_;
}
void MullData::setFactor(const int index, const int value)
{
if (index == 1)
{
factor1_ = value;
}
else
{
factor2_ = value;
}
}
void MullData::updateResult()
{
auto product = factor1_ * factor2_;
part1_ += product;
if (isEnabled_)
{
part2_ += product;
}
}
long long int MullData::getResultPart1()
{
return part1_;
}
long long int MullData::getResultPart2()
{
return part2_;
}

View File

@ -0,0 +1,35 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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
class MullData
{
public:
MullData();
bool getIsEnabled();
void setIsEnabled(const bool value);
int getFactor(const int index);
void setFactor(const int index, const int value);
void updateResult();
long long int getResultPart1();
long long int getResultPart2();
private:
bool isEnabled_;
int factor1_;
int factor2_;
long long int part1_;
long long int part2_;
};

View File

@ -0,0 +1,21 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullDataState.h"
void MullDataState::setData(MullData& data)
{
data_ = &data;
}

View File

@ -0,0 +1,28 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullData.h"
#include "StringState.h"
class MullDataState :
public StringState
{
public:
void setData(MullData& data);
protected:
MullData* data_{ nullptr };
};

View File

@ -0,0 +1,39 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullDoOpenState.h"
void MullDoOpenState::next(StringStateMachine* stateMachine)
{
if (stateMachine->getCurrent() == '(' && !data_->getIsEnabled())
{
stateMachine->setState(*doState_);
}
else if (stateMachine->getCurrent() == 'n' && data_->getIsEnabled())
{
stateMachine->setState(*dontState_);
}
else
{
stateMachine->setState(*failState_);
}
}
void MullDoOpenState::setTransitions(StringState& doState, StringState& dontState, StringState& failState)
{
doState_ = &doState;
dontState_ = &dontState;
failState_ = &failState;
}

View File

@ -0,0 +1,32 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullDataState.h"
class MullDoOpenState :
public MullDataState
{
public:
void enter(StringStateMachine* stateMachine) override {};
void next(StringStateMachine* stateMachine) override;
void exit(StringStateMachine* stateMachine) override {};
void setTransitions(StringState& doState, StringState& dontState, StringState& failState);
private:
StringState* doState_;
StringState* dontState_;
StringState* failState_;
};

View File

@ -0,0 +1,39 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullEntryState.h"
void MullEntryState::enter(StringStateMachine* stateMachine)
{
next(stateMachine);
}
void MullEntryState::next(StringStateMachine* stateMachine)
{
if (stateMachine->getCurrent() == 'm')
{
stateMachine->setState(*mulState_);
}
else if (stateMachine->getCurrent() == 'd')
{
stateMachine->setState(*doState_);
}
}
void MullEntryState::setTransitions(StringState& mulState, StringState& doState)
{
mulState_ = &mulState;
doState_ = &doState;
}

View File

@ -0,0 +1,31 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "StringState.h"
class MullEntryState :
public StringState
{
public:
void enter(StringStateMachine* stateMachine) override;
void next(StringStateMachine* stateMachine) override;
void exit(StringStateMachine* stateMachine) override {};
void setTransitions(StringState& mulState, StringState& doState);
private:
StringState* mulState_;
StringState* doState_;
};

View File

@ -0,0 +1,58 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullFactorState.h"
MullFactorState::MullFactorState(const char expected, const int index)
: expected_{ expected }, index_{ index }, successState_{}, failState_{} {}
void MullFactorState::enter(StringStateMachine* stateMachine)
{
data_->setFactor(index_, 0);
}
void MullFactorState::next(StringStateMachine* stateMachine)
{
if (stateMachine->getCurrent() == expected_ && data_->getFactor(index_) > 0)
{
if (index_ == 2)
{
data_->updateResult();
}
stateMachine->setState(*successState_);
}
else
{
int x = stateMachine->getCurrent() - '0';
if (0 <= x && x <= 9)
{
data_->setFactor(index_, data_->getFactor(index_) * 10 + x);
if (data_->getFactor(index_) > 999)
{
stateMachine->setState(*failState_);
}
}
else
{
stateMachine->setState(*failState_);
}
}
}
void MullFactorState::setTransitions(StringState& successState, StringState& failState)
{
successState_ = &successState;
failState_ = &failState;
}

View File

@ -0,0 +1,34 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullDataState.h"
class MullFactorState :
public MullDataState
{
public:
MullFactorState(const char expected, const int index);
void enter(StringStateMachine* stateMachine) override;
void next(StringStateMachine* stateMachine) override;
void exit(StringStateMachine* stateMachine) override {};
void setTransitions(StringState& successState, StringState& failState);
private:
char expected_;
int index_;
StringState* successState_;
StringState* failState_;
};

View File

@ -0,0 +1,44 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullItOver.h"
#include "StringStateMachine.h"
MullItOver::MullItOver()
: Solver{}, data_{}, states_ {
data_
} {}
std::string MullItOver::getPuzzleName() const
{
return "Day 3: Mull It Over";
}
std::string MullItOver::getInputFileName() const
{
return "mull_it_over.txt";
}
void MullItOver::processDataLine(const std::string& line)
{
StringStateMachine stateMachine{ line, states_.entryState };
stateMachine.run();
}
void MullItOver::finish()
{
part1 = data_.getResultPart1();
part2 = data_.getResultPart2();
}

View File

@ -0,0 +1,34 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullData.h"
#include "MullStates.h"
#include "Solver.h"
class MullItOver :
public Solver
{
public:
MullItOver();
std::string getPuzzleName() const override;
std::string getInputFileName() const override;
void processDataLine(const std::string& line) override;
void finish() override;
private:
MullData data_;
MullStates states_;
};

View File

@ -0,0 +1,38 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullStates.h"
MullStates::MullStates(MullData& data)
{
entryState.setTransitions(uState, oState);
uState.setTransitions(lState, entryState);
lState.setTransitions(mulOpenState, entryState);
mulOpenState.setTransitions(factor1State, entryState);
factor1State.setData(data);
factor1State.setTransitions(factor2State, entryState);
factor2State.setData(data);
factor2State.setTransitions(entryState, entryState);
oState.setTransitions(doOpenState, entryState);
doOpenState.setData(data);
doOpenState.setTransitions(doCloseState, apostropheState, entryState);
doCloseState.setData(data);
doCloseState.setTransitions(entryState);
apostropheState.setTransitions(tState, entryState);
tState.setTransitions(dontOpenState, entryState);
dontOpenState.setTransitions(dontCloseState, entryState);
dontCloseState.setData(data);
dontCloseState.setTransitions(entryState);
}

View File

@ -0,0 +1,41 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullCharState.h"
#include "MullDoOpenState.h"
#include "MullEntryState.h"
#include "MullFactorState.h"
#include "MullToggleCloseState.h"
class MullStates
{
public:
MullStates(MullData& data);
MullEntryState entryState;
MullCharState uState{ 'u' };
MullCharState lState{ 'l' };
MullCharState mulOpenState{ '(' };
MullFactorState factor1State{ ',', 1 };
MullFactorState factor2State{ ')', 2 };
MullCharState oState{ 'o' };
MullDoOpenState doOpenState;
MullToggleCloseState doCloseState{ true };
MullCharState apostropheState{ '\'' };
MullCharState tState{ 't' };
MullCharState dontOpenState{ '(' };
MullToggleCloseState dontCloseState{ false };
};

View File

@ -0,0 +1,33 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullToggleCloseState.h"
MullToggleCloseState::MullToggleCloseState(const bool isEnabler)
: isEnabler_{ isEnabler }, successState_{} {}
void MullToggleCloseState::next(StringStateMachine* stateMachine)
{
if (stateMachine->getCurrent() == ')')
{
data_->setIsEnabled(isEnabler_);
}
stateMachine->setState(*successState_);
}
void MullToggleCloseState::setTransitions(StringState& successState)
{
successState_ = &successState;
}

View File

@ -0,0 +1,32 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "MullDataState.h"
class MullToggleCloseState :
public MullDataState
{
public:
MullToggleCloseState(const bool isEnabler);
void enter(StringStateMachine* stateMachine) override {};
void next(StringStateMachine* stateMachine) override;
void exit(StringStateMachine* stateMachine) override {};
void setTransitions(StringState& successState);
private:
bool isEnabler_;
StringState* successState_;
};

View File

@ -19,8 +19,10 @@
#include "Program.h" #include "Program.h"
#include "SolverEngine.h" #include "SolverEngine.h"
// Solver implementations in day order.
#include "HistorianHysteria.h" #include "HistorianHysteria.h"
#include "RedNosedReports.h" #include "RedNosedReports.h"
#include "MullItOver.h"
void Program::run() void Program::run()
{ {
@ -33,6 +35,7 @@ void Program::runSolvers()
SolverEngine solverEngine{ getInputPaths() }; SolverEngine solverEngine{ getInputPaths() };
solverEngine.run(*std::make_unique<HistorianHysteria>()); solverEngine.run(*std::make_unique<HistorianHysteria>());
solverEngine.run(*std::make_unique<RedNosedReports>()); solverEngine.run(*std::make_unique<RedNosedReports>());
solverEngine.run(*std::make_unique<MullItOver>());
} }
std::vector<std::string> Program::getInputPaths() const std::vector<std::string> Program::getInputPaths() const

View File

@ -0,0 +1 @@
#include "StringState.h"

View File

@ -0,0 +1,31 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 <vector>
#include "StringStateMachine.h"
class StringStateMachine;
class StringState
{
public:
virtual void enter(StringStateMachine* stateMachine) = 0;
virtual void next(StringStateMachine* stateMachine) = 0;
virtual void exit(StringStateMachine* stateMachine) = 0;
virtual ~StringState() {};
};

View File

@ -0,0 +1,46 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 "StringStateMachine.h"
StringStateMachine::StringStateMachine(const std::string& line, StringState& entryState)
{
line_ = line;
entryState_ = &entryState;
currentState_ = entryState_;
current_ = ' ';
}
void StringStateMachine::run()
{
currentState_ = entryState_;
for (auto c : line_)
{
current_ = c;
currentState_->next(this);
}
}
char StringStateMachine::getCurrent() const
{
return current_;
}
void StringStateMachine::setState(StringState& state)
{
currentState_->exit(this);
currentState_ = &state;
currentState_->enter(this);
}

View File

@ -0,0 +1,36 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 <string>
#include "StringState.h"
class StringState;
class StringStateMachine
{
public:
StringStateMachine(const std::string& line, StringState& entryState);
void run();
char getCurrent() const;
void setState(StringState& state);
private:
std::string line_;
StringState* entryState_;
StringState* currentState_;
char current_;
};

View File

@ -26,6 +26,12 @@ I'm using a `std::multiset` to collect and sort the values for both lists. This
Here, we have a few conditionals to determine on the fly which of the numbers would make the report safe if dropped. The amount of cases is actually quite manageable. Here, we have a few conditionals to determine on the fly which of the numbers would make the report safe if dropped. The amount of cases is actually quite manageable.
### Day 3: Day 3: Mull It Over
:mag_right: Puzzle: <https://adventofcode.com/2024/day/3>, :white_check_mark: Solver: [`HistorianHysteria.cpp`](AdventOfCode2024/MullItOver.cpp)
A simple finite state machine crawling along the input character by character solves both parts nicely. The algorithm tracks whether `mul` instructions are enabled or not, but ignores this setting for part 1.
## License ## License
Copyright (C) 2024 Stefan Müller Copyright (C) 2024 Stefan Müller