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/2016/day12/lang.rkt

42 lines
1.2 KiB
Racket

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