Skip to content

Instantly share code, notes, and snippets.

@maacl
Created December 8, 2022 22:05
Show Gist options
  • Save maacl/85b8c886b6ec7b269cea2a1664ab6a3d to your computer and use it in GitHub Desktop.
Save maacl/85b8c886b6ec7b269cea2a1664ab6a3d to your computer and use it in GitHub Desktop.
AoC 2022 Day 7
(ns aoc2022.day7
(:require
[clojure.string :as str]
[clojure.zip :as z]
[clojure.core.match :refer [match]]))
(defn process-line
[tree line]
(match line
["$" "cd" ".."]
(-> tree z/up)
["$" "cd" _]
(-> (z/insert-child tree []) z/down)
[(f-size :guard #(re-matches #"\d+" %)) _]
(z/insert-child tree (parse-long f-size))
:else
tree))
;; Part 1
(->>
(slurp "resources/input7.txt")
str/split-lines
(map #(str/split % #" "))
(reduce process-line (z/vector-zip []))
z/root
(tree-seq vector? identity)
(filter vector?)
(map #(apply + (flatten %)))
(filter #(<= % 100000))
(apply +))
;; Part 2
(defn find-dir
[[root & dirs]]
(let [unused-space (- 70000000 root)
required-space (- 30000000 unused-space)]
(->>
dirs
reverse
(drop-while #(< % required-space))
first)))
(->>
(slurp "resources/input7.txt")
str/split-lines
(map #(str/split % #" "))
(reduce process-line (z/vector-zip []))
z/root
(tree-seq vector? identity)
(filter vector?)
(map #(apply + (flatten %)))
(sort >)
find-dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment