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/2017/d9/main.rkt

28 lines
950 B
Racket

#lang br/quicklang
(require "../helper.rkt")
(provide read-syntax (rename-out [#%mb #%module-begin]))
(define (read-syntax path port)
(strip-context #`(module mod "main.rkt"
#,@(port->lines port))))
(define-macro (#%mb STARS-LINE SEXP-LINE ...)
#`(#%module-begin
(if (eq? (process-line STARS-LINE) ')
(score (process-line SEXP-LINE))
(process-line SEXP-LINE #t))
...))
(define (process-line line [garbage #f])
(define gchars 0)
(let* ([line (string-trim line)]
[line (regexp-replace* #rx"!." line "")]
[line (regexp-replace* #rx"<.*?>" line
(λ (m) (set! gchars (+ gchars (string-length m) -2)) ""))]
[line (regexp-replace* #rx"," line "")])
(if garbage gchars (read (open-input-string line)))))
(define (score tree [start 0])
(+ (add1 start) (for/sum ([x (in-list tree)])
(score x (add1 start)))))