結果

問題 No.714 回転寿司屋のシミュレート
ユーザー Yosuke KonoYosuke Kono
提出日時 2018-10-17 22:52:00
言語 Ruby
(3.3.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 2,732 bytes
コンパイル時間 66 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 14,464 KB
最終ジャッジ日時 2024-04-20 20:49:25
合計ジャッジ時間 4,623 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
12,288 KB
testcase_01 AC 90 ms
12,032 KB
testcase_02 AC 90 ms
12,160 KB
testcase_03 AC 91 ms
12,288 KB
testcase_04 AC 94 ms
12,288 KB
testcase_05 AC 97 ms
12,544 KB
testcase_06 AC 98 ms
12,544 KB
testcase_07 AC 107 ms
13,312 KB
testcase_08 AC 114 ms
14,464 KB
testcase_09 AC 100 ms
12,800 KB
testcase_10 AC 99 ms
12,672 KB
testcase_11 AC 104 ms
12,928 KB
testcase_12 AC 101 ms
12,672 KB
testcase_13 AC 99 ms
12,800 KB
testcase_14 AC 97 ms
12,672 KB
testcase_15 AC 98 ms
12,544 KB
testcase_16 AC 97 ms
12,672 KB
testcase_17 AC 98 ms
12,800 KB
testcase_18 AC 99 ms
12,672 KB
testcase_19 AC 98 ms
12,672 KB
testcase_20 AC 97 ms
12,672 KB
testcase_21 AC 98 ms
12,800 KB
testcase_22 AC 94 ms
12,288 KB
testcase_23 AC 98 ms
12,416 KB
testcase_24 AC 98 ms
12,160 KB
testcase_25 AC 97 ms
12,544 KB
testcase_26 AC 96 ms
12,544 KB
testcase_27 AC 93 ms
12,288 KB
testcase_28 AC 94 ms
12,544 KB
testcase_29 AC 94 ms
12,544 KB
testcase_30 AC 89 ms
12,032 KB
testcase_31 AC 91 ms
12,160 KB
testcase_32 AC 91 ms
12,160 KB
testcase_33 AC 90 ms
12,288 KB
testcase_34 AC 89 ms
12,288 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:97: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Main.rb:79: warning: assigned but unused variable - want_toppings_size
Syntax OK

ソースコード

diff #

class Customer
  attr_accessor :position # 席番号
  attr_accessor :want_toppings # 欲しいネタリスト

  def initialize(position, toppings)
    @position, @want_toppings = position, toppings
  end

  def eat(topping_name)
    index = @want_toppings.index(topping_name)
    unless index.nil?
      @want_toppings.delete_at(index)
      return true
    end
    false
  end
end


class SushiDish
  attr_accessor :position # 席番号
  attr_accessor :topping_name # ネタ名

  def initialize(topping_name, position = 1)
    @topping_name, @position = topping_name, position
  end

  # 皿の移動。移動後の席の位置を返す。最後の席の位置にいたら nil を返す
  def move
    if @position < ConveyorBeltSushiSystem::SEAT_LENGTH
      @position += 1
    else
      @position = nil
    end
  end
end


class ConveyorBeltSushiSystem
  SEAT_LENGTH = 20

  attr_accessor :dishes
  attr_accessor :customers

  def initialize
    @dishes = Array.new(self.class::SEAT_LENGTH, nil)
    @customers = Array.new(self.class::SEAT_LENGTH, nil)
  end

  def go_round
    @customers.each do |customer|
      @dishes.each do |dish|
        customer.eat(dish)
      end
    end
    @dishes.each do |dish|
      dish.move
    end
  end
end

DATA_CUSTOMER = 0 # 客がくる。「[1]席番号、[2]食べたいものリストの要素数、[3]n食べたいもの1、...、[4+n]食べたいものn」
DATA_SUSHI = 1 # 寿司がコンベアに流される。「寿司ネタの名前」
DATA_CHECK = 2 # 客が会計する。「会計する客の席番号」

system = ConveyorBeltSushiSystem.new
data_length = gets.to_i
data_lines = []
data_length.times do
  data_lines.push(gets.split)
end

data_lines.each do |data|
  data[0] = data[0].to_i

  if data[0] == DATA_CUSTOMER
    want_topping_data_starts_at = 3 # データタイプ 0 の食べたいものリストが始まる要素番号
    position = data[1].to_i # 客が座る座席番号
    want_toppings_size = data[2].to_i
    want_toppings = [] # 食べたいものリスト
    (want_topping_data_starts_at..data.size).each do |index|
      want_toppings.push(data[index])
    end

    customer = Customer.new(position, want_toppings)
    system.customers[customer.position - 1] = customer
  elsif data[0] == DATA_SUSHI
    topping_name = data[1] # 寿司ネタの名前
    is_eaten = false
    system.customers.compact.each do |customer|
      is_eaten = customer.eat(topping_name) # 客が食べたら次のデータの処理へ
      if is_eaten
        puts customer.position
        break
      end
    end
    puts -1 unless is_eaten
  elsif data[0] == DATA_CHECK
    system.customers[data[1].to_i - 1] = nil
  else
    raise 'invalid data type.'
  end
end
0