Skip to content

Instantly share code, notes, and snippets.

@aikyo02
Last active October 22, 2015 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aikyo02/9ddd7b29460f429c7a35 to your computer and use it in GitHub Desktop.
Save aikyo02/9ddd7b29460f429c7a35 to your computer and use it in GitHub Desktop.
class OfficeTable
def initialize
@table = Array.new(8, 0)
end
def attend(customer_count)
if @table.all?{ |e| e == 0}
for i in 0..(customer_count-1) do
@table[i] = 3
end
return true
end
for i in 0..7 do
j = 0
while @table[(i + j) % 8] == 0
j += 1
end
# p "#{j}, #{customer_count}"
if customer_count <= j
for k in i..(i + customer_count-1) do
@table[k%8] = 3
end
return true
end
end
return false
end
def next_turn!
for i in 0..7 do
table[i] = table[i] - 1 if table[i] > 0
end
end
def table
@table
end
def result
@table.map { |e| e == 0 ? 0 : 1 }.join
end
end
def test(problem, expect)
table = OfficeTable.new
input = problem.split('')
until input.empty?
customer_count = input.shift
table.next_turn!
until table.attend(customer_count.to_i)
table.next_turn!
end
end
if table.result == expect
p "pass"
else
p "failed"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment