-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.jl
67 lines (60 loc) · 1.38 KB
/
day1.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
workspace()
inputFile = open("/Users/maxj/Code/Projects/advent-of-code/2016/directions.csv")
in = readstring(inputFile)
close(inputFile)
directions = split(strip(in), ", ")
#directions = ["R5", "L5", "R5", "R3"]
#directions = ["R2", "R2", "R2"]
#directions = ["R2", "L3"]
#directions = transpose(Array(input))
type Location
x::Int
y::Int
cardinalDirection::Char
end
loc = Location(0,0,'N')
for d in directions
direction = d[1]
distance = parse(Int,d[2:end])
if (loc.cardinalDirection == 'N')
if (direction == 'R')
loc.x += distance
loc.cardinalDirection = 'E'
else
loc.x -= distance
loc.cardinalDirection = 'W'
end
continue
end
if (loc.cardinalDirection == 'E')
if (direction == 'R')
loc.y -= distance
loc.cardinalDirection = 'S'
else
loc.y += distance
loc.cardinalDirection = 'N'
end
continue
end
if (loc.cardinalDirection == 'S')
if (direction == 'R')
loc.x -= distance
loc.cardinalDirection = 'W'
else
loc.x += distance
loc.cardinalDirection = 'E'
end
continue
end
if (loc.cardinalDirection == 'W')
if (direction == 'R')
loc.y += distance
loc.cardinalDirection = 'N'
else
loc.y -= distance
loc.cardinalDirection = 'S'
end
continue
end
end
println("Distance: ", abs(loc.x) + abs(loc.y), " (", loc.x, ", ", loc.y, ")")