Created
March 27, 2025 20:30
-
-
Save hiro-riveros/80750d4adb2a1d5eb1224219b6a22f51 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'byebug' | |
require 'benchmark' | |
aliens = [3, 4, 3, 1, 2] | |
def check_lifecycle(age: 0) | |
return 0 if age.zero? | |
age - 1 | |
end | |
def spawn_alien(aliens: []) | |
1.upto(80) do | |
new_aliens = [] | |
aliens.map! do |alien| | |
new_alien = false | |
result = check_lifecycle(age: alien) | |
new_alien = true if alien.zero? | |
new_aliens << 8 if new_alien | |
alien.zero? ? 6 : result | |
end | |
aliens += new_aliens | |
puts "day #{day}: #{aliens}" | |
end | |
aliens.size | |
end | |
puts Benchmark.measure { spawn_alien(aliens: aliens) } | |
# 17:13 solucion final |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment