-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.clj
52 lines (40 loc) · 1.18 KB
/
day15.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
(ns day15
(:require aoc
[flatland.ordered.map :refer [ordered-map]]
[clojure.string :as str]))
(defn word-hash [word]
(reduce
(fn [acc c]
(-> acc
(+ (int c))
(* 17)
(mod 256)))
0
word))
(defn parse-instruction [instr]
(if (str/ends-with? instr "-")
(let [name (drop-last instr)]
{:name name
:pos (word-hash name)
:func dissoc})
(let [name (drop-last 2 instr)]
{:name name
:pos (word-hash name)
:func assoc
:focal (parse-long (str (last instr)))})))
(defn hashmap [instructions]
(reduce
(fn [boxes {:keys [name pos func focal]}]
(update boxes pos func name focal))
(vec (repeat 256 (ordered-map)))
instructions))
(defn focusing-power [boxes]
(reduce + (for [[i box] (map-indexed vector boxes)
[j [_ focal]] (map-indexed vector box)]
(* (inc i) (inc j) focal))))
(defn solve [input]
(let [steps (aoc/parse-input-line input :words #",")
instructions (map parse-instruction steps)]
[(aoc/sum-map word-hash steps)
(focusing-power (hashmap instructions))]))
(solve (aoc/read-file 15))