Last active
October 23, 2015 16:36
-
-
Save mtsmfm/4f11795ad0d1bccc9d75 to your computer and use it in GitHub Desktop.
esm_doukaku_20151022
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'bundler' | |
Bundler.require | |
SEATS_COUNT = 8 | |
class Hi | |
def initialize(input) | |
@input = input | |
@eating = @cooking = @just_entered = 0 | |
end | |
def queue | |
@input.each_char.map(&:to_i) | |
end | |
def execute! | |
queue.each.with_index do |input, index| | |
succ | |
if enterable?(input) | |
enter(input) | |
else | |
redo | |
end | |
end | |
whole_str | |
end | |
def succ | |
@eating = @cooking | |
@cooking = @just_entered | |
@just_entered = 0 | |
end | |
def enterable?(headcount) | |
!!enterable_index(headcount) | |
end | |
def enter(headcount) | |
index = enterable_index(headcount) | |
@just_entered = (([1] * headcount) + [0] * (SEATS_COUNT - headcount)).rotate(-index).join.to_i(2) | |
end | |
def enterable_index(headcount) | |
(whole_str + whole_str).index(/0{#{headcount}}/) | |
end | |
def whole | |
@eating | @cooking | @just_entered | |
end | |
def whole_str | |
"%0#{SEATS_COUNT}b" % whole | |
end | |
end | |
def test(input, expect) | |
if Hi.new(input).execute! == expect | |
puts 'ok' | |
else | |
raise 'failed' | |
end | |
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'activesupport', require: 'active_support/all' | |
gem 'pry-byebug' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment