AoC20/lib/day12/day12.ex

128 lines
2.7 KiB
Elixir
Raw Normal View History

2020-12-12 16:50:48 +00:00
defmodule Day12 do
@example """
F10
N3
F7
R90
F11
"""
def part1(example \\ false) do
{x, y, _} =
example
|> parse_input()
|> follow_move_instructions({0, 0, 90})
manhattan_dist({x, y})
end
def part2(example \\ false) do
example
|> parse_input()
|> follow_waypoint_instructions({0, 0}, {10, 1})
|> manhattan_dist()
end
def parse_input(example) do
if(example, do: @example, else: File.read!("lib/day12/input.txt"))
|> String.trim()
|> String.split()
|> Enum.map(fn <<insn::binary-size(1), rest::binary>> ->
{insn, String.to_integer(rest)}
end)
end
def follow_move_instructions([], state) do
state
end
def follow_move_instructions([insn | rest], state) do
follow_move_instructions(rest, move(state, insn))
end
def move({x, y, dir}, {"L", val}) do
new_dir = rem(dir - val, 360)
new_dir = if new_dir < 0, do: new_dir + 360, else: new_dir
{x, y, new_dir}
end
def move({x, y, dir}, {"R", val}) do
{x, y, rem(dir + val, 360)}
end
def move({_, _, dir} = state, {"F", val}) do
index = if rem(dir, 180) == 0, do: 1, else: 0
sign = if dir == 180 || dir == 270, do: -1, else: 1
put_elem(state, index, elem(state, index) + sign * val)
end
def move({x, y, dir}, {"N", val}) do
{x, y + val, dir}
end
def move({x, y, dir}, {"S", val}) do
{x, y - val, dir}
end
def move({x, y, dir}, {"E", val}) do
{x + val, y, dir}
end
def move({x, y, dir}, {"W", val}) do
{x - val, y, dir}
end
def manhattan_dist({x, y}) do
abs(x) + abs(y)
end
def follow_waypoint_instructions([], ship, waypoint) do
ship
end
def follow_waypoint_instructions([insn | rest], ship, waypoint) do
{new_ship, new_waypoint} = do_waypoint_insn(insn, ship, waypoint)
follow_waypoint_instructions(rest, new_ship, new_waypoint)
end
def do_waypoint_insn({"N", val}, ship, {dx, dy}) do
{ship, {dx, dy + val}}
end
def do_waypoint_insn({"S", val}, ship, {dx, dy}) do
{ship, {dx, dy - val}}
end
def do_waypoint_insn({"E", val}, ship, {dx, dy}) do
{ship, {dx + val, dy}}
end
def do_waypoint_insn({"W", val}, ship, {dx, dy}) do
{ship, {dx - val, dy}}
end
def do_waypoint_insn({"L", val}, ship, {dx, dy}) do
new_waypoint =
case val do
90 ->
{-dy, dx}
180 ->
{-dx, -dy}
270 ->
{dy, -dx}
end
{ship, new_waypoint}
end
def do_waypoint_insn({"R", val}, ship, waypoint) do
do_waypoint_insn({"L", 360 - val}, ship, waypoint)
end
def do_waypoint_insn({"F", val}, {x, y}, {dx, dy} = waypoint) do
{{x + val * dx, y + val * dy}, waypoint}
end
end