Create a gist now

Instantly share code, notes, and snippets.

@takkanm /iekei.rb Secret
Last active Oct 22, 2015

class Seat
attr_reader :state
STATES = {
# current => :next
empty: :wait,
wait: :eating,
eating: :rest,
rest: :empty
}
def initialize
@state = :empty
end
def next_state!
@state = STATES[@state] unless empty?
end
def sitdown!
@state = :wait
end
def empty?
@state == :empty
end
end
class RamenShop
def initialize(size)
@seats_size = size
@seats = (1..@seats_size).to_a.map{ Seat.new }
end
def guide(member_count)
@seats.each_with_index do |_, seat_index|
next unless @seats[seat_index].empty?
seats = (seat_index..(seat_index + member_count - 1)).to_a.map {|x|
@seats[x < @seats_size ? x : (x - @seats_size)]
}
return seats.each(&:sitdown!) if seats.all?(&:empty?)
end
raise
rescue
next_state!
retry
end
def next_state!
@seats.each(&:next_state!)
end
def dump
p @seats.map(&:state)
end
def seats_map
@seats.map {|s| s.empty? ? 0 : 1 }
end
end
def test(visitor_group_str, result)
ramen_shop = RamenShop.new(8)
visitor_groups = visitor_group_str.chars.map(&:to_i)
visitor_groups.each do |visitor_group|
ramen_shop.next_state!
ramen_shop.guide(visitor_group)
end
p(ramen_shop.seats_map.join == result)
end
test("3316", "11111110")
test("3342153", "11111111")
test("8", "11111111")
test("232624", "11110110")
test("1", "10000000")
test("224673432", "11111000")
test("123464334", "11111110")
test("44372332", "11111111")
test("2344", "11111111")
test("6448476233", "11111100")
test("4345174644", "11111111")
test("77743", "11111110")
test("2136524281", "10000000")
test("344373", "11100000")
test("434226", "11111111")
test("7433223", "11111110")
test("2246534", "11110111")
test("634", "11111110")
test("2222", "11111100")
test("2442343238", "11111111")
test("7243344", "11111111")
test("26147234", "10111111")
test("34424", "10011111")
test("6334", "11011111")
test("3828342", "11011110")
test("4431", "11110000")
test("2843212125", "11111111")
test("333336482", "11000000")
test("374", "11110000")
test("4382333", "11100111")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment