-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.clj
53 lines (49 loc) · 1.66 KB
/
day7.clj
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
53
(ns clojure-solutions.day7
(:require [clojure.string :as str])
(:use [clojure-aoc-util.util] :reload))
(defn- parse []
(letfn [(dir-or-file [[type name]]
(if (= type "dir")
name
(read-string type)))]
(->> (slurp "../inputs/day7.txt")
(#(str/split % #"\$ "))
rest
(map (comp (partial map words) str/split-lines))
(map (fn [[cmd & inp]]
{:command [(keyword (first cmd)) (second cmd)]
:output (map dir-or-file inp)})))))
(defn- simulate [cmds]
(loop [pwd []
tree {}
[{output :output, [cmd dir] :command} & cs] cmds]
(if (nil? cmd)
tree
(case cmd
:cd (if (= dir "..")
(recur (pop pwd) tree cs)
(recur (conj pwd dir) tree cs))
:ls (recur pwd (assoc tree pwd output) cs)))))
(defn- solve []
(->> (simulate (parse))
(sort-by (comp count first) >)
(reduce (fn [hm [path vals]]
(assoc hm
path
(reduce (fn [acc el]
(+ acc (if (string? el)
(get hm (conj path el))
el)))
0
vals)))
{})
(into {})))
(defn day7 [p]
(let [hm (solve)]
(case p
:one (reduce (fn [acc [_ el]]
(if (<= el 100000) (+ acc el) acc))
0
hm)
:two (let [need (- 30000000 (- 70000000 (get hm ["/"])))]
(val (apply min-key val (filter-val #(<= need %) hm)))))))