2016
Matthew Butterick 8 years ago
parent 3ba5818afd
commit 5924352c7a

@ -1,16 +1,16 @@
#lang br/quicklang #lang br/quicklang
(module+ reader
(provide read-syntax) (define (read-syntax path port)
(define (read-syntax path port) (define turn-strings (string-split (port->string port) ","))
(define turn-strings (string-split (port->string port) ",")) (define turn-pattern #px"^([LR])(\\d+)$")
(define turn-pattern #px"^([LR])(\\d+)$") (define turn-datums
(define turn-datums (for*/list ([tstr (in-list turn-strings)])
(for*/list ([tstr (in-list turn-strings)]) (define match-result (regexp-match turn-pattern (string-trim tstr)))
(define match-result (regexp-match turn-pattern (string-trim tstr))) `(turn ,@(cdr (or match-result empty)))))
`(turn ,@(cdr (or match-result empty))))) (strip-bindings
(strip-bindings #`(module day01-mod "day01.rkt"
#`(module day01-mod "day01.rkt" #,@turn-datums)))
#,@turn-datums)))) (module+ reader (provide read-syntax))
(define-macro (mb . TURNS) (define-macro (mb . TURNS)
#'(#%module-begin #'(#%module-begin
@ -48,8 +48,8 @@
(define-macro-cases turn (define-macro-cases turn
[(_ DIR DIST) [(_ DIR DIST)
(with-pattern ([ENCODED-DIR (syntax-case #'DIR () (with-pattern ([ENCODED-DIR (syntax-case #'DIR ()
["L" #'rotate-left] ["L" #'rotate-left]
["R" #'rotate-right])]) ["R" #'rotate-right])])
#'($turn ENCODED-DIR (string->number DIST)))] #'($turn ENCODED-DIR (string->number DIST)))]
[else #'(void)]) [else #'(void)])
(provide turn) (provide turn)

@ -1,14 +1,14 @@
#lang br/quicklang #lang br/quicklang
(module+ reader
(provide read-syntax) (define (read-syntax path port)
(define (read-syntax path port) (define moveset-strs (string-split (port->string port)))
(define moveset-strs (string-split (port->string port))) (define moveset-datums
(define moveset-datums (for*/list ([msstr (in-list moveset-strs)])
(for*/list ([msstr (in-list moveset-strs)]) `(moveset ,@(regexp-match* #rx"." msstr))))
`(moveset ,@(regexp-match* #rx"." msstr)))) (strip-bindings
(strip-bindings #`(module day01-mod "day02.rkt"
#`(module day01-mod "day02.rkt" #,@moveset-datums)))
#,@moveset-datums)))) (module+ reader (provide read-syntax))
(define-macro moveset #'list) (define-macro moveset #'list)
(provide moveset) (provide moveset)

@ -1,14 +1,14 @@
#lang br/quicklang #lang br/quicklang
(module+ reader
(provide read-syntax) (define (read-syntax path port)
(define (read-syntax path port) (define moveset-strs (string-split (port->string port)))
(define moveset-strs (string-split (port->string port))) (define moveset-datums
(define moveset-datums (for*/list ([msstr (in-list moveset-strs)])
(for*/list ([msstr (in-list moveset-strs)]) `(moveset ,@(regexp-match* #rx"." msstr))))
`(moveset ,@(regexp-match* #rx"." msstr)))) (strip-bindings
(strip-bindings #`(module day01-mod "day02b.rkt"
#`(module day01-mod "day02b.rkt" #,@moveset-datums)))
#,@moveset-datums)))) (module+ reader (provide read-syntax))
(define-macro moveset #'list) (define-macro moveset #'list)
(provide moveset) (provide moveset)

@ -13,9 +13,7 @@
(provide (rename-out [mb #%module-begin])) (provide (rename-out [mb #%module-begin]))
(define-macro (triangle A B C) (define-macro (triangle A B C)
#'(list (string->number A) #'(map string->number (list A B C)))
(string->number B)
(string->number C)))
(provide triangle) (provide triangle)
(define (valid-triangle? triangle) (define (valid-triangle? triangle)

@ -1,9 +1,7 @@
#lang br/quicklang #lang br/quicklang
(require sugar/list) (require sugar/list)
(module+ reader (define (read-syntax path port)
(provide read-syntax)
(define (read-syntax path port)
(define triads (slice-at (map string-split (string-split (port->string port) "\n")) 3)) (define triads (slice-at (map string-split (string-split (port->string port) "\n")) 3))
(define new-triples (define new-triples
(slice-at (flatten (for/list ([triad (in-list triads)]) (slice-at (flatten (for/list ([triad (in-list triads)])
@ -11,7 +9,8 @@
(strip-bindings (strip-bindings
#`(module mod "day03b.rkt" #`(module mod "day03b.rkt"
#,@(for*/list ([triple (in-list new-triples)]) #,@(for*/list ([triple (in-list new-triples)])
`(triangle ,@triple)))))) `(triangle ,@triple)))))
(module+ reader (provide read-syntax))
(define-macro (mb . TRIANGLES) (define-macro (mb . TRIANGLES)
#'(#%module-begin #'(#%module-begin
@ -19,9 +18,7 @@
(provide (rename-out [mb #%module-begin])) (provide (rename-out [mb #%module-begin]))
(define-macro (triangle A B C) (define-macro (triangle A B C)
#'(list (string->number A) #'(map string->number (list A B C)))
(string->number B)
(string->number C)))
(provide triangle) (provide triangle)
(define (valid-triangle? triangle) (define (valid-triangle? triangle)

@ -1,12 +1,12 @@
#lang br/quicklang #lang br/quicklang
(module+ reader
(provide read-syntax) (define (read-syntax path port)
(define (read-syntax path port) (strip-bindings
(strip-bindings #`(module mod "day04.rkt"
#`(module mod "day04.rkt" #,@(for*/list ([room-str (in-lines port)]
#,@(for*/list ([room-str (in-lines port)] #:when (not (equal? "" room-str)))
#:when (not (equal? "" room-str))) `(room ,@(cdr (regexp-match #px"^(.*)-(\\d+)\\[(\\w+)\\]$" room-str)))))))
`(room ,@(cdr (regexp-match #px"^(.*)-(\\d+)\\[(\\w+)\\]$" room-str)))))))) (module+ reader (provide read-syntax))
#| #|
Each room consists of an encrypted name (lowercase letters separated by dashes) Each room consists of an encrypted name (lowercase letters separated by dashes)
@ -23,13 +23,13 @@ followed by a dash, a sector ID, and a checksum in square brackets.
(display "part a: ") (display "part a: ")
(displayln (for/sum ([room (in-list rooms)] (displayln (for/sum ([room (in-list rooms)]
#:when (real-room? room)) #:when (real-room? room))
($room-sector room))) ($room-sector room)))
(display "part b: ") (display "part b: ")
(displayln (displayln
(for/first ([room (in-list rooms)] (for/first ([room (in-list rooms)]
#:when (equal? (shift-string ($room-name room) ($room-sector room)) #:when (equal? (shift-string ($room-name room) ($room-sector room))
"northpole object storage")) "northpole object storage"))
($room-sector room))))) ($room-sector room)))))
(provide (rename-out [mb #%module-begin])) (provide (rename-out [mb #%module-begin]))
#| #|
@ -48,8 +48,8 @@ with ties broken by alphabetization.
(define (shift-string str shift) (define (shift-string str shift)
(list->string (list->string
(for/list ([c (in-string str)]) (for/list ([c (in-string str)])
(cond (cond
[(char=? c #\-) #\space] [(char=? c #\-) #\space]
[else [else
(define a-val (char->integer #\a)) (define a-val (char->integer #\a))
(integer->char (+ (modulo (+ (char->integer c) (- a-val) shift) 26) a-val))])))) (integer->char (+ (modulo (+ (char->integer c) (- a-val) shift) 26) a-val))]))))