# Day 19

## Exercises

* [x] [Need For Speed](#need-for-speed)

### Need For Speed

[Exercise on Exercism](https://exercism.org/tracks/elixir/exercises/need-for-speed) **|** [View my solution](https://exercism.org/tracks/elixir/exercises/need-for-speed/solutions/petros)

#### Solution

<details>

<summary>Expand to see code (spoiler alert)</summary>

{% code lineNumbers="true" %}

```elixir
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

```

{% endcode %}

[View gist on GitHub](https://gist.github.com/petros/5d3f62baccd06ae629942defa1121964)

</details>

#### Notes

Interesting. Pretty short and fast. Learned a few things about aliases and imports. I still don't fully understand what is the difference between `alias` and `import`.

## Overall progress

<figure><img src="https://722931016-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDUq5E45vIeUDn4lY3Psn%2Fuploads%2F4vTUXpaB1GTR6JS1DTC2%2F20220826-exhort-day19-progress.png?alt=media&#x26;token=1c4409b4-77d8-4d13-ad43-be310bc97514" alt="An image showing my progress on Exercism. It&#x27;s 21.2% as of Friday, 26 August 2022."><figcaption><p>Progress</p></figcaption></figure>
