Skip to content

Instantly share code, notes, and snippets.

@makoto
Created May 2, 2018 20:37
Show Gist options
  • Save makoto/7d1d4ae97035617b856415947fb3989c to your computer and use it in GitHub Desktop.
Save makoto/7d1d4ae97035617b856415947fb3989c to your computer and use it in GitHub Desktop.
# game.rb
require 'pry'
class Compare
def self.run(s, o)
s_off = 0
o_off = 0
return 0 if compare(s, s_off, o, o_off) == 0
s_head = ''
o_head = ''
s_length = label_count(s,s_off)
o_length = label_count(o,o_off)
while compare(s, s_off, o, o_off) != 0
s_head, s_off = progress(s, s_off) if s_length >= o_length
o_head, o_off = progress(o, o_off) if s_length <= o_length
s_length = label_count(s,s_off) unless s_length == 0
o_length = label_count(o,o_off) unless o_length == 0
# puts "s_length:#{s_length} : s_off #{s_off} : o_length:#{o_length} : o_off #{o_off}"
break if s_length == 0 && o_length == 0
end
puts "exit: #{s_head} <=> #{o_head} : #{s_head <=> o_head}: #{compare(s, s_off, o, o_off)}"
s_head <=> o_head
end
def self.label_count(s, off)
s[off..-1].gsub(/\D/, ',').split(',').count
end
def self.compare(s, s_off, o, o_off)
# puts "#{s[s_off..-1]} <=> #{o[o_off..-1]} = #{s[s_off..-1] <=> o[o_off..-1]}"
s[s_off..-1] <=> o[o_off..-1]
end
def self.progress(string, off)
length = string[off].to_i
start = off + 1
ending = start + length
head = string[start...ending]
off = off + 1 + length
[head, off]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment