diff --git a/AdventOfCode2024/AdventOfCode2024.vcxproj b/AdventOfCode2024/AdventOfCode2024.vcxproj index d648ef5..090671f 100644 --- a/AdventOfCode2024/AdventOfCode2024.vcxproj +++ b/AdventOfCode2024/AdventOfCode2024.vcxproj @@ -130,20 +130,41 @@ + + + + + + + + + - + + + + + + + + + + + + + diff --git a/AdventOfCode2024/AdventOfCode2024.vcxproj.filters b/AdventOfCode2024/AdventOfCode2024.vcxproj.filters index 56695fd..1d87b0d 100644 --- a/AdventOfCode2024/AdventOfCode2024.vcxproj.filters +++ b/AdventOfCode2024/AdventOfCode2024.vcxproj.filters @@ -33,7 +33,37 @@ Source Files - + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + Source Files @@ -53,11 +83,44 @@ Header Files + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + Header Files Header Files + + Header Files + + + Header Files + \ No newline at end of file diff --git a/AdventOfCode2024/MullCharState.cpp b/AdventOfCode2024/MullCharState.cpp new file mode 100644 index 0000000..b1fa759 --- /dev/null +++ b/AdventOfCode2024/MullCharState.cpp @@ -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 . + +#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; +} diff --git a/AdventOfCode2024/MullCharState.h b/AdventOfCode2024/MullCharState.h new file mode 100644 index 0000000..47372e2 --- /dev/null +++ b/AdventOfCode2024/MullCharState.h @@ -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 . + +#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_; +}; diff --git a/AdventOfCode2024/MullData.cpp b/AdventOfCode2024/MullData.cpp new file mode 100644 index 0000000..f5ac367 --- /dev/null +++ b/AdventOfCode2024/MullData.cpp @@ -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 . + +#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_; +} diff --git a/AdventOfCode2024/MullData.h b/AdventOfCode2024/MullData.h new file mode 100644 index 0000000..0931b0b --- /dev/null +++ b/AdventOfCode2024/MullData.h @@ -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 . + +#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_; +}; diff --git a/AdventOfCode2024/MullDataState.cpp b/AdventOfCode2024/MullDataState.cpp new file mode 100644 index 0000000..57a9ca7 --- /dev/null +++ b/AdventOfCode2024/MullDataState.cpp @@ -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 . + +#include "MullDataState.h" + +void MullDataState::setData(MullData& data) +{ + data_ = &data; +} diff --git a/AdventOfCode2024/MullDataState.h b/AdventOfCode2024/MullDataState.h new file mode 100644 index 0000000..2b014e9 --- /dev/null +++ b/AdventOfCode2024/MullDataState.h @@ -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 . + +#pragma once + +#include "MullData.h" +#include "StringState.h" + +class MullDataState : + public StringState +{ +public: + void setData(MullData& data); +protected: + MullData* data_{ nullptr }; +}; diff --git a/AdventOfCode2024/MullDoOpenState.cpp b/AdventOfCode2024/MullDoOpenState.cpp new file mode 100644 index 0000000..048b759 --- /dev/null +++ b/AdventOfCode2024/MullDoOpenState.cpp @@ -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 . + +#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; +} diff --git a/AdventOfCode2024/MullDoOpenState.h b/AdventOfCode2024/MullDoOpenState.h new file mode 100644 index 0000000..c07dc33 --- /dev/null +++ b/AdventOfCode2024/MullDoOpenState.h @@ -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 . + +#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_; +}; diff --git a/AdventOfCode2024/MullEntryState.cpp b/AdventOfCode2024/MullEntryState.cpp new file mode 100644 index 0000000..5d22315 --- /dev/null +++ b/AdventOfCode2024/MullEntryState.cpp @@ -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 . + +#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; +} diff --git a/AdventOfCode2024/MullEntryState.h b/AdventOfCode2024/MullEntryState.h new file mode 100644 index 0000000..9e505cc --- /dev/null +++ b/AdventOfCode2024/MullEntryState.h @@ -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 . + +#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_; +}; diff --git a/AdventOfCode2024/MullFactorState.cpp b/AdventOfCode2024/MullFactorState.cpp new file mode 100644 index 0000000..3725049 --- /dev/null +++ b/AdventOfCode2024/MullFactorState.cpp @@ -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 . + +#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; +} diff --git a/AdventOfCode2024/MullFactorState.h b/AdventOfCode2024/MullFactorState.h new file mode 100644 index 0000000..e30c9d1 --- /dev/null +++ b/AdventOfCode2024/MullFactorState.h @@ -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 . + +#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_; +}; diff --git a/AdventOfCode2024/MullItOver.cpp b/AdventOfCode2024/MullItOver.cpp new file mode 100644 index 0000000..a269aa8 --- /dev/null +++ b/AdventOfCode2024/MullItOver.cpp @@ -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 . + +#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(); +} diff --git a/AdventOfCode2024/MullItOver.h b/AdventOfCode2024/MullItOver.h new file mode 100644 index 0000000..fff410a --- /dev/null +++ b/AdventOfCode2024/MullItOver.h @@ -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 . + +#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_; +}; diff --git a/AdventOfCode2024/MullStates.cpp b/AdventOfCode2024/MullStates.cpp new file mode 100644 index 0000000..4d81379 --- /dev/null +++ b/AdventOfCode2024/MullStates.cpp @@ -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 . + +#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); +} diff --git a/AdventOfCode2024/MullStates.h b/AdventOfCode2024/MullStates.h new file mode 100644 index 0000000..952ebe1 --- /dev/null +++ b/AdventOfCode2024/MullStates.h @@ -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 . + +#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 }; +}; diff --git a/AdventOfCode2024/MullToggleCloseState.cpp b/AdventOfCode2024/MullToggleCloseState.cpp new file mode 100644 index 0000000..5084c47 --- /dev/null +++ b/AdventOfCode2024/MullToggleCloseState.cpp @@ -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 . + +#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; +} diff --git a/AdventOfCode2024/MullToggleCloseState.h b/AdventOfCode2024/MullToggleCloseState.h new file mode 100644 index 0000000..76f0615 --- /dev/null +++ b/AdventOfCode2024/MullToggleCloseState.h @@ -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 . + +#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_; +}; diff --git a/AdventOfCode2024/Program.cpp b/AdventOfCode2024/Program.cpp index 28ec101..d5364de 100644 --- a/AdventOfCode2024/Program.cpp +++ b/AdventOfCode2024/Program.cpp @@ -19,8 +19,10 @@ #include "Program.h" #include "SolverEngine.h" +// Solver implementations in day order. #include "HistorianHysteria.h" #include "RedNosedReports.h" +#include "MullItOver.h" void Program::run() { @@ -33,6 +35,7 @@ void Program::runSolvers() SolverEngine solverEngine{ getInputPaths() }; solverEngine.run(*std::make_unique()); solverEngine.run(*std::make_unique()); + solverEngine.run(*std::make_unique()); } std::vector Program::getInputPaths() const diff --git a/AdventOfCode2024/StringState.cpp b/AdventOfCode2024/StringState.cpp new file mode 100644 index 0000000..8c5d227 --- /dev/null +++ b/AdventOfCode2024/StringState.cpp @@ -0,0 +1 @@ +#include "StringState.h" diff --git a/AdventOfCode2024/StringState.h b/AdventOfCode2024/StringState.h new file mode 100644 index 0000000..4c88cd3 --- /dev/null +++ b/AdventOfCode2024/StringState.h @@ -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 . + +#pragma once + +#include + +#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() {}; +}; diff --git a/AdventOfCode2024/StringStateMachine.cpp b/AdventOfCode2024/StringStateMachine.cpp new file mode 100644 index 0000000..4235df4 --- /dev/null +++ b/AdventOfCode2024/StringStateMachine.cpp @@ -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 . + +#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); +} diff --git a/AdventOfCode2024/StringStateMachine.h b/AdventOfCode2024/StringStateMachine.h new file mode 100644 index 0000000..b3ddfdd --- /dev/null +++ b/AdventOfCode2024/StringStateMachine.h @@ -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 . + +#pragma once + +#include + +#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_; +}; diff --git a/README.md b/README.md index df55d83..18f472c 100644 --- a/README.md +++ b/README.md @@ -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. +### Day 3: Day 3: Mull It Over + +:mag_right: Puzzle: , :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 Copyright (C) 2024 Stefan Müller