Advent of Code day 2

Digging deeper in the way of thinking in Clojure, which I can only assume is a gateway drug to Meth.

(ns day-2.core
  (:require [clojure.string :as str])
  (:gen-class))

(defn solver [x y data]

  (loop [iptr 0
         d (assoc (assoc data 1 x) 2 y)]

    (if (not= (nth d iptr) 99)
      (let [a1 (nth d (+ iptr 1))
            a2 (nth d (+ iptr 2))
            a3 (nth d (+ iptr 3))
            ]

        (recur (+ iptr 4) (case (nth d iptr)
                            1  (assoc d a3 (+ (nth d a1) (nth d a2)))
                            2  (assoc d a3 (* (nth d a1) (nth d a2)))
                            ))
        )

      ;; else
      d
      )
    )
  )

(defn -main [& args]

  (def data (into []  (map #(Integer/parseInt %) (str/split (clojure.string/trim (slurp "./data/data.txt")) #","))))

  ;; Part 1
  (println (solver 12 2 data))

  ;; Part 2
  ;; plot out a few values to see if you can figure out what the program is doing
  ;; once you figure ou the pattern for the first digit and second digit, you have the answer
  ;; (println (for [x (range 0 99)]
  ;;   [x (nth (solver 41 x data) 0)]
  ;; ))
)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s