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

45 lines
1.7 KiB
Racket

8 years ago
#lang br/quicklang
;; http://adventofcode.com/2016/day/10
(provide read-syntax)
(define (read-syntax path port)
(strip-bindings
#`(module mod "lang.rkt"
#,@(for/list ([str (in-lines port)]
#:when (not (equal? str "")))
8 years ago
`(handle ,@(string-split str))))))
8 years ago
8 years ago
(define chip-comparison-key '(17 61))
(define-macro (mb . ARGS)
8 years ago
#'(#%module-begin
8 years ago
(begin . ARGS)
(displayln
(for/first ([(k v) (in-hash gates)]
#:when (equal? (sort (apply eval-gate k) <)
(sort chip-comparison-key <)))
k))
(displayln (for/product ([i (in-range 3)])
(gate-low "output" (~a i))))))
8 years ago
(provide (rename-out [mb #%module-begin]))
8 years ago
(define gates (make-hash))
8 years ago
8 years ago
(define-macro-cases handle
[(_ "value" VAL "goes" "to" TYPE NUM)
#'(hash-update! gates (list TYPE NUM) (λ (val) (cons (λ () (let ([v VAL])
(if (string? v)
(string->number v)
v))) val)) empty)]
[(_ "bot" BOT "gives" "low" "to" LOW-TYPE LOW-NUM "and" "high" "to" HIGH-TYPE HIGH-NUM)
#'(begin
(handle "value" (gate-low "bot" BOT) "goes" "to" LOW-TYPE LOW-NUM)
(handle "value" (gate-high "bot" BOT) "goes" "to" HIGH-TYPE HIGH-NUM))])
(provide handle)
8 years ago
8 years ago
(require sugar/cache)
(define/caching (eval-gate type num)
(for/list ([proc (in-list (hash-ref gates (list type num)))])
(proc)))
8 years ago
8 years ago
(define (gate-low type num) (car (sort (eval-gate type num) <)))
(define (gate-high type num) (car (sort (eval-gate type num) >)))