From 2986bb3110e409a2a338a83166afc692586d6703 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sun, 31 Dec 2017 07:16:12 -0800 Subject: [PATCH] d23 s2 --- 2017/d23/main.rkt | 19 +++++++++++-------- 2017/d23/star2.rkt | 2 ++ 2 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 2017/d23/star2.rkt diff --git a/2017/d23/main.rkt b/2017/d23/main.rkt index 8dec25a..edac345 100644 --- a/2017/d23/main.rkt +++ b/2017/d23/main.rkt @@ -6,7 +6,6 @@ (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)))) @@ -14,14 +13,18 @@ (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-macro (jnz REG VAL) #'(when (not (zero? (value REG))) (raise (value VAL)))) -(define (★ insts) +(define (★ insts [final-key 'mul-count]) (for/fold ([offset 0] - #:result (hash-ref regs 'mul-count)) + #:result (hash-ref regs final-key)) ([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)))) \ No newline at end of file + (with-handlers ([integer? (λ (num) (+ num offset))]) + (define inst (vector-ref insts offset)) + (inst) + (add1 offset)))) + +(define (★★ insts) + (hash-set! regs 'a 1) + (★ insts 'h)) \ No newline at end of file diff --git a/2017/d23/star2.rkt b/2017/d23/star2.rkt new file mode 100644 index 0000000..63f27b5 --- /dev/null +++ b/2017/d23/star2.rkt @@ -0,0 +1,2 @@ +#lang reader "main.rkt" ★★ ; to be fair, this question had nothing to do with Racket +set h 911 \ No newline at end of file