Skip to content

Instantly share code, notes, and snippets.

@olistik
Created March 24, 2012 10:29
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 olistik/63b5870eac5ac7323d21 to your computer and use it in GitHub Desktop.
Save olistik/63b5870eac5ac7323d21 to your computer and use it in GitHub Desktop.
MCC #0 solution
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