From 8d43e37df99f280377bed90284d6ac2428334804 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Wed, 30 Nov 2022 19:55:31 -0500 Subject: move 2021 days to folder; update README --- year2021/day10/part1.clj | 22 ++++++++++++++++++++++ year2021/day10/part2.clj | 24 ++++++++++++++++++++++++ year2021/day10/partboth.clj | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 year2021/day10/part1.clj create mode 100644 year2021/day10/part2.clj create mode 100644 year2021/day10/partboth.clj (limited to 'year2021/day10') diff --git a/year2021/day10/part1.clj b/year2021/day10/part1.clj new file mode 100644 index 0000000..60ed1e6 --- /dev/null +++ b/year2021/day10/part1.clj @@ -0,0 +1,22 @@ +(def to-closing {\{ \} \( \) \[ \] \< \>}) +(def to-score {\) 3 \] 57 \} 1197 \> 25137}) + +(defn check-line [input] + (loop [in input open '()] + (when-let [c (first in)] + (if (some->> (#{\} \) \] \>} c) (not= (first open))) + c + (recur + (rest in) + (if-let [op (to-closing c)] + (conj open op) + (rest open) + )))))) + +(->> (slurp "./in") + (clojure.string/split-lines) + (map (comp to-score check-line vec)) + (filter some?) + (apply +) + (println)) + diff --git a/year2021/day10/part2.clj b/year2021/day10/part2.clj new file mode 100644 index 0000000..4dfaf3b --- /dev/null +++ b/year2021/day10/part2.clj @@ -0,0 +1,24 @@ +(def to-closing {\{ \} \( \) \[ \] \< \>}) +(def to-score {\) 1 \] 2 \} 3 \> 4}) + +(defn check-line [input] + (loop [in input open '()] + (if-let [c (first in)] + (when (or (nil? (#{\} \) \] \>} c)) (= (first open) c)) + (recur + (rest in) + (if-let [op (to-closing c)] + (conj open op) + (rest open)))) + open + ))) + +(->> (slurp "./in") + (clojure.string/split-lines) + (map (comp check-line vec)) + (filter some?) + (map (partial reduce #(+ (* 5 %1) (to-score %2)) 0)) + (sort) + (#(nth % (quot (count %) 2))) + (println)) + diff --git a/year2021/day10/partboth.clj b/year2021/day10/partboth.clj new file mode 100644 index 0000000..78080d9 --- /dev/null +++ b/year2021/day10/partboth.clj @@ -0,0 +1,34 @@ +(def to-closing {\{ \} \( \) \[ \] \< \>}) +(def to-score {\) 3 \] 57 \} 1197 \> 25137}) +(def to-score2 {\) 1 \] 2 \} 3 \> 4}) + +(defn check-line [input] + (loop [in input open '()] + (if-let [c (first in)] + (if (some->> (#{\} \) \] \>} c) (not= (first open))) + c + (recur + (rest in) + (if-let [op (to-closing c)] + (conj open op) + (rest open) + ))) + open + ))) + +(->> (slurp "./in") + (clojure.string/split-lines) + (map (comp check-line vec)) + ; check-line returns first invalid character, or list of characters + ; necessary to close the line. Work through these through `reduce` + ; and build the answers for both parts: + (reduce + (fn [tots nxt] + (if (seq? nxt) + (update tots 1 #(conj % (reduce (fn [a b] (+ (* 5 a) (to-score2 b))) 0 nxt))) + (update tots 0 #(+ % (to-score nxt))))) + [0 '()]) + ; Get part 2 answer from the list of scores: + (#(update % 1 (fn [ns] (nth ns (quot (count ns) 2))))) + (println)) + -- cgit v1.2.3