7.7
5 Day 5
(require aoc-racket/day05) | package: aoc-racket |
The puzzle. Our input is a list of random-looking but not really random text strings.
<day05> ::=
5.1 How many strings are “nice”?
A string is “nice” if it meets certain criteria:
Contains three vowels (= aeiou).
Contains a double letter.
Does not contain ab, cd, pq, or xy.
This is a job for regexp-match. There’s nothing tricky here (except for remembering that certain matching functions require the pregexp pattern prefix rather than regexp).
<day05-setup> ::=
(require racket rackunit) (provide (all-defined-out))
<day05-q1> ::=
(define (nice? str) (define (three-vowels? str) (>= (length (regexp-match* #rx"[aeiou]" str)) 3)) (define (double-letter? str) (regexp-match #px"(.)\\1" str)) (define (no-kapu? str) (not (regexp-match #rx"ab|cd|pq|xy" str))) (and (three-vowels? str) (double-letter? str) (no-kapu? str))) (define (q1 words) (length (filter nice? words)))
5.2 How many strings are “nice” under new rules?
This time a string is “nice“ if it:
Contains a pair of two letters that appears twice without overlapping
Contains a letter that repeats with at least one letter in between
Again, a test of your regexp-writing skills.
<day05-q2> ::=
(define (nicer? str) (define (nonoverlapping-pair? str) (regexp-match #px"(..).*\\1" str)) (define (separated-repeater? str) (regexp-match #px"(.).\\1" str)) (and (nonoverlapping-pair? str) (separated-repeater? str) #t)) (define (q2 words) (length (filter nicer? words)))
5.3 Testing Day 5
<day05-test> ::=
(module+ test (define input-str (file->lines "day05-input.txt")) (check-equal? (q1 input-str) 238) (check-equal? (q2 input-str) 69))