summary refs log tree commit diff stats
path: root/day5.scm
diff options
context:
space:
mode:
Diffstat (limited to 'day5.scm')
-rwxr-xr-xday5.scm89
1 files changed, 89 insertions, 0 deletions
diff --git a/day5.scm b/day5.scm
new file mode 100755
index 0000000..3c35310
--- /dev/null
+++ b/day5.scm
@@ -0,0 +1,89 @@
+#!/usr/bin/env gosh
+(use file.util)
+
+(define (read-input filename)
+  (map string->number (string-split (file->string filename) ",")))
+
+(define (run-opcode vec pos)
+  (letrec ((instruction (vector-ref vec pos))
+           (opcode (mod instruction 100))
+           (mode1 (= 1 (div (mod instruction 1000) 100)))
+           (mode2 (= 1 (div (mod instruction 10000) 1000)))
+           (mode3 (= 1 (div instruction 10000)))
+           (parameter 
+            (lambda (x)
+              (cond
+                ((= x 1)
+                  (if mode1 
+                    (vector-ref vec (+ pos 1))
+                    (vector-ref vec (vector-ref vec (+ pos 1)))))
+                ((= x 2)
+                  (if mode2
+                    (vector-ref vec (+ pos 2))
+                    (vector-ref vec (vector-ref vec (+ pos 2)))))
+                ((= x 3)
+                  (if mode3
+                    (vector-ref vec (+ pos 3))
+                    (vector-ref vec (vector-ref vec (+ pos 3)))))))))
+    (print "opcode: " opcode)
+    (print "real params: " 
+      (vector-ref vec (+ pos 1)) " " 
+      (vector-ref vec (+ pos 2)) " " 
+      (vector-ref vec (+ pos 3)))
+    (print "resolved params: "
+      (parameter 1) " "
+      (parameter 2) " "
+      (parameter 3))
+    (print vec)
+    (cond
+      ((= opcode 1)
+        (vector-set! 
+          vec
+          (vector-ref vec (+ pos 3)) 
+          (+ (parameter 1) 
+             (parameter 2)))
+        (run-opcode vec (+ pos 4)))
+      ((= opcode 2)
+        (vector-set! 
+          vec 
+          (vector-ref vec (+ pos 3))
+          (* (parameter 1)
+             (parameter 2)))
+        (run-opcode vec (+ pos 4)))
+      ((= opcode 3)
+        (vector-set!
+          vec
+          (vector-ref vec (+ pos 1))
+          (string->number (read-line)))
+        (run-opcode vec (+ pos 2)))
+      ((= opcode 4)
+        (print (parameter 1))
+        (run-opcode vec (+ pos 2)))
+      ((= opcode 5) 
+        (if (= (parameter 1) 0)
+          (run-opcode vec (+ pos 3))
+          (run-opcode vec (parameter 2))))
+      ((= opcode 6)
+        (if (= (parameter 1) 0)
+          (run-opcode vec (parameter 2))
+          (run-opcode vec (+ pos 3))))
+      ((= opcode 7)
+        (if (< (parameter 1) (parameter 2))
+          (vector-set! vec (parameter 3) 1)
+          (vector-set! vec (parameter 3) 0))
+        (run-opcode vec (+ pos 4)))
+      ((= opcode 8)
+        (if (= (parameter 1) (parameter 2))
+          (vector-set! vec (parameter 3) 1)
+          (vector-set! vec (parameter 3) 0))
+        (run-opcode vec (+ pos 4)))
+      ((= opcode 99)
+        vec)
+      (else (print "this shouldn't happen")))))
+
+(define (part1 input)
+  (run-opcode input 0))
+
+(define (main args)
+  (let ((input (read-input "inputs/day5test2.txt")))
+    (part1 (list->vector input))))
\ No newline at end of file