master
Matthew Butterick 7 years ago
parent 9c770514eb
commit 72603adff5

@ -1,22 +1,19 @@
#lang br/quicklang #lang br/quicklang
(require sugar/list) (require "helper.rkt")
(module+ reader (module+ reader (provide read-syntax))
(require syntax/strip-context) (define (read-syntax path port)
(provide read-syntax) (strip-context (with-pattern ([DATUMS (port->datums port)])
(define (read-syntax path port) #'(module d1 aoc-racket/2017/d1
(strip-context #`(module day1 aoc-racket/2017/d1 . DATUMS))))
#,@(for/list ([datum (in-port read port)])
datum)))))
(provide (rename-out [mb #%module-begin])) (provide (rename-out [#%mb #%module-begin]))
(define-macro (mb SELECTOR CAPTCHA-STR ...) (define-macro (#%mb STARS NUMBER ...)
#'(#%module-begin (captcha-sum 'SELECTOR CAPTCHA-STR) ...)) #'(#%module-begin (captcha-sum 'STARS NUMBER) ...))
(define (captcha-sum offset-sig num) (define (captcha-sum stars num)
(define digits (for/list ([c (in-string (number->string num))]) (define digits (number->digits num))
(string->number (string c)))) (define offset (if (eq? stars ') -1 (quotient (length digits) 2)))
(define offset (if (eq? offset-sig ') -1 (/ (length digits) 2)))
(for/sum ([digit (in-list digits)] (for/sum ([digit (in-list digits)]
[other-digit (in-list (shift-cycle digits offset))] [other-digit (in-list (shift-cycle digits offset))]
#:when (= digit other-digit)) #:when (= digit other-digit))

@ -0,0 +1,11 @@
#lang br
(require syntax/strip-context sugar/list)
(provide (all-defined-out) (all-from-out syntax/strip-context sugar/list))
(define (port->datums port)
(for/list ([datum (in-port read port)])
datum))
(define (number->digits num)
(for/list ([c (in-string (number->string num))])
(string->number (string c))))