diff options
Diffstat (limited to 'day2.scm')
-rwxr-xr-x | day2.scm | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/day2.scm b/day2.scm new file mode 100755 index 0000000..bb6571f --- /dev/null +++ b/day2.scm @@ -0,0 +1,53 @@ +#!/usr/bin/env gosh +(use file.util) + +(define (read-input filename) + (map string->number (string-split (file->string filename) ","))) + +(define (run-opcode vec pos) + (let ((opcode (vector-ref vec pos))) + (cond + ((= opcode 1) + (vector-set! + vec + (vector-ref vec (+ pos 3)) + (+ (vector-ref vec (vector-ref vec (+ pos 1))) + (vector-ref vec (vector-ref vec (+ pos 2))))) + (run-opcode vec (+ pos 4))) + ((= opcode 2) + (vector-set! + vec + (vector-ref vec (+ pos 3)) + (* (vector-ref vec (vector-ref vec (+ pos 1))) + (vector-ref vec (vector-ref vec (+ pos 2))))) + (run-opcode vec (+ pos 4))) + ((= opcode 99) vec)))) + +(define (run-program input noun verb) + (let ((input-vector (list->vector input))) + (vector-set! input-vector 1 noun) + (vector-set! input-vector 2 verb) + (vector-ref (run-opcode input-vector 0) 0))) + +(define (part1 input) + (run-program input 12 2)) + +(define cartesian-product (lambda (xs ys) + (if (or (zero? (length xs)) (zero? (length ys))) + '() + (fold append '() (map (lambda (x) (map (lambda (y) (cons x y)) ys)) xs))))) + +(define (part2 input) + (letrec ((nouns (iota 100)) + (verbs (iota 100)) + (output (find + (lambda (x) + (= 19690720 + (run-program input (car x) (cdr x)))) + (cartesian-product nouns verbs)))) + (+ (cdr output) (* 100 (car output))))) + +(define (main args) + (let ((input (read-input "inputs/day2.txt"))) + (print (part1 input)) + (print (part2 input)))) \ No newline at end of file |