-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.clj
47 lines (42 loc) · 1.42 KB
/
day5.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
(ns clojure-solutions.day5
(:require [clojure.string :as str])
(:use [clojure-aoc-util.util] :reload))
(defn- parse-crates [crates]
(->> crates
str/split-lines
(drop-last 1)
(map #(re-seq #"(?:\s{3}|\[\w\])(?:\s?)" %))
transpose
(mapv (partial keep #(when-not (str/blank? %) %)))))
(defn- parse-moves [moves]
(->> moves
str/split-lines
(map #(re-find #"move (\d+) from (\d+) to (\d+)" %))
(map (fn [[_ move from to]]
{:move (read-string move)
:from (dec (read-string from))
:to (dec (read-string to))}))))
(defn- parse []
(->> (slurp "../inputs/day5.txt")
split-groups
((fn [[crates moves]]
{:crates (parse-crates crates)
:moves (parse-moves moves)}))))
(defn- do-move [tamper crates {:keys [move from to]}]
(let [row-from (nth crates from)
row-to (nth crates to)]
(assoc crates
from (drop move row-from)
to (concat (tamper (take move row-from))
row-to))))
(defn day5 [part]
(let [{:keys [crates moves]} (parse)]
(letfn [(solve [tamper]
(->> (reduce (partial do-move tamper) crates moves)
(map first)
str/join))]
(str/replace (case part
:one (solve reverse)
:two (solve identity))
#" |\[|\]"
""))))