clean up day 2 solution with fancy stuff
This commit is contained in:
parent
c278b60801
commit
f54047466a
20
2.rkt
20
2.rkt
|
@ -1,24 +1,26 @@
|
||||||
#lang racket
|
#lang curly-fn racket
|
||||||
|
|
||||||
(require "scripts/aoc.rkt")
|
(require "scripts/aoc.rkt")
|
||||||
|
|
||||||
;; solution for day 2
|
;; 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)
|
(define (password-valid-part1? lmin lmax char password)
|
||||||
(<= lmin (count (curry char=? char) (string->list password)) lmax))
|
(<= 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 (password-valid-part2? a b char password)
|
||||||
(define pwd-chars (string->list password))
|
(define check #{char=? (string-ref password (sub1 %)) char})
|
||||||
(xor (char=? (string-ref password (sub1 a)) char)
|
(xor (check a) (check b)))
|
||||||
(char=? (string-ref password (sub1 b)) char)))
|
|
||||||
|
|
||||||
(define (calculate valid-proc input)
|
(define (calculate valid-proc input)
|
||||||
(for/sum ([line (in-list input)])
|
(for/sum ([line (in-list input)])
|
||||||
(match line
|
(match line
|
||||||
[(pregexp #px"^([0-9]+)\\-([0-9]+) (.): (.*?)$" (list _ lmin lmax char password))
|
[(pregexp #px"^([0-9]+)\\-([0-9]+) (.): (.*?)$"
|
||||||
(if (valid-proc (string->number lmin) (string->number lmax)
|
(list _ (app string->number lmin) (app string->number lmax)
|
||||||
(string-ref char 0) password) 1 0)]
|
(app #{string-ref % 0} char) password))
|
||||||
[_ (error "failed to parse" line)])))
|
(if (valid-proc lmin lmax char password) 1 0)]
|
||||||
|
[_ (error "not shonks 🦈📉" line)])))
|
||||||
|
|
||||||
(module+ main
|
(module+ main
|
||||||
(define input (file->lines "inputs/2"))
|
(define input (file->lines "inputs/2"))
|
||||||
|
|
Loading…
Reference in New Issue