On this page:
<day05>
5.1 How many strings are “nice”?
<day05-setup>
<day05-q1>
5.2 How many strings are “nice” under new rules?
<day05-q2>
5.3 Testing Day 5
<day05-test>
7.1

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.

5.1 How many strings are “nice”?

A string is “nice” if it meets certain criteria:

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).

(require racket rackunit)
(provide (all-defined-out))

(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:

Again, a test of your regexp-writing skills.

(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

(module+ test
  (define input-str (file->lines "day05-input.txt"))
  (check-equal? (q1 input-str) 238)
  (check-equal? (q2 input-str) 69))