d23 s1
parent
d8d2a87f7b
commit
c22ba84d5e
@ -0,0 +1,27 @@
|
||||
#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))
|
||||
(struct offset-signal (val))
|
||||
|
||||
(provide set mul sub jnz)
|
||||
(define-macro (value VAL) #'(let ([val 'VAL]) (if (number? val) val (hash-ref! regs val 0))))
|
||||
(define-macro (set REG VAL) #'(hash-set! regs 'REG (value VAL)))
|
||||
(define-macro (sub REG VAL) #'(hash-update! regs 'REG (λ (val) (- val (value VAL))) 0))
|
||||
(define-macro (mul REG VAL) #'(begin (hash-update! regs 'mul-count add1 0)
|
||||
(hash-update! regs 'REG (λ (val) (* val (value VAL))) 0)))
|
||||
(define-macro (jnz REG VAL) #'(when (not (zero? (value REG))) (raise (offset-signal (value VAL)))))
|
||||
|
||||
(define (★ insts)
|
||||
(for/fold ([offset 0]
|
||||
#:result (hash-ref regs 'mul-count))
|
||||
([i (in-naturals)]
|
||||
#:break (not (<= 0 offset (sub1 (vector-length insts)))))
|
||||
(with-handlers ([offset-signal? (λ (os) (+ (offset-signal-val os) offset))])
|
||||
(define proc (vector-ref insts offset))
|
||||
(proc)
|
||||
(add1 offset))))
|
@ -0,0 +1,33 @@
|
||||
#lang reader "main.rkt" ★ ; 8281
|
||||
set b 93
|
||||
set c b
|
||||
jnz a 2
|
||||
jnz 1 5
|
||||
mul b 100
|
||||
sub b -100000
|
||||
set c b
|
||||
sub c -17000
|
||||
set f 1
|
||||
set d 2
|
||||
set e 2
|
||||
set g d
|
||||
mul g e
|
||||
sub g b
|
||||
jnz g 2
|
||||
set f 0
|
||||
sub e -1
|
||||
set g e
|
||||
sub g b
|
||||
jnz g -8
|
||||
sub d -1
|
||||
set g d
|
||||
sub g b
|
||||
jnz g -13
|
||||
jnz f 2
|
||||
sub h -1
|
||||
set g b
|
||||
sub g c
|
||||
jnz g 2
|
||||
jnz 1 3
|
||||
sub b -17
|
||||
jnz 1 -23
|
Reference in New Issue