-
Notifications
You must be signed in to change notification settings - Fork 0
/
orbits.yuk
executable file
·157 lines (112 loc) · 2.63 KB
/
orbits.yuk
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/local/bin/awful
!fun :add-object $name
!global &obj
:writeln s'added new object ' $name
:set &obj[$name] :dict
:set &obj[$name][s'depth'] i0
:set &obj[$name][s'parent'] s''
:set &obj[$name][s'children'] :arr
!nuf
!fun :add-orbit $name $parent
!global &obj
:add-object $name
:set &obj[$name][s'parent'] $parent
!if :arr-contains (:dict-keys &obj) $parent
:set &idx :arr-count &obj[$parent][s'children']
!else
:add-object $parent
:set &idx i0
!fi
:set &obj[$parent][s'children'][$idx] $name
:writeln s'added orbit: ' $name s' orbits ' $parent
!nuf
!fun :resolve-orbits $parent
!global &obj
:set &i i0
!while :lt $i :arr-count &obj[&parent][s'children']
:set &child &obj[&parent][s'children'][&i]
:set &obj[&child][s'parent'] $parent
:set &obj[&child][s'depth'] :add i1 &obj[&parent][s'depth']
:writeln sxobject's "x $parent s'" child #' $i s' is ' $child s' (depth: ' &obj[&child][s'depth'] s')'
:resolve-orbits $child
:add &i i1
!done
!nuf
!fun :count-orbits
!global &obj
:set &sum i0
:set &keys :dict-keys &obj
:set &i i0
!while :lt $i :arr-count $keys
:add &sum &obj[&keys[&i]][s'depth']
:add &i i1
!done
!return &sum
!nuf
!fun :find-path_r $src $dst &visited
!global &obj
:set &visited[$src] i1
# :writeln s'checking path from <' $src s'>(' &visited[$src] s') to <' $dst s'>'
:set &parent &obj[$src][s'parent']
!if :neq $parent s''
!if :eq $parent $dst
:write $dst s' <- '
!return i1
!fi
!if :not &visited[$parent]
:set &parent-path :find-path_r $parent $dst &visited
!if :gt $parent-path i0
:write $parent s' <- '
!return :add i1 $parent-path
!fi
!fi
!fi
:set &i i0
!while :lt $i :arr-count &obj[&src][s'children']
:set &child &obj[&src][s'children'][&i]
!if :not &visited[$child]
!if :eq $child $dst
:write $child s' <- '
!return i1
!fi
:set &child-path :find-path_r $child $dst &visited
!if :gt $child-path i0
:write $child s' <- '
!return :add i1 $child-path
!fi
!fi
:add &i i1
!done
!return i-1
!nuf
!fun :find-path $src $dst
!global &obj
!if :eq $src $dst
!return i0
!fi
:set &visited :dict
:set &keys :dict-keys &obj
:set &i i0
!while :lt $i :arr-count $keys
:set &visited[&keys[&i]] i0
:add &i i1
!done
:set &path-len :find-path_r $src $dst &visited
:writeln $src
!return $path-len
!nuf
!fun :read-input
!global &obj
!while :not :stdin-eof
:set &line s''
:readln &line
:set &parts :str-explode $line s')'
:add-orbit $parts[i1] $parts[i0]
!done
!nuf
:set &obj :dict
:add-object s'COM'
:read-input
:resolve-orbits s'COM'
:writeln :count-orbits
:writeln :find-path s'YOU' s'SAN'