DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 2: 1202 Program Alarm

Collapse
 
katafrakt profile image
Paweł Świątkowski • Edited

I can't believe no one has posted a solution in Janet yet! Oh wait, no, I totally believe it. It was also my first attempt in this language. I've been looking at it for some time already but only today I decided to give it a shot. It basically feels like a imperativ-ish LISP.

(def input "1,0,0,the rest of your input...")

(defn run-prog [noun verb]
  (def codes (map (fn [x] (scan-number x)) (string/split "," input)))
  (put codes 1 noun)
  (put codes 2 verb)

  (var curpos 0)
  (var curinstr (get codes curpos))
  (while (not (= curinstr 99))
    (def elem1 (get codes (+ curpos 1)))
    (def elem2 (get codes (+ curpos 2)))
    (def elem3 (get codes (+ curpos 3)))

    (if (= curinstr 1)
      (put codes elem3 (+ (get codes elem1) (get codes elem2))))

    (if (= curinstr 2)
      (put codes elem3 (* (get codes elem1) (get codes elem2))))

    (set curpos (+ curpos 4))
    (set curinstr (get codes curpos)))
  (get codes 0))

(print (string/format "%d" (run-prog 12 2)))

(loop [noun :range [0 99]]
  (loop [verb :range [0 99]]
    (def res (run-prog noun verb))
    (if (= res 19690720)
      (print (string/format "%d" (+ (* 100 noun) verb))))))