--- Day 11: Corporate Policy ---

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?

Rust Solution

use rustgym_util::*;
use std::fmt::Write;
use std::io::*;

pub fn solve(reader: &mut dyn BufRead, writer: &mut dyn Write) {
    let mut it = reader.lines().map(|l| l.unwrap());
    let line = it.next().unwrap();
    let res1: String = next_pass(&line);
    let res2: String = next_pass(&res1);
    writeln!(writer, "{}", res1).unwrap();
    writeln!(writer, "{}", res2).unwrap();
}

fn next_pass(password: &str) -> String {
    let val = decode(password);
    let mut x = val;
    loop {
        x += 1;
        let s = encode(x);
        if rule1(&s) && rule2(&s) && rule3(&s) {
            break;
        }
    }
    encode(x).into_iter().map(|b| b as char).collect()
}

fn decode(s: &str) -> i64 {
    let mut res = 0;
    for b in s.bytes() {
        res *= 26;
        res += (b - b'a') as i64;
    }
    res
}

fn encode(mut x: i64) -> Vec<u8> {
    let mut s: Vec<u8> = vec![];
    for _ in 0..8 {
        let c = b'a' + (x % 26) as u8;
        s.push(c);
        x /= 26;
    }
    s.into_iter().rev().collect()
}

fn rule1(s: &[u8]) -> bool {
    s.windows(3).any(|w| w[0] + 1 == w[1] && w[1] + 1 == w[2])
}

fn rule2(s: &[u8]) -> bool {
    !s.iter().any(|&c| c == b'i' || c == b'o' || c == b'l')
}

fn rule3(s: &[u8]) -> bool {
    let mut pairs: Vec<usize> = vec![];
    for i in 1..8 {
        if s[i - 1] == s[i] {
            pairs.push(i);
        }
    }
    pairs.len() > 1 && pairs.last().unwrap() - pairs.first().unwrap() > 1
}

advent_of_code_ignore!(test, "input.txt", "output.txt");

Having problems with this solution? Click here to submit an issue on github.