From 3c432ae236a11d5eac5d3ae128a6b0e7eb24244a Mon Sep 17 00:00:00 2001 From: Jack Kinsey Date: Thu, 5 Dec 2019 20:38:53 -0500 Subject: [PATCH] Fix day5pt1 and add correct day5pt2 --- resources/day05 | 1 + src/adventofcode2019/day05.clj | 59 +++++++++++++++++++++++++--------- 2 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 resources/day05 diff --git a/resources/day05 b/resources/day05 new file mode 100644 index 0000000..a7ddce9 --- /dev/null +++ b/resources/day05 @@ -0,0 +1 @@ +3,225,1,225,6,6,1100,1,238,225,104,0,1101,82,10,225,101,94,44,224,101,-165,224,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,1102,35,77,225,1102,28,71,225,1102,16,36,225,102,51,196,224,101,-3468,224,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1001,48,21,224,101,-57,224,224,4,224,1002,223,8,223,101,6,224,224,1,223,224,223,2,188,40,224,1001,224,-5390,224,4,224,1002,223,8,223,101,2,224,224,1,224,223,223,1101,9,32,224,101,-41,224,224,4,224,1002,223,8,223,1001,224,2,224,1,223,224,223,1102,66,70,225,1002,191,28,224,101,-868,224,224,4,224,102,8,223,223,101,5,224,224,1,224,223,223,1,14,140,224,101,-80,224,224,4,224,1002,223,8,223,101,2,224,224,1,224,223,223,1102,79,70,225,1101,31,65,225,1101,11,68,225,1102,20,32,224,101,-640,224,224,4,224,1002,223,8,223,1001,224,5,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,8,226,226,224,1002,223,2,223,1006,224,329,101,1,223,223,1008,677,677,224,102,2,223,223,1006,224,344,101,1,223,223,1107,226,677,224,102,2,223,223,1005,224,359,101,1,223,223,1008,226,226,224,1002,223,2,223,1006,224,374,1001,223,1,223,1108,677,226,224,1002,223,2,223,1006,224,389,1001,223,1,223,7,677,226,224,1002,223,2,223,1006,224,404,101,1,223,223,7,226,226,224,1002,223,2,223,1005,224,419,101,1,223,223,8,226,677,224,1002,223,2,223,1006,224,434,1001,223,1,223,7,226,677,224,1002,223,2,223,1006,224,449,1001,223,1,223,107,226,677,224,1002,223,2,223,1005,224,464,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,479,101,1,223,223,1007,226,226,224,102,2,223,223,1005,224,494,1001,223,1,223,1108,226,677,224,102,2,223,223,1005,224,509,101,1,223,223,1008,677,226,224,102,2,223,223,1005,224,524,1001,223,1,223,1007,677,226,224,102,2,223,223,1005,224,539,101,1,223,223,1108,226,226,224,1002,223,2,223,1005,224,554,101,1,223,223,108,226,226,224,102,2,223,223,1005,224,569,101,1,223,223,108,677,677,224,102,2,223,223,1005,224,584,101,1,223,223,1107,226,226,224,1002,223,2,223,1006,224,599,101,1,223,223,8,677,226,224,1002,223,2,223,1006,224,614,1001,223,1,223,108,677,226,224,102,2,223,223,1006,224,629,1001,223,1,223,1107,677,226,224,1002,223,2,223,1006,224,644,1001,223,1,223,107,677,677,224,102,2,223,223,1005,224,659,101,1,223,223,107,226,226,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226 diff --git a/src/adventofcode2019/day05.clj b/src/adventofcode2019/day05.clj index 0b2420d..d0d4b3f 100644 --- a/src/adventofcode2019/day05.clj +++ b/src/adventofcode2019/day05.clj @@ -1,22 +1,50 @@ -(ns adventofcode2019.day02 +(ns adventofcode2019.day05 [:require [adventofcode2019.lib :refer :all] [clojure.string :as str]]) -(def operations {1 [#(assoc %4 %3 (+ %1 %2)) 3] - 2 [#(assoc %4 %3 (* %1 %2)) 3] - 3 [#(do (println (%2 %1)) %2) 1] - 4 [#(assoc %2 %1 (parse-int (read-line))) 1]}) -(def decode-op (memoize (fn [opcode] +;; 0: function args&mem -> [mem (ctr -> ctr)] +;; 1: number of args +(def operations {1 [(fn [a b c mem] + [(assoc mem c (+ a b)) #(+ % 4)]) + 3] + 2 [(fn [a b c mem] + [(assoc mem c (* a b)) #(+ % 4)]) + 3] + 3 [(fn [a mem] + [(assoc mem a (parse-int (read-line))) #(+ % 2)]) + 1] + 4 [(fn [a mem] + (println (mem a)) + [mem #(+ % 2)]) + 1] + 5 [(fn [a b mem] + [mem (if (not= a 0) (constantly b) #(+ % 3))]) + 2] + 6 [(fn [a b mem] + [mem (if (= a 0) (constantly b) #(+ % 3))]) + 2] + 7 [(fn [a b c mem] + [(assoc mem c (if (< a b) 1 0)) #(+ % 4)]) + 3] + 8 [(fn [a b c mem] + [(assoc mem c (if (= a b) 1 0)) #(+ % 4)]) + 3]}) + +;; FIXME: sorry about the dropped forms i didn't want to fix it properly +;; the problem is that flags don't apply to certain args for certain ops +(defn decode-op [opcode] (let [str-code (format "%05d" opcode) [f3 f2 f1] (map #(= \1 %) (take 3 str-code)) op (parse-int (str/join (drop 3 str-code))) [operation arg-ct] (operations op)] [(case arg-ct - 1 #(operation ((if f1 identity %1) %2) %1) + 1 #(operation #_((if f1 identity %1) %2) %2 %1) + 2 #(operation ((if f1 identity %1) %2) + ((if f2 identity %1) %3) %1) 3 #(operation ((if f1 identity %1) %2) ((if f2 identity %1) %3) - ((if f3 identity %1) %4) %1) - nil) arg-ct])))) + #_((if f3 identity %1) %4) %4 %1)) + arg-ct])) (defn perform-operation [program counter] (let [opcode (program counter) @@ -25,8 +53,8 @@ (rest) (take arg-ct) (map program))] - [(apply operation program args) - (+ counter arg-ct 1)])) + (let [[program ctr-update] (apply operation program args)] + [program (ctr-update counter)]))) (defn intcode [program] (loop [[program counter] [program 0]] @@ -35,8 +63,9 @@ program (recur (perform-operation program counter)))))) -(defn day02 [] +(defn day05 [] (let [input (mapv parse-int (get-list-from-file (input-file) #","))] - (part1 "see below") - (intcode input) - #_(part2))) + (part1 "(input `1`)") + (intcode input) + (part2 "(input `5`)") + (intcode input))) -- 2.38.5