Day 14
Mix.install([
{:kino, "~> 0.14.2"}
])
IEx.Helpers.c("/Users/johnb/dev/2015adventOfCode/advent_of_code.ex")
alias AdventOfCode, as: AOC
alias Kino.Input
require Integer
# Note: when making the next template, something like this works well:
# `cat day01.livemd | sed 's/01/02/' > day02.livemd`
#
Installation and Data
input_p1example = Kino.Input.textarea("Example Data")
input_p1puzzleInput = Kino.Input.textarea("Puzzle Input")
input_source_select =
Kino.Input.select("Source", [{:example, "example"}, {:puzzle_input, "puzzle input"}])
p1data = fn ->
(Kino.Input.read(input_source_select) == :example &&
Kino.Input.read(input_p1example)) ||
Kino.Input.read(input_p1puzzleInput)
end
Part 1
defmodule Day14 do
@parser ~r/(\w+) can fly (\d+) km\/s for (\d+) seconds, but then must rest for (\d+) seconds./
def parse_reindeer(text) do
text
|> AOC.as_single_lines()
|> Enum.reduce(%{}, fn line, acc ->
[[_, who, speed, duration, rest]] = Regex.scan(@parser, line)
[speed, duration, rest] = Enum.map([speed, duration, rest], &String.to_integer/1)
acc
|> Map.put(who,
%{speed: speed,
duration: duration,
rest: rest,
sprint: duration + rest})
end)
end
def solve1(text) do
reindeer = parse_reindeer(text)
finish = (Enum.count(reindeer) == 2) && 1000 || 2503
reindeer
|> Enum.map(fn {who, %{speed: speed, duration: duration, rest: rest} = entry} ->
sprints = Float.floor(finish / (duration + rest))
distance = speed * duration * sprints +
speed * min(duration, finish - (sprints * (duration + rest)))
[distance, who, sprints, entry]
end)
|> Enum.sort_by(&List.first/1)
end
def solve2(text) do
reindeer = parse_reindeer(text)
|> IO.inspect(label: "reindeer")
finish = (Enum.count(reindeer) == 2) && 1000 || 2503
result = Enum.reduce(reindeer, %{}, fn {name, _}, acc ->
put_in(acc[name], %{position: 0, score: 0})
end)
|> IO.inspect(label: "result?")
Enum.reduce(0..finish, result, fn second, acc ->
# IO.inspect(acc)
if second > 2490 do
IO.inspect([second,
"Comet", acc["Comet"].position, acc["Comet"].score,
"Dancer", acc["Dancer"].position, acc["Dancer"].score])
end
current_state = acc
|> Enum.reduce(%{}, fn {name, %{position: position, score: score}}, map ->
%{speed: speed, duration: duration, sprint: sprint} = reindeer[name]
# |> IO.inspect(label: name)
is_resting = (rem(second, sprint) >= duration)
# |> IO.inspect(label: "is_resting")
movement = (is_resting && 0 || speed)
Map.put(map, name, %{position: position + movement, score: score})
end)
# |> IO.inspect()
farthest = current_state
|> Enum.map(fn {_name, entry} -> entry.position end)
|> Enum.max()
current_state
# |> IO.inspect(label: "current_state")
|> Enum.reduce(%{}, fn {name, %{position: position, score: score}}, map ->
# if second in (130..140) or second in (310..340) do
# IO.inspect([second, name, position, score, farthest])
# end
Map.put(map, name, %{
position: position,
score: score + ((position == farthest) && 1 || 0)
})
end)
end)
|> Enum.sort_by(fn {_name, %{position: _position, score: score}} -> score end)
end
end
# Example data:
# p1data.()
# |> Day14.solve1()
# |> IO.inspect(label: "\n*** Part 1 solution (example: 2)")
# 2655 for Donner
p1data.()
|> Day14.solve2()
|> IO.inspect(label: "\n*** Part 2 solution (example: 2)")
# 1059 (but ugh what a slog)