exercism/elixir/space-age/space_age.exs
2017-07-20 20:33:09 -04:00

35 lines
1,021 B
Elixir

defmodule SpaceAge do
@type planet :: :mercury | :venus | :earth | :mars | :jupiter
| :saturn | :uranus | :neptune
@doc """
Return the number of years a person that has lived for 'seconds' seconds is
aged on 'planet'.
"""
@spec age_on(planet, pos_integer) :: float
def age_on(planet, seconds) do
cond do
planet == :mercury ->
earth_age(seconds) / 0.2408467
planet == :venus ->
earth_age(seconds) / 0.61519726
planet == :earth ->
earth_age(seconds)
planet == :mars ->
earth_age(seconds) / 1.8808158
planet == :jupiter ->
earth_age(seconds) / 11.862615
planet == :saturn ->
earth_age(seconds) / 29.447498
planet == :uranus ->
earth_age(seconds) / 84.016846
planet == :neptune ->
earth_age(seconds) / 164.79132
end
end
def earth_age(seconds) do
seconds / 31557600
end
end