Skip to content

Instantly share code, notes, and snippets.

@roman01la
Created December 5, 2019 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roman01la/875d7c0c3cc8b75267730559b5d96251 to your computer and use it in GitHub Desktop.
Save roman01la/875d7c0c3cc8b75267730559b5d96251 to your computer and use it in GitHub Desktop.
AoC 2019, Day 5
(defn prefix-params [default params]
(let [diff-ln (- (count default) (count params))]
(if-not (== 0 diff-ln)
(str (subs default 0 diff-ln) params)
params)))
(defn read-instruction [s]
(case s
"1" [1 0 0 1]
"2" [2 0 0 1]
"3" [3 1]
"4" [4 0]
"7" [7 0 0 1]
"8" [8 0 0 1]
"99" [99]
(let [[_ params s] (re-find #"([0-9]+)([0-9]{2})$" s)
opcode (Integer/parseInt s)
params (case opcode
1 (prefix-params "100" params)
2 (prefix-params "100" params)
5 (prefix-params "00" params)
6 (prefix-params "00" params)
7 (prefix-params "100" params)
8 (prefix-params "100" params)
params)
params (->> (reverse params)
(mapv #(Character/digit ^char % 10)))]
(into [opcode] params))))
(defn get-param [memory offset mode]
(let [v (get memory offset)]
(condp == mode
0 (get memory v)
1 v
(throw (Error. (str "Unknown parameter mode " mode))))))
(defn read-params [memory offset params]
(map-indexed #(get-param memory (+ %1 offset) %2) params))
(defn next-offset ^long [offset params]
(+ offset (count params) 1))
(defn interpret [memory input]
(let [memory (transient memory)]
(loop [offset 0
log (list)]
(let [[opcode & raw-params] (read-instruction (str (get memory offset)))
params (read-params memory (inc offset) raw-params)]
(condp == opcode
1 (let [[a b c] params]
(assoc! memory c (+ a b))
(recur (next-offset offset params) log))
2 (let [[a b c] params]
(assoc! memory c (* a b))
(recur (next-offset offset params) log))
3 (let [[a] params]
(assoc! memory a input)
(recur (next-offset offset params) log))
4 (let [[a] params]
(recur (next-offset offset params) (cons a log)))
5 (let [[a b] params]
(if-not (== 0 a)
(recur b log)
(recur (next-offset offset params) log)))
6 (let [[a b] params]
(if (== 0 a)
(recur b log)
(recur (next-offset offset params) log)))
7 (let [[a b c] params]
(if (< a b)
(assoc! memory c 1)
(assoc! memory c 0))
(recur (next-offset offset params) log))
8 (let [[a b c] params]
(if (== a b)
(assoc! memory c 1)
(assoc! memory c 0))
(recur (next-offset offset params) log))
99 (when (every? zero? (rest log))
(first log))
(throw (Error. (str "Unknown opcode " opcode))))))))
(def input
(->> (clojure.string/split (slurp "input_2019_5") #",")
(map read-string)))
(defn input->memory [input]
(->> input
(map-indexed #(vector %1 %2))
(into {})))
(def memory (input->memory input))
(comment
(= 7265618 (interpret memory 1))
(= 7731427 (interpret memory 5)))
3,225,1,225,6,6,1100,1,238,225,104,0,1102,59,58,224,1001,224,-3422,224,4,224,102,8,223,223,101,3,224,224,1,224,223,223,1101,59,30,225,1101,53,84,224,101,-137,224,224,4,224,1002,223,8,223,101,3,224,224,1,223,224,223,1102,42,83,225,2,140,88,224,1001,224,-4891,224,4,224,1002,223,8,223,1001,224,5,224,1,223,224,223,1101,61,67,225,101,46,62,224,1001,224,-129,224,4,224,1002,223,8,223,101,5,224,224,1,223,224,223,1102,53,40,225,1001,35,35,224,1001,224,-94,224,4,224,102,8,223,223,101,6,224,224,1,223,224,223,1101,5,73,225,1002,191,52,224,1001,224,-1872,224,4,224,1002,223,8,223,1001,224,5,224,1,223,224,223,102,82,195,224,101,-738,224,224,4,224,1002,223,8,223,1001,224,2,224,1,224,223,223,1101,83,52,225,1101,36,77,225,1101,9,10,225,1,113,187,224,1001,224,-136,224,4,224,1002,223,8,223,101,2,224,224,1,224,223,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1007,226,226,224,1002,223,2,223,1006,224,329,1001,223,1,223,1108,226,226,224,102,2,223,223,1006,224,344,101,1,223,223,1007,677,677,224,102,2,223,223,1006,224,359,101,1,223,223,1108,677,226,224,1002,223,2,223,1005,224,374,1001,223,1,223,7,677,226,224,102,2,223,223,1005,224,389,1001,223,1,223,1008,677,677,224,1002,223,2,223,1005,224,404,101,1,223,223,108,226,226,224,1002,223,2,223,1006,224,419,101,1,223,223,1008,226,677,224,1002,223,2,223,1006,224,434,1001,223,1,223,1107,677,226,224,1002,223,2,223,1005,224,449,101,1,223,223,1008,226,226,224,102,2,223,223,1005,224,464,1001,223,1,223,8,226,226,224,1002,223,2,223,1006,224,479,1001,223,1,223,107,226,677,224,102,2,223,223,1005,224,494,1001,223,1,223,7,226,226,224,102,2,223,223,1005,224,509,1001,223,1,223,107,226,226,224,102,2,223,223,1005,224,524,101,1,223,223,107,677,677,224,1002,223,2,223,1006,224,539,101,1,223,223,8,677,226,224,1002,223,2,223,1006,224,554,101,1,223,223,1107,677,677,224,1002,223,2,223,1005,224,569,101,1,223,223,108,226,677,224,1002,223,2,223,1006,224,584,101,1,223,223,7,226,677,224,1002,223,2,223,1005,224,599,1001,223,1,223,8,226,677,224,102,2,223,223,1006,224,614,1001,223,1,223,108,677,677,224,1002,223,2,223,1006,224,629,1001,223,1,223,1007,226,677,224,1002,223,2,223,1006,224,644,101,1,223,223,1108,226,677,224,102,2,223,223,1005,224,659,1001,223,1,223,1107,226,677,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment