Skip to content

Instantly share code, notes, and snippets.

@invasionofsmallcubes
Last active December 3, 2019 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save invasionofsmallcubes/2ee9e664af187f0cc24736aeacc76627 to your computer and use it in GitHub Desktop.
Save invasionofsmallcubes/2ee9e664af187f0cc24736aeacc76627 to your computer and use it in GitHub Desktop.
Advent of Code Day 2 + Ex 2 #adventOfCode2019
;; https://adventofcode.com/2019/day/2
(def intcodeProgram []) ;; get from the exercise
(defn op
[step program instruction]
(let [pos1 (nth program (nth program (+ step 1)))
pos2 (nth program (nth program (+ step 2)))
resultPos (nth program (+ step 3))]
(assoc program resultPos (instruction pos1 pos2))))
(defn intcode
[program]
(let [length (count program)]
(loop [step 0
currentProgram program]
(let [currentElement (nth currentProgram step)]
(case currentElement
1 (recur (+ step 4) (op step currentProgram +))
2 (recur (+ step 4) (op step currentProgram *))
99 currentProgram)))))
(defn newProgram
[program couple]
(let [[i j] couple]
(assoc (assoc program 1 i) 2 j)))
(ns example.core
(:require [clojure.math.combinatorics :as combo]))
(defn doubleLoop
[program]
(let [length (count program)]
(combo/cartesian-product (range 0 length) (range 0 length))))
(defn find-first
[f coll]
(first (filter f coll)))
(defn findSolution
[program value]
(let [stream (doubleLoop program)]
(find-first #(= value (nth (intcode (newProgram program %) ) 0)) stream)))
(let [[noun verb](findSolution intcodeProgram 19690720)]
(+(* 100 noun) verb))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment