Skip to content

Instantly share code, notes, and snippets.

@petros
Last active August 28, 2022 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petros/5d3f62baccd06ae629942defa1121964 to your computer and use it in GitHub Desktop.
Save petros/5d3f62baccd06ae629942defa1121964 to your computer and use it in GitHub Desktop.
Need For Speed - Elixir - Exercism
defmodule NeedForSpeed do
alias NeedForSpeed.Race
alias NeedForSpeed.RemoteControlCar, as: Car
import IO, only: [puts: 1]
import IO.ANSI, except: [color: 1]
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment