From ff6f142e2b4c7531d20c77a999e449b9c8a6df5b Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sun, 5 Dec 2021 22:12:50 -0800 Subject: [PATCH] [2021][ruby][6.x] --- 2021/ruby/day_06.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 2021/ruby/day_06.rb diff --git a/2021/ruby/day_06.rb b/2021/ruby/day_06.rb new file mode 100644 index 0000000..4f609aa --- /dev/null +++ b/2021/ruby/day_06.rb @@ -0,0 +1,22 @@ +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] }