-
Notifications
You must be signed in to change notification settings - Fork 2
/
2.clj
35 lines (30 loc) · 915 Bytes
/
2.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
(ns advent-of-code.2019.2
(:require [advent-of-code.elves :refer :all]
[clojure.string :refer [split]]))
(def values
(mapv parse-long (split (first (day->input-lines 2019 2)) #",")))
;; solve part one
(def op-codes
{1 +
2 *
99 :exit})
(defn run [program]
(loop [program program
pos 0]
(let [op (op-codes (program pos))]
(if (= :exit op)
program
(let [[pos-a pos-b pos-dest] (subvec program (inc pos) (inc (+ pos 4)))
a (program pos-a)
b (program pos-b)]
(recur (assoc program pos-dest (op a b))
(+ pos 4)))))))
(run (assoc values 1 12, 2 2))
;; solve part two
(for [noun (range 0 (inc 99))
verb (range 0 (inc 99))
:let [output (-> values
(assoc 1 noun 2 verb)
(run))]
:when (= 19690720 (first output))]
(+ verb (* 100 noun)))