You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
aoc-racket/2019/09.rkt

95 lines
3.8 KiB
Racket

5 years ago
#lang br
(require racket/file rackunit)
(define (string->ints str) (map string->number (string-split str #px",\\s*")))
(define (string->regs str) (list->vector (string->ints str)))
(define ((binarize proc) x y) (if (proc x y) 1 0))
(define (make-runner program [calling-thd (current-thread)])
(thread (λ ()
(define regs (string->regs program))
5 years ago
(define (maybe-enlarge-regs ptr)
(unless (< ptr (vector-length regs))
(define newvec (make-vector (add1 ptr) 0))
5 years ago
(vector-copy! newvec 0 regs)
(set! regs newvec))
regs)
(define (reg-ref ptr) (vector-ref (maybe-enlarge-regs ptr) ptr))
(define (reg-set! ptr val) (vector-set! (maybe-enlarge-regs ptr) ptr val))
5 years ago
(define relative-base 0)
(let/ec terminate
(let loop ([ptr 0])
(define inst (for/list ([c (in-string (~r (reg-ref ptr) #:min-width 5 #:pad-string "0"))])
5 years ago
(string->number (string c))))
5 years ago
(define-values (opcode resolve)
5 years ago
(match inst
5 years ago
[(list d4 d3 d2 d1 d0)
5 years ago
(values (+ (* 10 d1) d0)
(λ (ptr offset)
(define parameter-value (match offset [1 d2] [2 d3] [3 d4]))
(define ptr-resolver (match parameter-value
[0 reg-ref] ; position
[1 values] ; immediate
[2 (compose1 (λ (ptr) (+ relative-base ptr)) reg-ref)])); relative
(ptr-resolver (+ ptr offset))))]))
5 years ago
(define next-ptr
5 years ago
(match opcode
5 years ago
[(or 1 2 7 8) ; 4-arity: add & multiply & compare
5 years ago
(reg-set! (resolve ptr 3) ((match opcode
[1 +]
[2 *]
[7 (binarize <)]
[8 (binarize =)]) (reg-ref (resolve ptr 1)) (reg-ref (resolve ptr 2))))
5 years ago
(+ ptr 4)]
[(or 3 4 9) ; 2-arity: input & output
(match opcode
5 years ago
[3 (reg-set! (resolve ptr 1) (thread-receive))]
[4 (thread-send calling-thd (reg-ref (resolve ptr 1)))]
[9 (set! relative-base (+ relative-base (reg-ref (resolve ptr 1))))])
5 years ago
(+ ptr 2)]
[(or 5 6) ; 3-arity: jump
(if ((match opcode
[5 not]
5 years ago
[6 values]) (zero? (reg-ref (resolve ptr 1))))
(reg-ref (resolve ptr 2))
5 years ago
(+ ptr 3))]
[99 (thread-send calling-thd 'done) (terminate)]
5 years ago
[_ (error "unknown opcode" opcode)]))
5 years ago
(loop next-ptr))))))
(define (run program-or-thread)
(define thd (match program-or-thread
[(? thread? thd) thd]
[program (make-runner program)]))
5 years ago
(for/list ([val (in-producer thread-receive 'done)])
val))
5 years ago
(check-equal?
(run "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99")
'(109 1 204 -1 1001 100 1 100 1008 100 16 101 1006 101 0 99))
5 years ago
5 years ago
(check-equal?
(run "1102,34915192,34915192,7,4,7,99,0")
'(1219070632396864))
(check-equal?
(run "104,1125899906842624,99")
'(1125899906842624))
5 years ago
;; 1
5 years ago
(define t1 (make-runner (file->string "09.rktd")))
(thread-send t1 1)
5 years ago
(check-equal?
(run t1)
'(3601950151))
5 years ago
;; 2
(define t2 (make-runner (file->string "09.rktd")))
(thread-send t2 2)
(check-equal?
(run t2)
'(64236))