Advent of Code 2020 solutions in Racket, I guess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.1 KiB

#lang curly-fn racket
(require "lib/common.rkt")
(define (tree? ch)
(char=? #\# ch))
(define (generate-nodes dx dy xmax ymax)
(for/list ([x (in-range 0 +inf.0 dx)]
[y (in-range 0 ymax dy)])
(list (modulo x xmax) y)))
(define ((node-is-tree? in) x y)
(tree? (string-ref (vector-ref in y) x)))
(define ((find in) dx dy)
(count (curry apply (node-is-tree? in))
(generate-nodes dx dy
(string-length (vector-ref in 0))
(vector-length in))))
(define (day3a in)
((find in) 3 1))
(define (day3b in)
(apply * (map (find in) '(1 3 5 7 1) '(1 1 1 1 2))))
(module+ main
(call-with-input-file "data/day3.txt"
(lambda (prt)
(define input (list->vector (port->lines prt)))
(answer 3 1 (day3a input))
(answer 3 2 (day3b input)))))
(module+ test
(require rackunit)
(call-with-input-file "data/day3.txt"
(lambda (prt)
(define input (list->vector (port->lines prt)))
(check-equal? (day3a input) 237
"final answer part 1")
(check-equal? (day3b input) 2106818610
"final answer part 2"))))