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/2020/02.rkt

21 lines
899 B
Racket

#lang br
(require racket/file rackunit)
(struct rec (password target low high))
(define recs (for/list ([ln (file->lines "02.rktd")])
(match-define (list range target password) (string-split ln))
(match-define (list low high) (map string->number (string-split range "-")))
(rec password (string-ref (string-trim target ":") 0) low high)))
(check-equal?
(for/sum ([rec recs])
(define howmany (count (λ (c) (char=? c (rec-target rec))) (string->list (rec-password rec))))
(if (<= (rec-low rec) howmany (rec-high rec)) 1 0))
422)
(check-equal?
(for/sum ([rec recs])
(define low? (char=? (rec-target rec) (string-ref (rec-password rec) (sub1 (rec-low rec)))))
(define high? (char=? (rec-target rec) (string-ref (rec-password rec) (sub1 (rec-high rec)))))
(if (or (and low? (not high?)) (and high? (not low?))) 1 0))
451)