aoc2020/2.rkt

32 lines
957 B
Racket
Raw Normal View History

2020-12-02 08:50:15 +00:00
#lang racket
(require "scripts/aoc.rkt")
;; solution for day 2
(define (password-valid-part1? lmin lmax char password)
(<= lmin (count (curry char=? char) (string->list password)) lmax))
(define (password-valid-part2? a b char password)
(define pwd-chars (string->list password))
(xor (char=? (string-ref password (sub1 a)) char)
(char=? (string-ref password (sub1 b)) char)))
(define (calculate valid-proc input)
(for/sum ([line (in-list input)])
(match line
[(pregexp #px"^([0-9]+)\\-([0-9]+) (.): (.*?)$" (list _ lmin lmax char password))
(if (valid-proc (string->number lmin) (string->number lmax)
(string-ref char 0) password) 1 0)]
[_ (error "failed to parse" line)])))
(module+ main
(define input (file->lines "inputs/2"))
;; part 1
(answer 2 1 (calculate password-valid-part1? input))
;; part 2
(answer 2 2 (calculate password-valid-part2? input))
(displayln "meow"))