-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.jl
107 lines (97 loc) · 3.52 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import Base.*
function parse_line(line)
(instruction=first(line), argument=parse(Int, line[2:end]))
end
function interpret_part1(ship_pos, instruction, argument, ship_bearing)
if instruction == 'F'
new_ship_pos = (
x = ship_pos.x + (argument * sind(ship_bearing)),
y = ship_pos.y + (argument * cosd(ship_bearing)),
)
return (new_ship_pos, ship_bearing)
elseif instruction == 'R'
new_ship_bearing = mod(ship_bearing + argument, 360)
return (ship_pos, new_ship_bearing)
elseif instruction == 'L'
new_ship_bearing = mod(ship_bearing - argument, 360)
return (ship_pos, new_ship_bearing)
elseif instruction == 'N'
new_ship_pos = (x = ship_pos.x, y = ship_pos.y + argument)
return (new_ship_pos, ship_bearing)
elseif instruction == 'S'
new_ship_pos = (x = ship_pos.x, y = ship_pos.y - argument)
return (new_ship_pos, ship_bearing)
elseif instruction == 'E'
new_ship_pos = (x = ship_pos.x + argument, y = ship_pos.y)
return (new_ship_pos, ship_bearing)
elseif instruction == 'W'
new_ship_pos = (x = ship_pos.x - argument, y = ship_pos.y)
return (new_ship_pos, ship_bearing)
else
error("Could not interpret instruction $instruction with arg $argument")
end
end
function part1(code)
ship_pos = (x=0.0, y=0.0)
ship_bearing = 90.0
for c in code
(ship_pos, ship_bearing) = interpret_part1(
ship_pos, c.instruction, c.argument, ship_bearing
)
end
abs(ship_pos.x) + abs(ship_pos.y)
end
function *(M::AbstractArray, p::NamedTuple{(:x, :y)})
p2 = M * [p.x; p.y]
(x=p2[1], y=p2[2])
end
R(θ) = [cosd(θ) -sind(θ); sind(θ) cosd(θ)]
function interpret_part2(ship_pos, waypoint_pos, instruction, argument)
if instruction == 'F'
new_ship_pos = (
x = ship_pos.x + argument*waypoint_pos.x,
y = ship_pos.y + argument*waypoint_pos.y,
)
return (new_ship_pos, waypoint_pos)
elseif instruction == 'R'
new_waypoint_pos = R(-argument) * waypoint_pos
return (ship_pos, new_waypoint_pos)
elseif instruction == 'L'
new_waypoint_pos = R(argument) * waypoint_pos
return (ship_pos, new_waypoint_pos)
elseif instruction == 'N'
new_waypoint_pos = (x = waypoint_pos.x, y = waypoint_pos.y + argument)
return (ship_pos, new_waypoint_pos)
elseif instruction == 'S'
new_waypoint_pos = (x = waypoint_pos.x, y = waypoint_pos.y - argument)
return (ship_pos, new_waypoint_pos)
elseif instruction == 'E'
new_waypoint_pos = (x = waypoint_pos.x + argument, y = waypoint_pos.y)
return (ship_pos, new_waypoint_pos)
elseif instruction == 'W'
new_waypoint_pos = (x = waypoint_pos.x - argument, y = waypoint_pos.y)
return (ship_pos, new_waypoint_pos)
else
error("Could not interpret instruction $instruction with arg $argument")
end
end
function part2(code)
ship_pos = (x=0.0, y=0.0)
waypoint_pos = (x=10.0, y=1.0)
for c in code
(ship_pos, waypoint_pos) = interpret_part2(
ship_pos, waypoint_pos, c.instruction, c.argument
)
end
abs(ship_pos.x) + abs(ship_pos.y)
end
function main()
filename = "inputs/day12.txt"
code = filename |> readlines .|> parse_line
part1_solution = part1(code)
part2_solution = part2(code)
(part1_solution, part2_solution)
end
@time (part1_solution, part2_solution) = main()
@show part1_solution
@show part2_solution