From a521c98193a01927f5d4f7aa245a6fc9ca7d8ad9 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Wed, 9 Dec 2020 19:11:42 +0100 Subject: day08 --- day08.clj | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 day08.clj (limited to 'day08.clj') diff --git a/day08.clj b/day08.clj new file mode 100644 index 0000000..d6b2d60 --- /dev/null +++ b/day08.clj @@ -0,0 +1,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 -- cgit 1.4.1