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

Loading…
Cancel
Save