diff options
author | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
commit | 8777ff071f7bb37631baa7b6717ad29961e50911 (patch) | |
tree | 6d59c4ed58e454b960339c3d1151f0a879e8d7cb /racket/aoc2022/day-07 | |
parent | 6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff) | |
download | gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip |
sorting by language
Diffstat (limited to 'racket/aoc2022/day-07')
-rw-r--r-- | racket/aoc2022/day-07/day-07.rkt | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/racket/aoc2022/day-07/day-07.rkt b/racket/aoc2022/day-07/day-07.rkt new file mode 100644 index 0000000..3826cc4 --- /dev/null +++ b/racket/aoc2022/day-07/day-07.rkt @@ -0,0 +1,36 @@ +#lang racket + +(require advent-of-code + fancy-app + threading + (only-in relation ->number)) + +(define command-output (~> (fetch-aoc-input (find-session) 2022 7) (string-split "\n"))) + +(define (parse-commands cmds) + (define (update-sizes h path size) + (match path + ['() h] + [(list* _ fs) (update-sizes (hash-update h path (+ _ size) 0) fs size)])) + + (for/fold ([folders (hash)] [current-path '()] [previously-seen? #false] #:result folders) + ([cmd (in-list cmds)]) + (match (string-split cmd) + [(list "$" "ls") (values folders current-path (hash-has-key? folders current-path))] + [(list "$" "cd" "/") (values folders '("/") #false)] + [(list "$" "cd" "..") (values folders (rest current-path) #false)] + [(list "$" "cd" folder) (values folders (cons folder current-path) #false)] + [(list "dir" _) (values folders current-path previously-seen?)] + [(list (app ->number size) _) + (cond + [previously-seen? (values folders current-path previously-seen?)] + [else (values (update-sizes folders current-path size) current-path previously-seen?)])]))) + +(define folders (parse-commands command-output)) + +;; part 1 +(for/sum ([(_ v) (in-hash folders)] #:when (<= v 100000)) v) + +;; part 2 +(define required-to-delete (- 30000000 (- 70000000 (hash-ref folders '("/"))))) +(~>> folders hash-values (filter (> _ required-to-delete)) (apply min)) |