exercism/elixir/need-for-speed/lib/need_for_speed.ex

44 lines
980 B
Elixir
Raw Normal View History

2024-03-07 05:52:53 +00:00
defmodule NeedForSpeed do
alias NeedForSpeed.Race
alias NeedForSpeed.RemoteControlCar, as: Car
import IO, only: [puts: 1]
import IO.ANSI, except: [color: 1]
# Add missing aliases and imports here.
# Do not edit the code below.
def print_race(%Race{} = race) do
puts("""
🏁 #{race.title} 🏁
Status: #{Race.display_status(race)}
Distance: #{Race.display_distance(race)}
Contestants:
""")
race.cars
|> Enum.sort_by(&(-1 * &1.distance_driven_in_meters))
|> Enum.with_index()
|> Enum.each(fn {car, index} -> print_car(car, index + 1) end)
end
defp print_car(%Car{} = car, index) do
color = color(car)
puts("""
#{index}. #{color}#{car.nickname}#{default_color()}
Distance: #{Car.display_distance(car)}
Battery: #{Car.display_battery(car)}
""")
end
defp color(%Car{} = car) do
case car.color do
:red -> red()
:blue -> cyan()
:green -> green()
end
end
end