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

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

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

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

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

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

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