-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.clj
39 lines (34 loc) · 1.22 KB
/
day9.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
(ns clojure-solutions.day9
(:require [clojure.string :as str])
(:use [clojure-aoc-util.util] :reload))
(defn- parse []
(->> (slurp "../inputs/day9.txt")
str/split-lines
(mapcat (fn [move]
(let [[mv amount] (words move)]
;; Expand moves to individual instructions.
(repeat (read-string amount) (keyword mv)))))))
(defn- move [[z w] dir]
(case dir
:L [(- z 1) w]
:R [(+ z 1) w]
:U [z (+ w 1)]
:D [z (- w 1)]))
(defn- follow [[tx ty] [hx hy]]
(let [dx (- hx tx), dy (- hy ty)
ax (abs dx), ay (abs dy)]
(cond
(and (<= ax 1) (<= ay 1)) [tx ty] ; directly adjacent
(> ax ay) [(- hx (signum dx)), hy] ; horizontally estranged
(> ay ax) [hx, (- hy (signum dy))] ; vertically estranged
:else [(- hx (signum dx)), ; diagonally estranged
(- hy (signum dy))])))
(defn- simulate [ms]
(iterate #(reductions follow [0 0] %) (reductions move [0 0] ms)))
(defn day9 [p]
(count
(into #{}
(nth (simulate (parse))
(case p
:one 1
:two 9)))))