1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-09 02:59:06 +01:00
CodeWars/6kyu/design_a_simple_automaton/solution.cpp

37 lines
611 B
C++
Raw Normal View History

#include <map>
#include <set>
class Automaton
{
std::set<int> F{2};
const int initial_state = 1;
std::map<int, std::map<char, int>> transitions{
{1, std::map<char, int>{
{'0', 1}, {'1', 2}
}
},
{2, std::map<char, int>{
{'0', 3}, {'1', 2}
}
},
{3, std::map<char, int>{
{'0', 2}, {'1', 2}
}
}
};
public:
Automaton() {}
bool read_commands(const std::vector<char>& commands) {
int state = initial_state;
for (const char c : commands) {
state = transitions[state][c];
}
return F.find(state) != F.end();
}
};