Skip to main content

aoc/year2015/
day11.rs

1//! # Corporate Policy
2//!
3//! Like the [previous day], we rely on the special structure of the input to solve
4//! more efficiently than the general case.
5//!
6//! The key observation is that if there are no straights or pairs in the first 4 digits then the
7//! next lowest valid sequence will end in a string of the form `aabcc`,
8//! compressing 2 pairs and a straight into 5 digits.
9//!
10//! * If the current string ends with `xxyzz` then increment the third digit and wrap around
11//!   to `aabcc`.
12//! * The 5 digit sequence cannot start with any letter from `g` to `o` inclusive or it would
13//!   contain an invalid character somewhere in the sequence.
14//!
15//! [previous day]: crate::year2015::day10
16use std::str::from_utf8;
17
18type Password = [u8; 8];
19type Input = [Password; 2];
20
21pub fn parse(input: &str) -> Input {
22    let password = clean(input.trim().as_bytes().try_into().unwrap());
23
24    // No pairs in the first 4 characters.
25    assert!(!password[..4].array_windows().any(|&[a, b]| a == b));
26    // No straights in the first 4 characters.
27    assert!(!password[..4].array_windows().any(|&[a, b, c]| a + 1 == b && b + 1 == c));
28    // No potential carry in the third character.
29    assert_ne!(password[2], b'z');
30
31    let first = next_password(password);
32    let second = next_password(first);
33    [first, second]
34}
35
36pub fn part1(input: &Input) -> &str {
37    from_utf8(&input[0]).unwrap()
38}
39
40pub fn part2(input: &Input) -> &str {
41    from_utf8(&input[1]).unwrap()
42}
43
44/// Sanitize the input to make sure it has no invalid characters. We increment the first invalid
45/// character found, for example `abcixyz` becomes `abcjaaa`.
46fn clean(mut password: Password) -> Password {
47    if let Some(index) = password.iter().position(|&d| matches!(d, b'i' | b'o' | b'l')) {
48        password[index] += 1;
49        password[index + 1..].fill(b'a');
50    }
51    password
52}
53
54/// Find the next valid 5 digit sequence of form `aabcc`.
55fn next_password(mut password: Password) -> Password {
56    // If the sequence would contain any illegal character, then skip to the next possible
57    // valid sequence starting with `p`.
58    if (b'g'..b'o').contains(&password[3]) {
59        return fill(password, b'p');
60    }
61
62    // If there's room then check if a sequence starting with the current character is
63    // higher than the current password.
64    if password[3] <= b'x' {
65        let candidate = fill(password, password[3]);
66        if candidate > password {
67            return candidate;
68        }
69    }
70
71    // Otherwise, we need to increment the first digit of the sequence.
72    if password[3] == b'x' {
73        // If it starts with `x` then increment the third digit and wrap around.
74        password[2] += if matches!(password[2], b'h' | b'n' | b'k') { 2 } else { 1 };
75        fill(password, b'a')
76    } else if password[3] == b'f' {
77        // If it would enter the invalid range from `g` to `o` then take the next valid start `p`.
78        fill(password, b'p')
79    } else {
80        // Otherwise, increment the first digit.
81        fill(password, password[3] + 1)
82    }
83}
84
85/// Creates a sequence of form `aabcc` from an arbitrary starting character.
86fn fill(mut password: Password, start: u8) -> Password {
87    password[3] = start;
88    password[4] = start;
89    password[5] = start + 1;
90    password[6] = start + 2;
91    password[7] = start + 2;
92    password
93}