Skip to content

Instantly share code, notes, and snippets.

@sotolf2
Created December 2, 2019 16:21
Show Gist options
  • Save sotolf2/1c96a12b2223c0a1c8eb160e68641e9f to your computer and use it in GitHub Desktop.
Save sotolf2/1c96a12b2223c0a1c8eb160e68641e9f to your computer and use it in GitHub Desktop.
#lang racket
(define (get-input)
(map string->number (string-split (car (file->lines "day2.txt")) ",")))
(define (add code ip)
(let ([a (vector-ref code (vector-ref code (+ ip 1)))]
[b (vector-ref code (vector-ref code (+ ip 2)))])
(vector-set! code (vector-ref code (+ ip 3)) (+ a b))
code))
(define (mul code ip)
(let ([a (vector-ref code (vector-ref code (+ ip 1)))]
[b (vector-ref code (vector-ref code (+ ip 2)))])
(vector-set! code (vector-ref code (+ ip 3)) (* a b))
code))
(define (run-code code noun verb)
(define (run-op code ip)
(cond [(= 99 (vector-ref code ip)) code]
[(= 1 (vector-ref code ip)) (run-op (add code ip) (+ 4 ip))]
[(= 2 (vector-ref code ip)) (run-op (mul code ip) (+ 4 ip))]))
(let ([code (list->vector code)])
(vector-set! code 1 noun)
(vector-set! code 2 verb)
(run-op code 0)))
(writeln "Part1: ")
(writeln (vector-ref (run-code (get-input) 12 2) 0))
(define (search code needle)
(for*/first ([noun (in-range 100)]
[verb (in-range 100)]
#:when (= (vector-ref (run-code code noun verb) 0) needle))
(+ (* 100 noun) verb)))
(writeln "Part2: ")
(writeln (search (get-input) 19690720))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment