-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.jl
71 lines (62 loc) · 1.13 KB
/
6.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
using DelimitedFiles
mutable struct Object{T}
child::T
Object{T}() where T = new{T}()
end
function parse_input(def)
d = Dict{String, Object}()
for r in def
A, B = split(r,')')
_A = get!(d,A,Object{Object}())
_B = get!(d,B,Object{Object}())
_B.child = _A
end
return d
end
v = ["COM)B",
"B)C",
"C)D",
"D)E",
"E)F",
"B)G",
"G)H",
"D)I",
"E)J",
"J)K",
"K)L",
"K)YOU",
"I)SAN"]
function count_til_last(o::Object,i=0)
try
count_til_last(o.child,i+1)
catch e
return i
end
end
t = parse_input(v)
sum(count_til_last.(values(t)))
f = readdlm("6.input")
t = parse_input(f)
sum(count_til_last.(values(t)))
# # Part 2
function all_orbits(o::Object,found=Object{Object}[])
try
push!(found,o)
all_orbits(o.child,found)
catch e
return found
end
end
function count_distance(o1,o2)
# find common ancestor
for (i,oi) in enumerate(o1)
for (j,oj) in enumerate(o2)
if oi === oj
return i+j-4
end
end
end
end
YOU = all_orbits(t["YOU"])
SAN = all_orbits(t["SAN"])
count_distance(YOU, SAN)