diff options
author | brian <brianmchu42@gmail.com> | 2024-08-03 13:33:43 -0700 |
---|---|---|
committer | brian <brianmchu42@gmail.com> | 2024-08-03 13:33:43 -0700 |
commit | a88e8da779cd09c398723e4e87e79d945af0994e (patch) | |
tree | 0be5a89e050f088ebc166433c4ce251d43062bb4 /day2.scm | |
download | AdventOfCode2019-a88e8da779cd09c398723e4e87e79d945af0994e.tar.gz |
initial commit to day 6 main
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 |