From 4f738c22e80c25410d0cfd772280f069fb1504ed Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sat, 17 Dec 2016 23:13:45 -0800 Subject: [PATCH] day18 --- 2016/day18/input.rkt | 2 ++ 2016/day18/lang.rkt | 30 ++++++++++++++++++++++++++++++ 2016/day18/test.rkt | 2 ++ 3 files changed, 34 insertions(+) create mode 100644 2016/day18/input.rkt create mode 100644 2016/day18/lang.rkt create mode 100644 2016/day18/test.rkt diff --git a/2016/day18/input.rkt b/2016/day18/input.rkt new file mode 100644 index 0000000..6c7712f --- /dev/null +++ b/2016/day18/input.rkt @@ -0,0 +1,2 @@ +#lang reader "lang.rkt" +.^^^.^.^^^.^.......^^.^^^^.^^^^..^^^^^.^.^^^..^^.^.^^..^.^..^^...^.^^.^^^...^^.^.^^^..^^^^.....^.... \ No newline at end of file diff --git a/2016/day18/lang.rkt b/2016/day18/lang.rkt new file mode 100644 index 0000000..5715c01 --- /dev/null +++ b/2016/day18/lang.rkt @@ -0,0 +1,30 @@ +#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 #\.]))) diff --git a/2016/day18/test.rkt b/2016/day18/test.rkt new file mode 100644 index 0000000..1411285 --- /dev/null +++ b/2016/day18/test.rkt @@ -0,0 +1,2 @@ +#lang reader "lang.rkt" +.^^.^.^^^^ \ No newline at end of file