-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.jl
90 lines (57 loc) · 1.88 KB
/
day8.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
using DelimitedFiles
cd("/Users/patricklauer/Documents/GitHub/advent_of_code_2023/")
lines = readdlm("day8_input.txt")
## seperate directions
directions = lines[1]
directions = collect(directions)
## seperate and clean map information
map = lines[2:end,:]
map = map[:, [1,3,4]]
function remove_symbols(matrix)
return replace(matrix, r"[\(,\)]" => "")
end
map[:,2] = remove_symbols.(map[:,2])
map[:,3] = remove_symbols.(map[:,3])
map
## first column = location
## second column = location if left step
## third column = location if right step
## while loop through matrix till it reaches ZZZ and keep track of steps
directions = repeat(directions, 100)
location = "AAA"
steps = 0
while location != "ZZZ"
i = findfirst( x -> x == 1, map[:,1] .== location) ## find the row in which the correct location is in the matrix
if directions[steps+1] == 'L'
location = map[i,2]
steps += 1
elseif directions[steps+1] == 'R'
location = map[i,3]
steps += 1
end
end
## Part B
## loop through all elements that end with A and track the steps they need to reach ZZZ
## find least common multiplicate of the steps
count_steps = function(map, direction, location)
steps = 0
while endswith.(location, "Z") != true
i = findall( x -> x == 1, map[:,1] .== location) ## find the row in which the correct location is in the matrix
if directions[steps+1] == 'L'
location = map[i,2][1]
elseif directions[steps+1] == 'R'
location = map[i,3][1]
end
steps += 1
end
return steps
end
directions = repeat(directions, 100000)
locations = String.([s for s in map[:,1] if endswith(s, "A")])
all_steps = []
for i in 1:length(locations)
push!(all_steps, count_steps(map, directions, locations[i]))
end
all_steps
## find least common multiplicate
reduce(lcm, all_steps)