[2022][ruby][13.x] refactored

pull/1/head
Alpha Chen 2 years ago
parent d246206681
commit c5846c52e9
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -8,8 +8,8 @@ def compare(left, right)
left.zip(right).each do |left, right| left.zip(right).each do |left, right|
case compare(left, right) case compare(left, right)
when -1 then return -1 when -1 then return -1
when 0 # no-op when 0 # keep going
when 1 then return 1 when 1 then return 1
end end
end end
(left.size == right.size) ? 0 : -1 (left.size == right.size) ? 0 : -1
@ -19,17 +19,19 @@ def compare(left, right)
end end
# part one # part one
# pairs = ARGF.read.split("\n\n") pairs = ARGF.read.split("\n\n")
# pairs = pairs.map {|pair| pair.lines(chomp: true).map { eval(_1) }} pairs = pairs.map {|pair| pair.lines(chomp: true).map { eval(_1) }}
# p pairs.map.with_index {|(left,right),i| p pairs.map.with_index
# [compare(left, right), i+1] .select {|(l,r),_| compare(l, r) == -1 }
# }.select {|cmp,_| cmp == -1 }.sum(&:last) .sum { _1.last + 1 }
# part two # part two
pairs = ARGF.read.lines(chomp: true).reject(&:empty?).map { eval(_1) } pairs = pairs.flatten(1)
pairs << [[2]] << [[6]]
dividers = [[[2]], [[6]]]
pairs.concat(dividers)
pairs = pairs.sort { compare(_1, _2) } pairs = pairs.sort { compare(_1, _2) }
a = pairs.index([[2]]) p dividers
b = pairs.index([[6]]) .map { pairs.index(_1) + 1 }
p (a+1)*(b+1) .inject(:*)

Loading…
Cancel
Save