7.4

### 11Day 11

 (require aoc-racket/day11) package: aoc-racket

The puzzle. Our input is a short alphabetic key that represents a password.



#### 11.1What’s the next password that meets the criteria?

Though the password is alphabetic, we can increment it as we would a numerical password, by changing the rightmost letter to the next letter (for instance x to y, y to z). When we reach z, we roll over to a, and “carry over” the surplus by incrementing the letter to the left.

Furthermore, like Day 5, the puzzle provides certain criteria that must be met:

• The password must have a sequence of three consecutive letters (like bcd).

• The password may not contain i, o, or l.

• The password must contain two different, non-overlapping pairs of letters.

As in Day 5, we’ll use regexp-match to implement tests for these conditions. We’ll also use regexp-replace* to build the function that increments a password alphabetically. Then it’s a simple matter of looking at passwords until we find one that works.

The increment-password function works by using the observation that if the password ends in any number of zs, you have to roll them over to a and increment the letter to the left. Otherwise, you can just increment the last letter — which is actually the same rule, but with zero zs. This logic can all be captured in one regular expression — #rx"^(.*?)(.)(z*)\$".

The three-consecutive-letters? test works by converting the letters to numbers and creating a list of the differences betweeen adjacent values. Any three consecutive letters will differ by value of 1. So if the list of differences contains the subsequence '(1 1), then the string has three consecutive letters.

 (define (q1 input-key) (find-next-valid-password input-key))

#### 11.2What’s the next valid password after that?

We take the answer to question 1 and use it as input to the same function.

 (define (q2 input-key) (find-next-valid-password (q1 input-key)))

#### 11.3Testing Day 11

 (module+ test (define input-key (file->string "day11-input.txt")) (check-equal? (q1 input-key) "hxbxxyzz") (check-equal? (q2 input-key) "hxcaabcc"))