master
Matthew Butterick 7 years ago
parent 34ecb04da4
commit a2d11e5688

@ -0,0 +1,31 @@
#lang reader "../aoc-lang.rkt"
(provide (rename-out [#%mb #%module-begin]) ★★)
(define-macro (#%mb (STARS) TOKS ...)
#`(#%module-begin
(time (STARS (vector (λ () TOKS) ...)))))
(define regs (make-hasheq))
(define last-sound-played (make-parameter #f))
(struct offset-signal (val))
(struct end-signal (val))
(provide snd set add mul mod rcv jgz)
(define-macro (value VAL) #'(let ([val 'VAL]) (if (number? val) val (hash-ref regs val))))
(define-macro (snd REG) #'(last-sound-played (hash-ref regs 'REG)))
(define-macro (set REG VAL) #'(hash-set! regs 'REG (value VAL)))
(define-macro (add REG VAL) #'(hash-update! regs 'REG (λ (val) (+ val (value VAL))) 0))
(define-macro (mul REG VAL) #'(hash-update! regs 'REG (λ (val) (* val (value VAL))) 0))
(define-macro (mod REG VAL) #'(hash-update! regs 'REG (λ (val) (modulo val (value VAL))) 0))
(define-macro (rcv REG) #'(unless (zero? (hash-ref regs 'REG)) (raise (last-sound-played))))
(define-macro (jgz REG VAL) #'(when (positive? (hash-ref regs 'REG)) (raise (offset-signal (value VAL)))))
(define ( insts)
(with-handlers ([number? values])
(for/fold ([offset 0])
([i (in-naturals)])
(with-handlers ([offset-signal? (λ (os) (+ (offset-signal-val os) offset))])
(define proc (vector-ref insts offset))
(proc)
(add1 offset)))))

@ -0,0 +1,42 @@
#lang reader "main.rkt"
set i 31
set a 1
mul p 17
jgz p p
mul a 2
add i -1
jgz i -2
add a -1
set i 127
set p 464
mul p 8505
mod p a
mul p 129749
add p 12345
mod p a
set b p
mod b 10000
snd b
add i -1
jgz i -9
jgz a 3
rcv b
jgz b -1
set f 0
set i 126
rcv a
rcv b
set p a
mul p -1
add p b
jgz p 4
snd a
set a b
jgz 1 3
snd b
set f 1
add i -1
jgz i -11
snd a
jgz f -16
jgz a -19

@ -0,0 +1,11 @@
#lang reader "main.rkt" ; 4
set a 1
add a 2
mul a a
mod a 5
snd a
set a 0
rcv a
jgz a -1
set a 1
jgz a -2