finish day12

2016
Matthew Butterick 8 years ago
parent 4c514babbe
commit 2139fc459d

@ -1,25 +1,41 @@
#lang br/quicklang #lang br/quicklang
;; http://adventofcode.com/2016/day/12 ;; http://adventofcode.com/2016/day/12
(provide read-syntax
(provide read-syntax) (rename-out [mb #%module-begin])
cpy inc dec jnz)
(define (read-syntax path port) (define (read-syntax path port)
(strip-bindings (strip-bindings
#`(module mod "lang.rkt" #`(module mod "lang.rkt"
#,@(for/list ([str (in-lines port)] #,@(for/list ([str (in-lines port)]
#:when (not (equal? str ""))) #:when (not (zero? (string-length str))))
(format-datum `(handle ,@(map string->symbol (string-split str)))))))) (format-datum '(~a) str)))))
(define (mb . INSTS) (define-macro (mb . INSTS)
#'(#%module-begin #'(#%module-begin
(define insts (list . INSTS)) (define insts (vector . INSTS))
(define regs (make-hash '((a . 0)(b . 0)(c . 0)(d . 0)))) (define regs (make-hash '((a . 0)(b . 0)(c . 0)(d . 0))))
(let loop ([ptr 0]) (println (solve insts regs))
(define inst (list-ref insts ptr)) (hash-set! regs 'c 1)
(inst regs)))) (println (solve insts regs))))
(provide (rename-out [mb #%module-begin]))
(define (solve insts regs)
(let loop ([ptr 0])
(if (>= ptr (vector-length insts))
regs
(loop (+ ptr (let ([move ((vector-ref insts ptr) regs)])
(if (void? move) 1 move)))))))
(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-cases handle (define-macro (jnz X Y)
[(_ cpy X Y) #'(λ(regs) 42)]) #'(λ(regs)
(provide handle) (when (not (zero? (if (number? 'X) 'X (hash-ref regs 'X))))
Y)))