-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.clj
49 lines (36 loc) · 1.49 KB
/
day16.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
(ns adventofcode2017.day16
(:require [clojure.string :as str]
[clojure.java.io :as io]))
(def programs (mapv char (range (int \a) (inc (int \p)))))
(defn spin [n coll]
(vec (concat (take-last n coll) (drop-last n coll))))
(defn exchange [x y coll]
(-> coll
(assoc x (coll y))
(assoc y (coll x))))
(defn partner [a b coll]
(exchange (.indexOf coll a) (.indexOf coll b) coll))
(def input (slurp (io/resource "2017/day16")))
(def input-fns (->> (str/split input #",")
(map #(cond
(re-matches #"^s\d+$" %)
(let [[_ n] (re-matches #"^s(\d+)$" %)]
(partial spin (Integer/parseInt n)))
(re-matches #"^x\d+/\d+$" %)
(let [[_ x y] (re-matches #"^x(\d+)/(\d+)$" %)]
(partial exchange (Integer/parseInt x) (Integer/parseInt y)))
:else
(let [[_ a b] (re-matches #"^p([a-p])/([a-p])$" %)]
(partial partner (first a) (first b)))))))
(def input-fn (apply comp (reverse input-fns)))
;; part 1
(apply str (input-fn programs))
; bijankplfgmeodhc
;; part 2
(def dance-seq (iterate input-fn programs))
(def period (->> (take-while #(not= (first dance-seq) %)
(rest dance-seq))
count
inc))
(apply str (nth dance-seq (mod 1000000000 period)))
; bpjahknliomefdgc