blob: 7bcb3ca880e569f617a331bf3069ebf53debc0cf (
plain) (
blame)
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
|
#lang racket
(define data (file->lines "day07"))
(define fs (make-hash))
(let loop ([data (cdr data)]
[cwd '()])
(let ([cmd (car data)]
[rest (cdr data)])
(cond [(string-prefix? cmd "$ cd")
(let ([dir (third (string-split cmd " "))])
(if (equal? dir "..")
(loop rest (cdr cwd))
(loop rest (cons dir cwd))))]
[(equal? cmd "$ ls")
(let dir-loop ([dir-list rest])
(if (null? dir-list)
(void) ; done
(let ([line (car dir-list)]
[dir-rest (cdr dir-list)])
(cond [(string-prefix? line "$")
(loop (cons line dir-rest) cwd)] ; push back line
[(string-prefix? line "dir")
(dir-loop dir-rest)] ; ignore
[else
(let* ([fields (string-split line " ")]
[size (string->number (first fields))])
(let segment-loop ([dir cwd])
(hash-update! fs
(string-join (reverse dir) "/" #:before-first "/")
(curry + size)
0)
(unless (null? dir)
(segment-loop (cdr dir))))
(dir-loop dir-rest))]))))]
[else (error "invalid line")])))
(for/sum ([(dir size) fs])
(if (<= size 100000)
size
0))
; 1306611
(let* ([total (hash-ref fs "/")]
[unused (- 70000000 total)]
[needed (- 30000000 unused)])
(for/fold ([smallest total])
([(dir size) fs])
(if (>= size needed)
(min smallest size)
smallest)))
; 13210366
|