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/2021/ruby/day_06.rb

23 lines
479 B

ages = ARGF.read.split(?,).map(&:to_i)
# 80.times do
# new_fish = 0
# ages.map! {|age|
# if age.zero?
# new_fish += 1
# 6
# else
# age - 1
# end
# }
# ages.concat(Array.new(new_fish) { 8 })
# end
# p ages.count
DURATION = 256
DESCENDANTS = Hash.new {|h,k|
children = (k...DURATION).step(7)
h[k] = children.count + children.map {|d| d + 9 }.select {|d| d < DURATION }.sum {|d| h[d] }
}
p ages.count + ages.sum {|age| DESCENDANTS[age] }