aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-07 14:35:06 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-07 14:35:06 -0500
commit0e12a59e64a6f95c2448cc5ddec96984d6111613 (patch)
tree513ba7ec1400c5a2985a2ec1cbbf866176115b2d
parent1a9df79bcd45b24476465cca61d8d57bbb94a89a (diff)
parent2c2ad6fcd6889340670ced5637c024590a064173 (diff)
downloadgleam_aoc-0e12a59e64a6f95c2448cc5ddec96984d6111613.tar.gz
gleam_aoc-0e12a59e64a6f95c2448cc5ddec96984d6111613.zip
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode
-rw-r--r--2022/day-07/day-07.rkt36
1 files changed, 36 insertions, 0 deletions
diff --git a/2022/day-07/day-07.rkt b/2022/day-07/day-07.rkt
new file mode 100644
index 0000000..4ebd3b4
--- /dev/null
+++ b/2022/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 cmd
+ ["$ ls" (values folders current-path (hash-has-key? folders current-path))]
+ ["$ cd /" (values folders '("/") #false)]
+ ["$ cd .." (values folders (rest current-path) #false)]
+ [(regexp #px"\\$ cd (.+)" (list _ folder)) (values folders (cons folder current-path) #false)]
+ [(regexp #px"dir (.+)") (values folders current-path previously-seen?)]
+ [(regexp #px"(.+) (.+)" (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))