aboutsummaryrefslogtreecommitdiff
path: root/2019/day-02
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-25 14:31:10 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-25 14:31:10 -0500
commitfd94f5238073368d96d9af28070402411c2ad28f (patch)
tree6693e0b8a9f31ce4e461c5a8a9a0f08af482a52f /2019/day-02
parentbb236ec6e5b41840ac0f10c68b332e68a43cfc2a (diff)
downloadgleam_aoc-fd94f5238073368d96d9af28070402411c2ad28f.tar.gz
gleam_aoc-fd94f5238073368d96d9af28070402411c2ad28f.zip
2019 day 2 and 3 complete
Diffstat (limited to '2019/day-02')
-rw-r--r--2019/day-02/day-02.rkt32
1 files changed, 32 insertions, 0 deletions
diff --git a/2019/day-02/day-02.rkt b/2019/day-02/day-02.rkt
new file mode 100644
index 0000000..56019e8
--- /dev/null
+++ b/2019/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))