/.clj
Created
December 8, 2022 22:05
AoC 2022 Day 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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