Advent of Code 2015 Day 14 Part 1
Mix.install([
{:kino_aoc, "~> 0.1"}
])
Get Inputs
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2015", "14", System.fetch_env!("LB_SESSION"))
My answer
Regex.named_captures(
~r/(?[a-zA-Z]+) can fly (?\d+) km\/s for (?\d+) seconds, but then must rest for (?\d+)/,
"Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds."
)
reindeer_list =
puzzle_input
|> String.split("\n")
|> Enum.map(fn row ->
Regex.named_captures(
~r/(?[a-zA-Z]+) can fly (?\d+) km\/s for (?\d+) seconds, but then must rest for (?\d+)/,
row
)
|> then(fn %{"speed" => speed, "keep" => keep, "rest" => rest} ->
%{
speed: String.to_integer(speed),
keep: String.to_integer(keep),
rest: String.to_integer(rest),
distance: 0
}
end)
end)
1..2503
|> Enum.reduce(reindeer_list, fn sec, acc_reindeer_list ->
acc_reindeer_list
|> Enum.map(fn reindeer ->
remain = rem(sec, reindeer.keep + reindeer.rest)
if remain > 0 and remain <= reindeer.keep do
Map.put(reindeer, :distance, reindeer.distance + reindeer.speed)
else
reindeer
end
end)
end)
|> Enum.map(&(&1.distance))
|> Enum.max()