aoc2020/2.rkt

34 lines
1.1 KiB
Racket

#lang curly-fn racket
(require "scripts/aoc.rkt")
;; solution for day 2
;; checks if the count of the given character in the password is between lmin and lmax
(define (password-valid-part1? lmin lmax char password)
(<= lmin (~>> password string->list (count #{char=? char %})) lmax))
;; checks that exactly one of the positions specified by a and b contain char
(define (password-valid-part2? a b char password)
(define check #{char=? (string-ref password (sub1 %)) char})
(xor (check a) (check b)))
(define (calculate valid-proc input)
(for/sum ([line (in-list input)])
(match line
[(pregexp #px"^([0-9]+)\\-([0-9]+) (.): (.*?)$"
(list _ (app string->number lmin) (app string->number lmax)
(app #{string-ref % 0} char) password))
(if (valid-proc lmin lmax char password) 1 0)]
[_ (error "not shonks 🦈📉" 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"))