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/2017/d23/main.rkt

30 lines
1.2 KiB
Racket

7 years ago
#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))
(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)))
7 years ago
(define-macro (jnz REG VAL) #'(when (not (zero? (value REG))) (raise (value VAL))))
7 years ago
7 years ago
(define ( insts [final-key 'mul-count])
7 years ago
(for/fold ([offset 0]
7 years ago
#:result (hash-ref regs final-key))
7 years ago
([i (in-naturals)]
#:break (not (<= 0 offset (sub1 (vector-length insts)))))
7 years ago
(with-handlers ([integer? (λ (num) (+ num offset))])
(define inst (vector-ref insts offset))
(inst)
(add1 offset))))
(define (★★ insts)
(hash-set! regs 'a 1)
( insts 'h))