aboutsummaryrefslogtreecommitdiff
path: root/aoc2019/day-02
diff options
context:
space:
mode:
authorJ.J <thechairman@thechairman.info>2023-11-30 17:10:00 -0500
committerJ.J <thechairman@thechairman.info>2023-11-30 17:10:00 -0500
commit8ab65dc2da1742eb86ec636c50c7018385b68167 (patch)
treec4fd556aca9b867cfa1f2f174128c30857353884 /aoc2019/day-02
parentfafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1 (diff)
downloadgleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.tar.gz
gleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.zip
prep for 2023, renaming for consistency
Diffstat (limited to 'aoc2019/day-02')
-rw-r--r--aoc2019/day-02/day-02.rkt32
1 files changed, 32 insertions, 0 deletions
diff --git a/aoc2019/day-02/day-02.rkt b/aoc2019/day-02/day-02.rkt
new file mode 100644
index 0000000..56019e8
--- /dev/null
+++ b/aoc2019/day-02/day-02.rkt
@@ -0,0 +1,32 @@
+#lang racket
+
+(require advent-of-code
+ threading)
+
+(define initial-opcodes
+ (parameterize ([current-readtable (make-readtable #f #\, #\space #f)])
+ (port->list read (open-aoc-input (find-session) 2019 2 #:cache #true))))
+
+;; part 1
+(define (edit-opcode ocs oc-1 oc-2)
+ (~> ocs (list-set 1 oc-1) (list-set 2 oc-2)))
+
+(define (run-intcode ocs)
+ (for/fold ([ocs ocs] #:result (car ocs))
+ ([pointer (in-range 0 (length initial-opcodes) 4)] #:break (= 99 (list-ref ocs pointer)))
+ (define op
+ (match (list-ref ocs pointer)
+ [1 +]
+ [2 *]))
+ (list-set ocs
+ (list-ref ocs (+ 3 pointer))
+ (op (list-ref ocs (list-ref ocs (+ 1 pointer)))
+ (list-ref ocs (list-ref ocs (+ 2 pointer)))))))
+
+(~> initial-opcodes (edit-opcode 12 2) run-intcode)
+
+;; part 2
+(for*/first ([noun (inclusive-range 0 99)]
+ [verb (inclusive-range 0 99)]
+ #:when (~> initial-opcodes (edit-opcode noun verb) run-intcode (= 19690720)))
+ (+ (* 100 noun) verb))