day 2: 2 shonk 2 furious

This commit is contained in:
xenia 2020-12-02 03:50:15 -05:00
parent b55369a332
commit 76cd3ad58e
1 changed files with 31 additions and 0 deletions

31
2.rkt Normal file
View File

@ -0,0 +1,31 @@
#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"))