Shuttle Search
defmodule Aoc2020.Day13 do
@moduledoc "Shuttle Search"
def run(), do: part_1()
def part_1() do
{timestamp, buses} = parse_input()
Enum.map(buses, fn bus -> {bus, minutes_after_timestamp(timestamp, bus)} end)
|> Enum.sort_by(&elem(&1, 1))
|> List.first()
|> (fn {bus, minutes} -> bus * minutes end).()
end
def parse_input() do
File.read!("priv/inputs/2020/day13.txt")
|> String.trim()
|> String.split("\n")
|> (fn [timestamp, line] ->
{String.to_integer(timestamp), parse_buses(line)}
end).()
end
def parse_buses(line) do
line
|> String.split(",")
|> Enum.filter(&(&1 != "x"))
|> Enum.map(&String.to_integer/1)
end
def minutes_after_timestamp(timestamp, bus) do
multiple = floor(timestamp / bus)
bus * (multiple + 1) - timestamp
end
end