1
0
Fork 0
This commit is contained in:
Jonathan Chan 2019-12-03 22:12:31 -08:00
parent a588c9b2d1
commit 6ad4c77ce1
1 changed files with 40 additions and 0 deletions

40
src/04.rkt Normal file
View File

@ -0,0 +1,40 @@
#lang racket
(require "../lib.rkt")
(define-values (start end) (values 235741 706948))
(define (neq? b1 b2) (not (eq? b1 b2)))
(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)
(eq? (first cs) (second cs))]
[else
(or (and (eq? (first cs)
(second cs))
(neq? (second cs)
(third cs)))
(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)