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