Skip to content

Commit 0951937

Browse files
committed
2017 6
Officially caught up to my previous 2017 attempt in Scala :D
1 parent 6ee68c7 commit 0951937

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/advent_of_code/2017/05.clj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
(ns advent-of-code.2017.05
2-
(:require [advent-of-code.utils :as u]
3-
[advent-of-code.points :as p]))
2+
(:require [advent-of-code.utils :as u]))
43

54
(def path (u/get-input 2017 5))
6-
(def tpath (u/test-path 2017 5))
75

86
(defn run [path part2?]
97
(let [input (u/read-file-list path parse-long)

src/advent_of_code/2017/06.clj

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
(ns advent-of-code.2017.06
2+
(:require [advent-of-code.utils :as u]))
3+
4+
(def path (u/get-input 2017 6))
5+
(def tpath (u/test-path 2017 6))
6+
7+
(defn index-max [vector]
8+
(second (reduce-kv (fn [[m :as acc] idx v] (if (> v m) [v idx] acc)) [Integer/MIN_VALUE -1] vector)))
9+
10+
(defn run [path part1?]
11+
(let [input (u/read-file-line path u/nums)
12+
n (count input)]
13+
(loop [curr input
14+
seen (transient #{})
15+
cycles 0
16+
on? false]
17+
(cond (and (or on? part1?) (seen curr)) cycles
18+
(seen curr) (recur curr (transient #{}) 0 true)
19+
:else (let [idx (index-max curr)
20+
v (curr idx)
21+
global (quot v n)
22+
rem (mod v n)
23+
new-c (->> (assoc curr idx 0)
24+
(mapv #(+ global %)))]
25+
(recur (reduce (fn [acc i] (update acc (mod (+ idx i 1) n) inc)) new-c (range rem))
26+
(conj! seen curr)
27+
(inc cycles)
28+
on?))))))
29+
30+
(comment
31+
32+
(run tpath true)
33+
(run path true)
34+
(run tpath false)
35+
(run path false)
36+
37+
;
38+
)

0 commit comments

Comments
 (0)