parent
ca65631b37
commit
7f25d2bde6
@ -0,0 +1,26 @@
|
||||
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
|
||||
|
||||
diffs.sum(&:last)
|
||||
}
|
||||
.sum
|
||||
|
||||
# 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
|
||||
|
Loading…
Reference in new issue