2019/src/04.rkt

38 lines
1.2 KiB
Racket
Raw Normal View History

2019-12-04 06:12:31 +00:00
#lang racket
(require "../lib.rkt")
(define-values (start end) (values 235741 706948))
(define (two-adjacent-digits? cs)
(if (< (length cs) 2) #f
(or (eq? (first cs)
(second cs))
(two-adjacent-digits? (cdr cs)))))
(define (digits-never-decrease? cs)
(if (< (length cs) 2) #t
(and (char<=? (first cs)
(second cs))
(digits-never-decrease? (cdr cs)))))
(define (has-group-of-two? cs)
(cond [(< (length cs) 2) #f]
[(= (length cs) 2)
(char=? (first cs) (second cs))]
2019-12-04 06:12:31 +00:00
[else
(or (and (char=? (first cs)
(second cs))
(nchar=? (second cs)
(third cs)))
2019-12-04 06:12:31 +00:00
(has-group-of-two? (dropf cs (λ (c) (eq? c (first cs))))))]))
(define-values (part1 part2)
(let* ([passwords (map (compose string->list number->string) (range start (add1 end)))]
[passwords (filter two-adjacent-digits? passwords)]
[passwords (filter digits-never-decrease? passwords)]
[passwords* (filter has-group-of-two? passwords)])
(values (length passwords)
(length passwords*))))
(show-solution part1 part2)