diff --git a/src/04.rkt b/src/04.rkt new file mode 100644 index 0000000..532e672 --- /dev/null +++ b/src/04.rkt @@ -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) \ No newline at end of file