day13: make more concise

master
Clyne 3 years ago
parent 55387461c6
commit c54de0ebc6

@ -2,40 +2,29 @@
(def input (->> (slurp "./in") (def input (->> (slurp "./in")
(str/split-lines) (str/split-lines)
(split-with not-empty))) (split-with not-empty)
(def points (->> (first input) ((juxt
(map #(str/split % #",")) (fn [lst]
(map (partial map read-string)) (reduce
(set))) #(conj %1 (mapv read-string (str/split %2 #",")))
(def folds (->> (rest (second input)) #{} (first lst)))
(map #(str/split % #"=")) (fn [lst]
(map #(do [(last (first %)) (read-string (second %))])))) (->> (second lst)
(drop 1)
(map #(-> %
(str/split #"=")
(update 0 (comp {\x 0 \y 1} last))
(update 1 read-string)))
(first)
))))))
(loop [fds (take 1 folds) pts points] (defn fold-point [idx chg pt]
(if (empty? fds) (cond-> pt (> (get pt idx) chg) (update idx #(- % (* 2 (- % chg))))))
(println (count pts))
(recur (let [instruction (second input)]
(rest fds) (->> (first input)
(let [ins (first fds) chg (second ins)] (map (partial fold-point (first instruction) (second instruction)))
(if (= \x (first ins)) (distinct)
(reduce (count)
#(conj %1 (println)))
(if (< (first %2) chg)
%2
(seq [(- (first %2) (* 2 (- (first %2) chg))) (second %2)])))
#{}
pts)
(reduce
#(conj %1
(if (< (second %2) chg)
%2
(seq [(first %2) (- (second %2) (* 2 (- (second %2) chg)))])
))
#{}
pts)
)
)
)
)
)

@ -2,44 +2,32 @@
(def input (->> (slurp "./in") (def input (->> (slurp "./in")
(str/split-lines) (str/split-lines)
(split-with not-empty))) (split-with not-empty)
(def points (->> (first input) ((juxt
(map #(str/split % #",")) (fn [lst]
(map (partial map read-string)) (reduce
(set))) #(conj %1 (mapv read-string (str/split %2 #",")))
(def folds (->> (rest (second input)) #{} (first lst)))
(map #(str/split % #"=")) (fn [lst]
(map #(do [(last (first %)) (read-string (second %))])))) (->> (rest (second lst))
(map #(-> %
(str/split #"=")
(update 0 (comp {\x 0 \y 1} last))
(update 1 read-string)))
))))))
(defn print-grid [pts] (defn print-grid [pts]
(doseq [p pts] (println (str/join [(first p) "," (second p)]))) (doseq [p pts] (println (str/join [(first p) "," (second p)]))))
)
(loop [fds folds pts points] (defn fold-point [idx chg pt]
(if (empty? fds) (cond-> pt (> (get pt idx) chg) (update idx #(- % (* 2 (- % chg))))))
(print-grid pts)
(recur (print-grid
(rest fds) (apply
(let [ins (first fds) chg (second ins)] (partial
(if (= \x (first ins)) reduce
(reduce #(let [ins (first %2)]
#(conj %1 (set (map (partial fold-point (first %2) (second %2)) %1))))
(if (< (first %2) chg) input
%2 ))
(seq [(- (first %2) (* 2 (- (first %2) chg))) (second %2)])))
#{}
pts)
(reduce
#(conj %1
(if (< (second %2) chg)
%2
(seq [(first %2) (- (second %2) (* 2 (- (second %2) chg)))])
))
#{}
pts)
)
)
)
)
)

Loading…
Cancel
Save