Created
May 29, 2017 19:02
-
-
Save cia-rana/9e7a4318f4d709b576a869d5e0208c9f to your computer and use it in GitHub Desktop.
どきどきトロッコ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
transition = {} | |
transition[0] = { | |
1 => [0, 1], | |
2 => [0, 2], | |
3 => [0, 2], | |
4 => [0], | |
5 => [0], | |
6 => [0, 1], | |
7 => [0], | |
8 => [], | |
9 => [0] | |
} | |
transition[1] = { | |
1 => [1, 2], | |
2 => [1], | |
3 => [0, 1], | |
4 => [0, 1], | |
5 => [1, 2], | |
6 => [1], | |
7 => [], | |
8 => [1], | |
9 => [1] | |
} | |
transition[2] = { | |
1 => [2], | |
2 => [1, 2], | |
3 => [2], | |
4 => [1, 2], | |
5 => [0, 2], | |
6 => [0, 2], | |
7 => [2], | |
8 => [2], | |
9 => [] | |
} | |
nfa = ->(states, inputs) { | |
return states if inputs.empty? | |
states.map { |i| | |
nfa[transition[i][inputs[0]], inputs[1..-1]] | |
} | |
} | |
nfa_runner = ->(input) { | |
outputs = [0, 1, 2].map { |i| | |
(i+97).chr unless nfa[[i], input.chars.map(&:to_i)].flatten.compact.empty? | |
}.compact | |
outputs.empty? ? '-' : outputs * '' | |
} | |
puts nfa_runner[gets] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment