LoginSignup
2
1

More than 5 years have passed since last update.

オフラインリアルタイムどう書くE05 Ruby で解く

Posted at

当日不参加でした。ので久々のオンラインノンリアルタイムどう書く。

湯上りにプレッシャーのない状況も手伝って 23 分で書けました。
珍しく最初に思いついたものをそのままコードにできて、珍しくつまずきなく全入力がパスしました。

orde05dokitruck.rb
def move((a, b, c) , no)
  case no
  when '1' then [a, (a | b), (b | c)]
  when '2' then [a, (b | c), (a | c)]
  when '3' then [(a | b), b, (a | c)]
  when '4' then [(a | b), (b | c), c]
  when '5' then [(a | c), b, (b | c)]
  when '6' then [(a | c), (a | b), c]
  when '7' then [a, [], c]
  when '8' then [[], b, c]
  when '9' then [a, b, []]
  end
end

def solve(input)
  result = input.chars.reduce([['a'], ['b'], ['c']]) {|course, c| move(course, c) }.reduce(:|).sort

  if result.empty?
    '-'
  else
    result.join
  end
end

DATA.each do |line|
  input, expected = line.split
  actual = solve(input)
  if actual == expected
    print '.'
  else
    puts(<<~EOS)

      input:    #{input}
      expected: #{expected}
      actual:   #{actual}
    EOS
  end
end

puts

__END__
1728398 bc
789 -
274 ac
185 abc
396 ab
1278 abc
7659832 a
178 bc
189 ab
197 a
278 ac
289 bc
297 a
378 ac
389 b
397 ab
478 c
489 bc
497 ab
578 bc
589 b
597 ac
678 c
689 ab
697 ac
899 b
7172 ac
54787 bc
83713 bc
149978 -
159735 abc
1449467 abc
9862916 b
96112873 ab
311536789 -
281787212994 abc
697535114542 ac
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1