diff --git a/2016/day25/input.rkt b/2016/day25/input.rkt new file mode 100644 index 0000000..6788074 --- /dev/null +++ b/2016/day25/input.rkt @@ -0,0 +1,31 @@ +#lang reader "lang.rkt" +cpy a d +cpy 14 c +cpy 182 b +inc d +dec b +jnz b -2 +dec c +jnz c -5 +cpy d a +jnz 0 0 +cpy a b +cpy 0 a +cpy 2 c +jnz b 2 +jnz 1 6 +dec b +dec c +jnz c -4 +inc a +jnz 1 -7 +cpy 2 b +jnz c 2 +jnz 1 4 +dec b +dec c +jnz 1 -4 +jnz 0 0 +out b +jnz a -19 +jnz 1 -21 \ No newline at end of file diff --git a/2016/day25/lang.rkt b/2016/day25/lang.rkt new file mode 100644 index 0000000..403a65d --- /dev/null +++ b/2016/day25/lang.rkt @@ -0,0 +1,48 @@ +#lang br/quicklang ;; http://adventofcode.com/2016/day/25 +(provide read-syntax + (rename-out [mb #%module-begin]) + cpy inc dec jnz out) + +(define (read-syntax path port) + (strip-bindings + #`(module mod "lang.rkt" + #,@(for/list ([str (in-lines port)] + #:when (not (zero? (string-length str)))) + (format-datum '(~a) str))))) + +(define-macro (mb . INSTS) + #'(#%module-begin + (define insts (vector . INSTS)) + (define regs (make-hash '((a . 0)(b . 0)(c . 0)(d . 0)))) + (for*/first ([i (in-naturals)] + #:when + (begin + (hash-set! regs 'a i) + (regexp-match "010101010101" (with-output-to-string (λ () (solve insts regs 50000)))))) + i))) + +(define (solve insts regs max-count) + (let loop ([ptr 0][count 0]) + (if (or (>= ptr (vector-length insts)) (> count max-count)) + regs + (loop (+ ptr (let ([move ((vector-ref insts ptr) regs)]) + (if (void? move) 1 move))) + (add1 count))))) + +(define-macro (cpy X Y) + #'(λ(regs) + (define val (if (number? 'X) 'X (hash-ref regs 'X))) + (hash-set! regs 'Y val))) + +(define-macro (inc X) #'(λ(regs) (hash-update! regs 'X add1))) + +(define-macro (dec X) #'(λ(regs) (hash-update! regs 'X sub1))) + +(define-macro (jnz X Y) + #'(λ(regs) + (when (not (zero? (if (number? 'X) 'X (hash-ref regs 'X)))) + Y))) + +(define-macro (out X) + #'(λ(regs) + (print (hash-ref regs 'X)))) diff --git a/2016/day25/test.rkt b/2016/day25/test.rkt new file mode 100644 index 0000000..de3dce0 --- /dev/null +++ b/2016/day25/test.rkt @@ -0,0 +1,7 @@ +#lang reader "lang.rkt" +cpy 41 a +inc a +inc a +dec a +jnz a 2 +dec a \ No newline at end of file