-
-
Save olistik/63b5870eac5ac7323d21 to your computer and use it in GitHub Desktop.
MCC #0 solution
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
class Combinations | |
def initialize options = {} | |
@input = options[:input] | |
@lengths = @input.map &:size | |
@counters = @lengths.map { 0 } | |
@current = @input.map {|line| line[0]}.join | |
end | |
def each | |
begin | |
yield @current | |
end while clock! | |
end | |
private | |
def clock! | |
index = 0 | |
while index < @input.size && (@counters[index] += 1) == @lengths[index] | |
@counters[index] %= @lengths[index] | |
update_current! index | |
index += 1 | |
end | |
if index < @input.size | |
update_current! index | |
true | |
else | |
false | |
end | |
end | |
def update_current! index | |
@current[index] = @input[index][@counters[index]] | |
end | |
end | |
class Solution | |
def initialize | |
input = STDIN.readlines.map &:strip | |
combinations = Combinations.new input: input | |
combinations.each do |combination| | |
puts combination | |
end | |
end | |
end | |
Solution.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment