-
Notifications
You must be signed in to change notification settings - Fork 0
/
day03.clj
62 lines (48 loc) · 1.47 KB
/
day03.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
50
51
52
53
54
55
56
57
58
59
60
61
62
(ns advent.day03
(:require [advent.core :as c]
[clojure.pprint :as pp]
[clojure.string :as str]))
(defn read-input [file]
(->> file
(c/read-lines)
(map seq)
(map cycle)))
(def right 3)
(def down 1)
(defn step [{:keys [col row] :as pos}]
(let [next {:col (+ col right)
:row (+ row down)}]
next))
(defn get [g {:keys [col row] :as pos}]
(nth (nth g row) col))
(defn part1 [file]
(let [grid (read-input file)
rows (dec (count grid))]
(loop [pos {:col 0 :row 0}
path []]
(let [row (:row pos)]
(if (> row rows)
(count (filter #(= \# %) path))
(recur (step pos) (conj path (get grid pos))))))))
;; Part2
;; Run for a number of slopes and multiple all the results
(def slopes [{:right 1 :down 1}
{:right 3 :down 1}
{:right 5 :down 1}
{:right 7 :down 1}
{:right 1 :down 2}])
(defn step2 [{:keys [right down] :as slope} {:keys [col row] :as pos}]
(let [next {:col (+ col (:right slope))
:row (+ row (:down slope))}]
next))
(defn traverse [grid slope]
(let [rows (dec (count grid))]
(loop [pos {:col 0 :row 0}
path []]
(let [row (:row pos)]
(if (> row rows)
(count (filter #(= \# %) path))
(recur (step2 slope pos) (conj path (get grid pos))))))))
(defn part2 [file]
(let [grid (read-input file)]
(apply * (map #(traverse grid %) slopes))))