commit cd603c791ced3af0f30452a976a620aab0612b78
parent 846df490e7b4a48b2ab4b8cdc404f848c88329aa
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date: Fri, 6 Dec 2019 10:06:21 -0500
Add Julia solution for day 06
Diffstat:
A | day-06/day-06.jl | | | 66 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 66 insertions(+), 0 deletions(-)
diff --git a/day-06/day-06.jl b/day-06/day-06.jl
@@ -0,0 +1,66 @@
+function part_1(input)
+ orbit_map = Dict(map(x -> (x[2], x[1]), input))
+ orbit_count = Dict()
+ for planet in keys(orbit_map)
+ center = orbit_map[planet]
+ path = []
+ root_count = 0
+ pushfirst!(path, planet)
+ while true
+ pushfirst!(path, center)
+ if center in keys(orbit_count)
+ root_count = orbit_count[center]
+ break
+ else
+ if center in keys(orbit_map)
+ center = orbit_map[center]
+ else
+ break
+ end
+ end
+ end
+ for root in path
+ orbit_count[root] = root_count
+ root_count += 1
+ end
+ end
+ sum(values(orbit_count))
+end
+
+function part_2(input)
+ orbit_map = Dict(map(x -> (x[2], x[1]), input))
+ # Build SAN path.
+ santa_path = Dict()
+ begin
+ center = orbit_map["SAN"]
+ orbit_count = 0
+ while true
+ santa_path[center] = orbit_count
+ if center in keys(orbit_map)
+ center = orbit_map[center]
+ orbit_count += 1
+ else
+ break
+ end
+ end
+ end
+ # Trace back YOU path until we get a hit.
+ begin
+ center = orbit_map["YOU"]
+ orbit_count = 0
+ while true
+ if center in keys(santa_path)
+ return orbit_count + santa_path[center]
+ else
+ center = orbit_map[center]
+ orbit_count += 1
+ end
+ end
+ end
+end
+
+input = map(x -> split(x, ")"), readlines(open("input.txt")))
+
+println("Julia:")
+println("Part 1: ", part_1(input))
+println("Part 2: ", part_2(input))