day-06.jl (1728B)
1 function part_1(input)
2 orbit_map = Dict(map(x -> (x[2], x[1]), input))
3 orbit_count = Dict()
4 for planet in keys(orbit_map)
5 center = orbit_map[planet]
6 path = []
7 root_count = 0
8 pushfirst!(path, planet)
9 while true
10 pushfirst!(path, center)
11 if center in keys(orbit_count)
12 root_count = orbit_count[center]
13 break
14 else
15 if center in keys(orbit_map)
16 center = orbit_map[center]
17 else
18 break
19 end
20 end
21 end
22 for root in path
23 orbit_count[root] = root_count
24 root_count += 1
25 end
26 end
27 sum(values(orbit_count))
28 end
29
30 function part_2(input)
31 orbit_map = Dict(map(x -> (x[2], x[1]), input))
32 # Build SAN path.
33 santa_path = Dict()
34 begin
35 center = orbit_map["SAN"]
36 orbit_count = 0
37 while true
38 santa_path[center] = orbit_count
39 if center in keys(orbit_map)
40 center = orbit_map[center]
41 orbit_count += 1
42 else
43 break
44 end
45 end
46 end
47 # Trace back YOU path until we get a hit.
48 begin
49 center = orbit_map["YOU"]
50 orbit_count = 0
51 while true
52 if center in keys(santa_path)
53 return orbit_count + santa_path[center]
54 else
55 center = orbit_map[center]
56 orbit_count += 1
57 end
58 end
59 end
60 end
61
62 input = map(x -> split(x, ")"), readlines(open("input.txt")))
63
64 println("Julia:")
65 println("Part 1: ", part_1(input))
66 println("Part 2: ", part_2(input))