Create a gist now

Instantly share code, notes, and snippets.

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