Snippets

cia_rana どきどきトロッコ

Created by cia_rana last modified
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]

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.