DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 9: Sensor Boost

Collapse
 
johnnyjayjay profile image
Johnny

Today's addition was simple enough. For safe access to "not yet allocated" memory, I created 2 new helper functions:

; Reads a value from memory
(defn read-mem [process-state address]
  (let [mem (:memory process-state)]
    (if (>= address (count mem)) 0 (mem address))))

; Writes a value to memory and returns the new process state
(defn write-mem [process-state address value]
  (let [mem (:memory process-state)]
    (if (>= address (count mem))
      (assoc process-state :memory (conj (into mem (repeat (- address (count mem)) 0)) value))
      (assoc-in process-state [:memory address] value))))

And after changing the instruction type from int to long (which is the default number type in clojure anyway), I could just add the new stuff to my existing framework:

; new parameter mode function
(fn [process-state address]
  (+ (read-mem process-state address) (:rel-base process-state)))

; new opcode
(def rel-base-offset-opcode
  {:operator (fn [process-state offset-pointer]
               (-> process-state
                   (update :rel-base + (read-mem process-state offset-pointer))
                   (update :pointer + 2)))
   :params   1})

As always, here's the whole code: github.com/jkoenig134/AdventOfCode...