結果

問題 No.769 UNOシミュレータ
ユーザー gemmarogemmaro
提出日時 2020-04-29 08:15:14
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 2,521 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 7,296 KB
実行使用メモリ 35,712 KB
最終ジャッジ日時 2024-11-27 12:48:15
合計ジャッジ時間 7,868 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 88 ms
12,160 KB
testcase_02 AC 88 ms
12,160 KB
testcase_03 AC 88 ms
12,160 KB
testcase_04 AC 91 ms
12,160 KB
testcase_05 AC 92 ms
12,160 KB
testcase_06 AC 92 ms
12,160 KB
testcase_07 AC 92 ms
12,160 KB
testcase_08 WA -
testcase_09 AC 103 ms
12,800 KB
testcase_10 AC 105 ms
12,672 KB
testcase_11 AC 106 ms
12,672 KB
testcase_12 AC 236 ms
18,176 KB
testcase_13 AC 239 ms
18,176 KB
testcase_14 WA -
testcase_15 AC 414 ms
26,112 KB
testcase_16 WA -
testcase_17 AC 408 ms
25,344 KB
testcase_18 WA -
testcase_19 AC 561 ms
34,432 KB
testcase_20 AC 558 ms
34,304 KB
testcase_21 WA -
testcase_22 AC 84 ms
12,160 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

# frozen_string_literal: true

#
# represent drunks situation
#
class Drunks
  def number(last)
    if @prev2
      suffer2
      advance unless last
    elsif @prev4
      suffer4
      advance unless last
    end
    consume
    advance unless last
  end

  def draw2(last)
    if @prev4
      suffer4
      advance unless last
    end
    consume
    @bomb += 2
    @prev2 = true
    advance unless last
  end

  def draw4(last)
    if @prev2
      suffer2
      advance unless last
    end
    consume
    @bomb += 4
    @prev4 = true
    advance unless last
  end

  # rubocop:todo Metrics/PerceivedComplexity
  def skip(last) # rubocop:todo Metrics/CyclomaticComplexity
    if @prev2
      suffer2
      advance unless last
    elsif @prev4
      suffer4
      advance unless last
    end
    consume
    advance unless last
    advance unless last
  end
  # rubocop:enable Metrics/PerceivedComplexity

  def reverse(last)
    if @prev2
      suffer2
      advance unless last
    elsif @prev4
      suffer4
      advance unless last
    end
    @reverse = !@reverse
    consume
    advance unless last
  end

  def winner
    winner_id = @turn + 1
    winner_first_num = @members[@turn].original
    "#{winner_id} #{winner_first_num}"
  end

  private

  def initialize(num)
    @turn = 0
    @reverse = false
    @prev2 = false
    @prev4 = false
    @bomb = 0
    @members = Array.new(num) { Drunk.new }
  end

  def advance
    turn = @turn + (@reverse ? -1 : 1)
    @turn = turn % @members.size
  end

  def previous
    turn = @turn + (@reverse ? 1 : -1)
    @turn = turn % @members.size
  end

  def suffer2
    @members[@turn].penalty += @bomb
    @bomb = 0
    @prev2 = false
  end

  def suffer4
    @members[@turn].penalty += @bomb
    @bomb = 0
    @prev4 = false
  end

  def consume
    @members[@turn].change += 1
  end

  #
  # represent drunk
  #
  class Drunk
    attr_accessor :change, :penalty

    def original
      @change - @penalty
    end

    private

    def initialize
      @change = 0
      @penalty = 0
    end
  end
end

# -------------------------------------- exec

N, M = gets.chomp.split.map(&:to_i)
LS = M.times.map { gets.chomp }

drunks = Drunks.new(N)
LS.each.with_index do |type, index|
  last = index == LS.size - 1
  case type
  when 'number'
    drunks.number(last)
  when 'drawtwo'
    drunks.draw2(last)
  when 'drawfour'
    drunks.draw4(last)
  when 'skip'
    drunks.skip(last)
  when 'reverse'
    drunks.reverse(last)
  end
end
RESULT = drunks.winner

puts RESULT
0