From a88e8da779cd09c398723e4e87e79d945af0994e Mon Sep 17 00:00:00 2001 From: brian Date: Sat, 3 Aug 2024 13:33:43 -0700 Subject: initial commit to day 6 --- day2.scm | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 day2.scm (limited to 'day2.scm') 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 -- cgit 1.4.1-2-gfad0