-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-14.clj
52 lines (43 loc) · 1.34 KB
/
day-14.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 day-13 (:require [hashp.core]))
(def parse-bin #(Long/parseLong % 2))
(def parse-int #(Long/parseLong %))
(defn parse [[instr value]]
(case instr
"mask" {:instr :mask :value value}
{:instr :mem
:index (parse-int (re-find #"\d+" instr))
:value (parse-int value)}))
(defn mask-bit [m v]
(case m
\X #{\0 \1}
\1 \1
\0 v))
(defn store-indirect [mem dests value]
(reduce #(assoc %1 %2 value) mem dests))
(defn bifurcate [head [bit & address]]
(cond
(nil? bit) (list head)
(set? bit) (mapcat #(bifurcate (conj head %) address) bit)
:else (recur (conj head bit) address)))
(defn apply-mask [mask value]
(let [binary (Long/toString value 2)
padding (repeat (- (count mask) (count binary)) \0)]
(->> (clojure.string/join (concat padding binary))
(map mask-bit mask)
(bifurcate [])
(map clojure.string/join)
(map parse-bin))))
(defn execute [acc instr]
(case (:instr instr)
:mask (assoc acc :mask (:value instr))
:mem (update acc :mem store-indirect (apply-mask (:mask acc) (:index instr)) (:value instr))))
(defn puzzle2 [in]
(->> (clojure.string/split-lines input)
(map #(clojure.string/split % #" = "))
(map parse)
(reduce execute {:mem {}})
(:mem)
(vals)
(apply +)))
(def input (slurp "input14.txt"))
(puzzle2 input)