blob: a6c99ae050f92c12ae6012cd443b3ed56ef6634e (
plain)
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
|
(ql:quickload '(:fiveam :cl-ppcre))
(defun parse-to-tree (stream)
(labels ((scan ()
(loop for item = (read-line stream nil nil)
while (and item (not (equal "$ cd .." item)))
when (parse item) collect it))
(parse (item)
(cond
((ppcre:register-groups-bind (dir) ("\\$ cd (\\w+|/)" item) (cons dir (scan))))
((ppcre:register-groups-bind (size name) ("^(\\d+) (\\w+)" item)
(cons name (parse-integer size)))))))
(car (scan))))
(defun aggregate-size (tree)
(labels ((combiner (tri)
(list (car tri)
(adder tri)
(reduce (lambda (acc item)
(if (numberp (cdr item)) ;; skips files
acc
(cons (combiner item) acc)))
(cdr tri) :initial-value nil)))
(adder (tri)
(reduce (lambda (acc item)
(+ acc (if (numberp (cdr item))
(cdr item)
(adder item))))
(cdr tri) :initial-value 0)))
(combiner tree)))
(defun flat-dir-size ()
(with-open-file (in "input")
(delete nil
(flatten-tree
(aggregate-size
(parse-to-tree in)))
:test #'eq) ))
(defun flatten-tree (tree)
(cond
((null tree) nil)
((consp (car tree)) (nconc (flatten-tree (car tree))
(flatten-tree (cdr tree))))
(t (cons (car tree) (flatten-tree (cdr tree))))))
(defun addr-small-dirs (dir-sizes small-limit)
(loop for (_name size) on dir-sizes by #'cddr
when (< size small-limit)
sum size))
(defun dir-to-del (dir-sizes total-disk needed-disk)
(let* ((free-disk (- total-disk (cadr dir-sizes)))
(to-free (- needed-disk free-disk)))
(loop for (_ size) on dir-sizes by #'cddr
when (> size to-free)
minimizing size)))
(fiveam:test results
(fiveam:is (= 1845346
(addr-small-dirs (flat-dir-size) 100000)))
(fiveam:is (= 3636703
(dir-to-del (flat-dir-size) 70000000 30000000))))
|