This commit is contained in:
Shadowfacts 2020-12-12 11:50:48 -05:00
parent e44dd74a7d
commit ab1e845162
2 changed files with 903 additions and 0 deletions

127
lib/day12/day12.ex Normal file
View File

@ -0,0 +1,127 @@
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

776
lib/day12/input.txt Normal file
View File

@ -0,0 +1,776 @@
R180
E1
N1
R90
E4
F84
W3
F19
E5
N1
W5
W4
R90
S1
F70
R90
W2
S3
L180
E2
R90
E1
R90
N2
E3
F55
W3
R90
S2
E4
L90
F33
W4
S3
F11
N4
W4
F88
W3
F62
L90
S3
W3
N1
E3
E2
N1
E3
S3
E5
S4
W5
L90
W5
R90
E4
F36
N4
E4
E5
F47
R90
N3
E3
L180
S5
R90
S5
W2
S3
F43
W2
R180
W2
S2
L180
E2
F49
L90
F77
S5
E1
S2
F39
L180
F12
W1
L90
F60
S2
E3
N1
E5
R90
E1
N4
W4
S4
E2
L90
F100
E3
F58
S2
E4
F83
W3
N1
R90
F99
W5
W4
F19
N3
W1
W5
F70
R180
R90
F58
E5
N3
R90
N4
F86
N4
F70
L90
F36
R90
S4
R180
N5
F81
W5
R90
E5
L90
F73
S5
E3
N1
F27
E2
N3
L180
W2
F24
N2
L90
F69
N2
E3
F49
L90
S4
W5
F18
R180
F62
S2
F65
E3
F100
N1
F96
W1
F75
L180
W2
S2
F93
E5
S3
R90
S2
F34
R90
F83
S1
W1
F58
L180
N4
E1
R90
W2
F93
S3
F20
R90
N3
E3
N3
L90
F8
S3
F18
S3
N2
F6
L270
F70
W5
S3
F54
E2
F75
E3
R90
N2
W3
L90
E4
F58
N5
F97
W2
L90
W5
S2
W1
S5
F85
N1
E1
N4
E4
L90
E1
R90
S5
L180
R270
N1
L90
E4
N3
F45
N2
F68
R90
F36
N5
F82
S5
E4
R180
S2
L180
N3
R270
W5
F70
L90
W5
F80
W4
N2
R90
S3
W1
F23
N1
W1
N4
F70
S3
L180
F57
R90
L90
F55
L90
N4
F87
L90
F1
L270
F17
N5
R180
F84
R90
W5
F7
W1
S1
E3
F46
S5
E2
F23
R90
E4
W2
F96
E5
L90
F65
F3
S5
E5
N3
W4
L90
S2
F57
E1
R90
F68
E3
L90
W1
F29
N5
W5
N1
F95
N1
L90
F31
S5
L180
N2
W5
R90
F27
E1
R90
E3
S5
F10
R90
N4
E2
F25
S4
E5
F51
N3
W2
L90
S3
L180
F17
E4
F93
E3
L90
F41
L90
S5
L90
W5
N1
F81
L90
E4
W2
R90
W1
S5
R90
F39
W3
R90
N5
E1
L90
F82
S3
R90
W4
F66
F4
L90
F77
R90
E1
L90
F53
S4
F35
W1
F64
R90
F9
S1
E1
L90
W4
R90
S2
W5
R90
S4
L90
N3
F8
L180
N5
E5
N4
F35
N5
W1
N1
E5
F15
R180
F92
W3
L90
F4
L90
E1
S3
W3
R90
F37
N5
F19
S2
F98
L90
F24
W3
F68
N5
R90
W3
L90
W3
L90
S1
L90
S4
W3
F56
N4
R90
E3
W1
L90
E4
N3
R180
E1
S1
W2
R90
N3
F82
N2
F37
S3
L180
E2
L180
F6
N2
F96
E2
R180
E2
W3
R90
E2
S5
S1
F23
R90
W5
F75
S1
L90
S3
E1
F83
W4
L180
W5
L90
N1
E1
S2
F17
L90
S2
F53
R90
S3
N3
W1
N4
L180
L90
E3
F9
S5
F24
W3
E5
N2
F73
N1
F28
N2
W4
N3
F53
E5
F47
W2
F60
L90
E2
F19
S1
F63
W5
F100
N3
L180
F83
N4
W5
F37
S1
F50
E1
N2
W3
R90
F85
S4
F72
N4
L90
F48
R90
F99
R90
F58
W3
W4
F64
E1
R90
F74
L90
F23
N3
N3
E1
S1
W5
L180
F98
L90
F36
W4
S2
W3
F9
F72
W5
F78
N2
F65
S3
F47
S5
R90
F68
L180
W2
F7
E2
E3
S4
R90
N2
L180
W2
R180
E4
R90
W3
L90
E4
F54
L180
E2
F6
W5
F82
E4
R90
E4
F25
N2
R270
N4
F18
N5
R90
S3
R90
F38
R90
F97
W4
F85
S4
F56
E4
S1
F40
W3
F52
L90
F76
N4
F15
S2
F22
S5
L180
F91
L180
F8
L90
E4
N4
F67
L90
S3
R180
R90
N4
F71
W3
F34
E2
N1
F43
W5
L180
N5
W2
F42
R90
W3
F39
E1
S2
L180
N5
E3
N5
F28
E1
R90
S3
F40
L90
S2
S2
L90
W5
L90
F93
R180
W4
S4
W4
F100
S3
R90
E2
L180
W1
E3
S5
L90
F87
N1
R90
F3
R90
E5
R90
S3
F45
L90
S2
F42
R90
F95
L90
E1
N3
R90
F73
S3
E1
L90
S2
E3
L90
L270
F38
S5
R90
F42
L90
N1
F7
S3
F65
N2
F42
L180
W5
S4
E4
F65
S4
E5
F51
E4
R180
F70
R90
F28
N5
W5
N1
F96
L90
W4
S3
W3
F89
W1
L90
F75
L270
S3
R90
L90
F7
E2
F24
R180
S2
L180
F48
R90
F37
W2
R90
W4
L90
W3
F81
E4
N2
F39
E4
N1
W1
L90
F59