-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd7.hoon
69 lines (69 loc) · 1.9 KB
/
d7.hoon
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
|= input=(list @t)
=<
=| cd=tape
=| dirs=(map tape @ud)
=| folders=(set tape)
=. dirs (~(put by dirs) "/" 0)
=. folders (~(put in folders) "/")
|-
?~ input (get-sum dirs folders)
=/ line=tape (trip i.input)
?: =('$' +2:line)
?~ (find "$ cd " line)
$(input t.input)
$(cd (parse-dir cd (slag 5 line)), input t.input)
=/ filename=tape (slag +((need (find " " line))) line)
=| filesize=@ud
=? filesize ?!(=("dir" (scag 3 line))) (convert-decimal (crip (scag (need (find " " line)) line)))
=? folders =("dir" (scag 3 line)) (~(put in folders) (weld cd filename))
?: (~(has by dirs) filename) $(input t.input)
=/ temp=(map tape @ud) dirs
=. temp (update-folder-sizes temp cd filesize)
$(dirs (~(put by temp) (weld cd filename) filesize), input t.input)
::
|%
++ get-sum
|= [dirs=(map tape @ud) folders=(set tape)]
^- @ud
=| sum=@ud
=/ folders-list=(list tape) ~(tap in folders)
|-
?~ folders-list sum
?: (lte (~(got by dirs) i.folders-list) 100.000) $(sum (add sum (~(got by dirs) i.folders-list)), folders-list t.folders-list)
$(folders-list t.folders-list)
::
++ update-folder-sizes
|= [dirs=(map tape @ud) cd=tape size=@ud]
^- (map tape @ud)
|-
?: =(cd ~) dirs
?: =("/" cd)
=/ folder-size=@ud (~(got by dirs) cd)
(~(put by dirs) cd (add folder-size size))
=. cd (snip cd)
=/ folder-size=@ud (~(got by dirs) cd)
=/ next-dir=tape (flop (slag (need (find "/" (flop cd))) (flop cd)))
$(dirs (~(put by dirs) cd (add folder-size size)), cd next-dir)
::
++ convert-decimal
|= input=@t
^- @ud
=/ num=tape (trip input)
=. num (flop num)
=| count=@ud
=| output=tape
|-
?~ num (slav %ud (crip output))
?: (gth count 2)
$(output (weld "." output), count 0)
$(output [i.num output], num t.num, count +(count))
::
++ parse-dir
|= [cd=tape dir=tape]
^- tape
?. =(".." dir)
?: =("" cd) "/"
(weld cd (weld dir "/"))
=. cd +3:(flop cd)
(flop (slag (need (find "/" cd)) cd))
--