2022-12-29 — 2 min read

Advent of Code 2015, Day 11 – In Ruby

After a bit of a pause, let’s get back to the puzzles with the 11th day of 2015!

The first half of the puzzle

Santa’s previous password expired, and he needs help choosing a new one.

To help him remember his new password after the old one expires, Santa has devised a method of coming up with a password based on the previous one. Corporate policy dictates that passwords must be exactly eight lowercase letters (for security reasons), so he finds his new password by incrementing his old password string repeatedly until it is valid.

Incrementing is just like counting with numbers: xx, xy, xz, ya, yb, and so on. Increase the rightmost letter one step; if it was z, it wraps around to a, and repeat with the next letter to the left until one doesn’t wrap around.

Unfortunately for Santa, a new Security-Elf recently started, and he has imposed some additional password requirements:

  • Passwords must include one increasing straight of at least three letters, like abc, bcd, cde, and so on, up to xyz. They cannot skip letters; abd doesn’t count.
  • Passwords may not contain the letters i, o, or l, as these letters can be mistaken for other characters and are therefore confusing.
  • Passwords must contain at least two different, non-overlapping pairs of letters, like aa, bb, or zz.

For example:

  • hijklmmn meets the first requirement (because it contains the straight hij) but fails the second requirement requirement (because it contains i and l).
  • abbceffg meets the third requirement (because it repeats bb and ff) but fails the first requirement.
  • abbcegjk fails the third requirement, because it only has one double letter (bb).
  • The next password after abcdefgh is abcdffaa.
  • The next password after ghijklmn is ghjaabcc, because you eventually skip all the passwords that start with ghi…, since i is not allowed.

Given Santa’s current password (your puzzle input), what should his next password be?

The solution:

#!/usr/bin/env ruby

input = +ARGV.first
straights = Regexp.union ('a'..'z').to_a.each_cons(3).map(&:join)

loop do
  input.succ!
  break if input[straights] && input !~ /[iol]/ && input.scan(/(.)\1/).uniq.size > 1
end

puts "The next password is: #{input}"

First, we read our input from the command line. This time, we’ll need to modify it during the execution, hence the use of the unary plus operator: it turns the by-default frozen input string to an unfrozen one. Next, we create a regexp for the first requirement, let’s see it step-by-step:

Then, we generate the next password (String#succ! does just what we need) until all of the requirements are satisfied. We have to use this roundabout way because Ruby doesn’t have proper do while loops: it’d be more elegant to write

input.succ! until input[straights] && input !~ /[iol]/ && input.scan(/(.)\1/).uniq.size > 1

but that’d still evaluate the condition before executing input.succ!.

The second half of the puzzle

Santa’s password expired again. What’s the next one?

The solution is the same as above, we simply change the command-line parameter to the previous password.

The code with my input text is available in the GitHub repo.

Thanks for reading! If you have any comments, additions, or corrections, feel free to reach me via e-mail.

Copyright © 2023 csm.hu
Contact