aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2021-12-25 23:22:50 -0500
committerClyne Sullivan <clyne@bitgloo.com>2021-12-25 23:22:50 -0500
commit7a78309cf5492a1aeae7cd551ceb9906026e938b (patch)
tree67b936adf8e8b109a46a2e725e8088636a9925b8
parent6d3da47455a0283cabe72f488368782dc138f5ee (diff)
add day 24
-rw-r--r--day24/partboth.clj47
-rw-r--r--day24/solved-by-hand-with-help.md4
2 files changed, 51 insertions, 0 deletions
diff --git a/day24/partboth.clj b/day24/partboth.clj
new file mode 100644
index 0000000..4874f62
--- /dev/null
+++ b/day24/partboth.clj
@@ -0,0 +1,47 @@
+(require '[clojure.string :as str])
+
+(defn alu-parse-argument [arg]
+ (if (contains? #{\w \x \y \z} (first arg))
+ (str/join ["@" arg])
+ (read-string arg)))
+
+(defn alu-parse-instruction [[ins arg1 arg2]]
+ (let [arg2-parsed (if (some? arg2) (alu-parse-argument arg2) 0)]
+ (case ins
+ "inp"
+ (str/join ["(reset! " arg1 " (- (int (get input (swap! index inc))) 48))\n"])
+ "add"
+ (str/join ["(swap! " arg1 " +' " arg2-parsed ")\n"])
+ "mul"
+ (str/join ["(swap! " arg1 " *' " arg2-parsed ")\n"])
+ "div"
+ (str/join ["(swap! " arg1 " quot " arg2-parsed ")\n"])
+ "mod"
+ (str/join ["(swap! " arg1 " rem " arg2-parsed ")\n"])
+ "eql"
+ (str/join ["(swap! " arg1 " #(if (= % " arg2-parsed ") 1 0))\n"]))))
+
+(def alu-program-header
+ "(defn alu-program [input]
+ (let [index (atom -1) w (atom 0) x (atom 0) y (atom 0) z (atom 0)]\n")
+
+(defn alu-compile-program [instructions]
+ (let [parsed-ins (mapv alu-parse-instruction instructions)]
+ (eval (read-string
+ (str/join [alu-program-header
+ (str/join parsed-ins)
+ "@z))"])))))
+
+(def program (->> (slurp "./in")
+ str/split-lines
+ (map #(str/split % #"\s+"))
+ (alu-compile-program)))
+
+; to split by `inp w`s:
+; (partition 18)
+; (reverse)
+; (mapv alu-compile-program)))
+
+(println "Part 1:" (program "16181111641521"))
+(println "Part 2:" (program "59692994994998"))
+
diff --git a/day24/solved-by-hand-with-help.md b/day24/solved-by-hand-with-help.md
new file mode 100644
index 0000000..1ddf03e
--- /dev/null
+++ b/day24/solved-by-hand-with-help.md
@@ -0,0 +1,4 @@
+Went through this wonderful guide:
+
+[https://github.com/mrphlip/aoc/blob/master/2021/24.md](https://github.com/mrphlip/aoc/blob/master/2021/24.md)
+