You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
advent-of-code/2023/ruby/day_09.rb

18 lines
370 B

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