-
Notifications
You must be signed in to change notification settings - Fork 1
/
day12.jl
76 lines (70 loc) · 2.07 KB
/
day12.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module Day12
using AdventOfCode2020
function day12(input::String = readInput(joinpath(@__DIR__, "..", "data", "day12.txt")))
instructions = parse_input(input)
return [part1(instructions), part2(instructions)]
end
function parse_input(input::String)
sinput = split(input)
instructions = Array{Tuple{Char,Int},1}(undef, length(sinput))
for (i, line) in enumerate(sinput)
m = match(r"(N|S|E|W|L|R|F)(\d+)", line)
m == nothing && AssertionError("Invalid input")
m[1] in ('L', 'R') && m % 90 != 0 && AssertionError("only multiples of 90 degrees are supported")
instructions[i] = (m[1][1], parse(Int, m[2]))
end
return instructions
end
function part1(instructions::Array{Tuple{Char,Int},1})
direction = [1, 0]
position = [0, 0]
L = [0 -1; 1 0]
R = [0 1; -1 0]
for (com, val) in instructions
if com == 'N'
position[2] += val
elseif com == 'S'
position[2] -= val
elseif com == 'E'
position[1] += val
elseif com == 'W'
position[1] -= val
elseif com == 'L'
n = val ÷ 90
direction = L^n * direction
elseif com == 'R'
n = val ÷ 90
direction = R^n * direction
elseif com == 'F'
position += val * direction
end
end
return abs.(position) |> sum
end
function part2(instructions::Array{Tuple{Char,Int},1})
position = [0, 0]
waypoint = [10, 1]
L = [0 -1; 1 0]
R = [0 1; -1 0]
for (com, val) in instructions
if com == 'N'
waypoint[2] += val
elseif com == 'S'
waypoint[2] -= val
elseif com == 'E'
waypoint[1] += val
elseif com == 'W'
waypoint[1] -= val
elseif com == 'L'
n = val ÷ 90
waypoint = L^n * waypoint
elseif com == 'R'
n = val ÷ 90
waypoint = R^n * waypoint
elseif com == 'F'
position += val * waypoint
end
end
return abs.(position) |> sum
end
end # module