[2023][ruby][9.x] dry

main
Alpha Chen 12 months ago
parent 7f25d2bde6
commit b5bfbcff0b
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -1,26 +1,17 @@
histories = ARGF.readlines.map { _1.scan(/-?\d+/).map(&:to_i) }
# part one
p histories
.map {|history|
diffs = [history]
until diffs.last.all?(&:zero?)
diffs << diffs.last.each_cons(2).map { _2 - _1 }
end
history_diffs = histories.map {|history|
diffs = [history]
until diffs.last.all?(&:zero?)
diffs << diffs.last.each_cons(2).map { _2 - _1 }
end
diffs
}
diffs.sum(&:last)
}
.sum
# part one
p history_diffs.sum { _1.sum(&:last) }
# part two
p histories
.map {|history|
diffs = [history]
until diffs.last.all?(&:zero?)
diffs << diffs.last.each_cons(2).map { _2 - _1 }
end
diffs.map(&:first).reverse.inject { _2 - _1 }
}
.sum
p history_diffs.sum {|diffs|
diffs.map(&:first).reverse.inject { _2 - _1 }
}

Loading…
Cancel
Save