59 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			59 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
// 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;
 | 
						|
}
 |