about summary refs log tree commit diff
path: root/day08.clj
blob: d6b2d60972bbdae5a341f4040173863b33a535fc (plain) (blame)
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
(defn parse [l]
  (let [words (clojure.string/split l #" ")]
    [(first words) (Integer/parseInt (second words))]))

(def data
  (->> (slurp "day08")
       (clojure.string/split-lines)
       (map parse)
       vec))

(defn run [p ip acc seen]
  (if (or (seen ip) (>= ip (count p)))
    [ip acc]
    (let [seen (conj seen ip)
          [op arg] (p ip)]
      (case op
        "nop" (recur p (inc ip)   acc         seen)
        "acc" (recur p (inc ip)   (+ acc arg) seen)
        "jmp" (recur p (+ ip arg) acc         seen)))))

(second (run data 0 0 #{})) ; => 1451

(some (fn [n]
        (let [[ip acc] (run (update-in data [n 0]
                                       {"nop" "jmp" ,"jmp" "nop", "acc" "acc"})
                         0 0 #{})]
          (if (= ip (count data))
            acc)))
      (range (count data))) ; => 1160