Created
April 30, 2016 00:45
-
-
Save vznvzn/a52dca6a8ec2f3b692eb04ebdadc2aa6 to your computer and use it in GitHub Desktop.
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
def units(l) | |
v = {} | |
i = 0 | |
while (i < l.size) \ | |
if (l[i].size == 1) then | |
v[l[i][0]] = nil | |
l.delete_at(i) | |
next | |
end | |
i += 1 | |
end | |
return v.keys | |
end | |
def unit(l) | |
u = units(l) | |
u2 = u.map { |x| -x } | |
i = 0 | |
f = false | |
while (i < l.size) | |
f = true if (!(l[i] & (u + u2)).empty?) | |
if (!(l[i] & u).empty?) | |
l.delete_at(i) | |
next | |
end | |
l[i] -= u2 | |
i += 1 | |
end | |
l.concat(u.map { |x| [x] }) | |
return f | |
end | |
l = readlines.map { |x| x.split().map { |x| x.to_i } } | |
begin end while (unit(l)) | |
raise if (!l.select { |x| x.size != 1 }.empty?) | |
p(l.flatten.sort_by { |x| x.abs }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment