aoc2020/2.rkt

34 lines
1.1 KiB
Racket
Raw Normal View History

#lang curly-fn racket
2020-12-02 08:50:15 +00:00
(require "scripts/aoc.rkt")
;; solution for day 2
;; checks if the count of the given character in the password is between lmin and lmax
2020-12-02 08:50:15 +00:00
(define (password-valid-part1? lmin lmax char password)
(<= lmin (~>> password string->list (count #{char=? char %})) lmax))
2020-12-02 08:50:15 +00:00
;; checks that exactly one of the positions specified by a and b contain char
2020-12-02 08:50:15 +00:00
(define (password-valid-part2? a b char password)
(define check #{char=? (string-ref password (sub1 %)) char})
(xor (check a) (check b)))
2020-12-02 08:50:15 +00:00
(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)])))
2020-12-02 08:50:15 +00:00
(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"))