2017
Matthew Butterick 7 years ago
parent 196f710363
commit 123ab1927c

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