-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.clj
31 lines (28 loc) · 930 Bytes
/
day2.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
(ns clojure-solutions.day2
(:require [clojure.string :as str]))
(defn- parse []
(->> (slurp "./input/day2.txt")
str/split-lines
(map (fn [s]
(let [[kw n] (str/split s #" ")
n (read-string n) ; something like ViewPatterns would be neat
kw (keyword kw)]
(if (= :up kw) [:down (- n)] [kw n]))))))
;; 1524750
(defn one []
(->> (parse)
(reduce (fn [acc [k v]]
(update acc k #(+ v %)))
{:forward 0 :down 0})
((fn [{f :forward d :down}]
(* f d)))))
;; 1592426537
(defn two []
(->> (parse)
(reduce (fn [[aim depth horiz] [d i]]
(case d
:down [(+ aim i) depth horiz]
:forward [aim (+ depth (* aim i)) (+ horiz i)]))
[0 0 0])
rest ; only depth and horiz
(reduce *)))