You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.3 KiB
Racket
81 lines
2.3 KiB
Racket
9 years ago
|
#lang scribble/lp2
|
||
|
@(require scribble/manual aoc-racket/helper)
|
||
|
|
||
|
@aoc-title[5]
|
||
|
|
||
9 years ago
|
@defmodule[aoc-racket/day05]
|
||
9 years ago
|
|
||
9 years ago
|
@link["http://adventofcode.com/day/5"]{The puzzle}. Our @link-rp["day05-input.txt"]{input} is a list of random-looking but not really random text strings.
|
||
9 years ago
|
|
||
9 years ago
|
@chunk[<day05>
|
||
|
<day05-setup>
|
||
|
<day05-q1>
|
||
|
<day05-q2>
|
||
|
<day05-test>]
|
||
9 years ago
|
|
||
|
@section{How many strings are ``nice''?}
|
||
|
|
||
|
A string is ``nice'' if it meets certain criteria:
|
||
|
|
||
|
@itemlist[
|
||
|
@item{Contains three vowels (= @litchar{aeiou}).}
|
||
|
@item{Contains a double letter.}
|
||
|
@item{Does not contain @litchar{ab}, @litchar{cd}, @litchar{pq}, or @litchar{xy}.}
|
||
|
]
|
||
|
|
||
|
This is a job for @racket[regexp-match]. There's nothing tricky here (except for remembering that certain matching functions require the @racket[pregexp] pattern prefix rather than @racket[regexp]).
|
||
|
|
||
|
|
||
9 years ago
|
@chunk[<day05-setup>
|
||
9 years ago
|
(require racket rackunit)
|
||
9 years ago
|
(provide (all-defined-out))
|
||
9 years ago
|
]
|
||
|
|
||
9 years ago
|
@chunk[<day05-q1>
|
||
9 years ago
|
(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)))
|
||
|
|
||
|
]
|
||
|
|
||
|
@section{How many strings are ``nice'' under new rules?}
|
||
|
|
||
|
This time a string is ``nice`` if it:
|
||
|
|
||
|
@itemlist[
|
||
|
@item{Contains a pair of two letters that appears twice without overlapping}
|
||
|
@item{Contains a letter that repeats with at least one letter in between}
|
||
|
]
|
||
|
|
||
|
Again, a test of your regexp-writing skills.
|
||
|
|
||
9 years ago
|
@chunk[<day05-q2>
|
||
9 years ago
|
(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)))]
|
||
|
|
||
9 years ago
|
@section{Testing Day 5}
|
||
9 years ago
|
|
||
9 years ago
|
@chunk[<day05-test>
|
||
9 years ago
|
(module+ test
|
||
9 years ago
|
(define input-str (file->lines "day05-input.txt"))
|
||
9 years ago
|
(check-equal? (q1 input-str) 238)
|
||
|
(check-equal? (q2 input-str) 69))]
|