-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.clj
33 lines (30 loc) · 1.32 KB
/
day13.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
(ns clojure-solutions.day13
(:require [clojure.string :as str])
(:use [clojure-solutions.util] :reload))
(defn- parse []
(letfn [(parse-fold [fold] ; "blahblah x=123" or "blahblah x=123"
(let [[xy _eq & n] (drop-while #(and (not= % \x) (not= % \y)) fold)]
[xy (coll-to-base 10 n)]))]
(->> (slurp "./input/day13.txt")
split-groups ; split points from folds
(map str/split-lines) ; split points and folds into singletons
((fn [[points folds]]
{:folds (map parse-fold folds)
:dots (set (map #(read-string (str/join ["[" % "]"])) ; read as vector
points))})))))
(defn- solve
"Apply all folds to a set of points."
[folds dots]
(letfn [(do-fold [[xy a] [x y]]
(case xy ; fold either along x or y axis
\x (if (> x a) [(- a (- x a)) y ] [x y])
\y (if (> y a) [x (- a (- y a))] [x y])))]
(reduce (fn [acc f]
(reduce #(conj %1 (do-fold f %2)) #{} acc))
dots
folds)))
(defn day13 []
(let [{:keys [folds dots]} (parse)]
(println (count (solve (take 1 folds) dots))) ; => 607
(doseq [line (plot-set (solve folds dots))] ; => CPZLPFZL
(println line))))