-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.clj
56 lines (48 loc) · 1.36 KB
/
day5.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
(ns adventofcode2017.day5
(:require [clojure.java.io :as io]
[clojure.string :as str]
[criterium.core :refer [quick-bench]]
[primitive-math]))
(def input (slurp (io/resource "2017/day5")))
(def data (->> (str/split-lines input)
(mapv #(Integer/parseInt %))))
;; part 1
(defn jump [[maze pos]]
(when-some [offset (get maze pos)]
[(update maze pos inc) (+ pos offset)]))
(->> (iterate jump [data 0])
(take-while some?)
count
dec)
; 358309
;; part 2
(defn jump2 [[maze pos]]
(when-some [offset (get maze pos)]
[(update maze pos #(if (>= % 3) (dec %) (inc %))) (+ pos offset)]))
(->> (iterate jump2 [data 0])
(take-while some?)
count
dec)
; 28178177
;; perf
(set! *warn-on-reflection* true)
(primitive-math/use-primitive-operators)
(quick-bench
(let [maze (->> (io/resource "2017/day5")
slurp
str/split-lines
(map #(Integer/parseInt %))
int-array)
len (alength maze)]
(loop [maze maze
pos 0
steps 0]
(if (< -1 pos len)
(let [offset (aget maze pos)]
(recur (doto maze
(aset pos (if (>= offset 3) (dec offset) (inc offset))))
(+ pos offset)
(inc steps)))
steps)))
)
; Execution time mean : 106.287963 ms