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/2016/day18/lang.rkt

31 lines
997 B
Racket

8 years ago
#lang br/quicklang ;; http://adventofcode.com/2016/day/18
(provide read-syntax
(rename-out [mb #%module-begin]))
(define (read-syntax path port)
(strip-bindings
#`(module mod "lang.rkt"
#,(string-trim (port->string port)))))
(define-macro (mb STR)
#'(#%module-begin
(define (traps cs)
(length (filter (λ(c) (char=? #\. c)) cs)))
(let loop ([cs (string->list STR)]
[count (traps (string->list STR))]
[i 0])
(if (= i 399999) ; number of rows
count
(let* ([result (next-cs cs)]
[this-count (traps result)])
(loop result (+ this-count count) (add1 i)))))))
(define (next-cs cs)
(define adj-cs (append (list #\.) cs (list #\.)))
(for/list ([c1 (in-list adj-cs)]
[c2 (in-list (cdr adj-cs))]
[c3 (in-list (cddr adj-cs))])
(case (list c1 c2 c3)
[((#\^ #\^ #\.) (#\. #\^ #\^) (#\^ #\. #\.) (#\. #\. #\^)) #\^]
[else #\.])))