1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//! # [Day 5: Doesn't He Have Intern-Elves For This?](https://adventofcode.com/2015/day/5)
//! Santa needs help figuring out which strings in his text file are naughty or nice.
//!
//! A nice string is one with all of the following properties:
//!
//! -   It contains at least three vowels (`aeiou` only), like `aei`, `xazegov`, or `aeiouaeiouaeiou`.
//! -   It contains at least one letter that appears twice in a row, like `xx`, `abcdde` (`dd`),
//!     or `aabbccdd` (`aa`, `bb`, `cc`, or `dd`).
//! -   It does not contain the strings `ab`, `cd`, `pq`, or `xy`, even if they are part of one of
//!     the other requirements.
//!
//! For example:
//!
//! -   `ugknbfddgicrmopn` is nice because it has at least three vowels (`u...i...o...`),
//!     a double letter (`...dd...`), and none of the disallowed substrings.
//! -   `aaa` is nice because it has at least three vowels and a double letter, even though
//!     the letters used by different rules overlap.
//! -   `jchzalrnumimnmhp` is naughty because it has no double letter.
//! -   `haegwjzuvuyypxyu` is naughty because it contains the string `xy`.
//! -   `dvszwmarrgswjxmb` is naughty because it contains only one vowel.
//!
//! **How many strings are nice?**
//!
//! # Part Two
//!
//! Realizing the error of his ways, Santa has switched to a better model of determining whether a
//! string is naughty or nice. None of the old rules apply, as they are all clearly ridiculous.
//!
//! Now, a nice string is one with all of the following properties:
//!
//! -   It contains a pair of any two letters that appears at least twice in the string without
//!     overlapping, like `xyxy` (`xy`) or `aabcdefgaa` (`aa`),
//!     but not like `aaa` (`aa`, but it overlaps).
//! -   It contains at least one letter which repeats with exactly one letter between them,
//!     like `xyx`, `abcdefeghi` (`efe`), or even `aaa`.
//!
//! For example:
//!
//! -   `qjhvhtzxzqqjkmpb` is nice because is has a pair that appears twice (`qj`) and a letter
//!     that repeats with exactly one letter between them (`zxz`).
//! -   `xxyxx` is nice because it has a pair that appears twice and a letter that repeats with one
//!     between, even though the letters used by each rule overlap.
//! -   `uurcxstgmygtbstg` is naughty because it has a pair (`tg`) but no repeat with a single
//!     letter between them.
//! -   `ieodomkazucvgmuy` is naughty because it has a repeating letter with one between (`odo`),
//!     but no pair that appears twice.
//!
//! **How many strings are nice under these new rules?**

/// Part 1: How many strings are nice?
#[aoc(day5, part1)]
fn part1(input: &str) -> usize {
    input.lines().filter(|line| is_nice1(line)).count()
}

/// Part 2: How many strings are nice under these new rules?
#[aoc(day5, part2)]
fn part2(input: &str) -> usize {
    input.lines().filter(|line| is_nice2(line)).count()
}

fn is_nice1(input: &str) -> bool {
    has_three_vowels(input) && has_double_letter(input) && !has_invalid(input)
}

fn is_nice2(input: &str) -> bool {
    has_pair_twice_without_overlapping(input) && has_repeat_with_gap(input)
}

fn has_three_vowels(s: &str) -> bool {
    s.chars()
        .filter(|c| *c == 'a' || *c == 'e' || *c == 'i' || *c == 'o' || *c == 'u')
        .count()
        >= 3
}

fn has_invalid(s: &str) -> bool {
    s.contains("ab") || s.contains("cd") || s.contains("pq") || s.contains("xy")
}

fn has_double_letter(s: &str) -> bool {
    let g = unicode_segmentation::UnicodeSegmentation::graphemes(s, true).collect::<Vec<&str>>();
    for i in 1..g.len() {
        if g[i - 1] == g[i] {
            return true;
        }
    }
    false
}

fn has_pair_twice_without_overlapping(input: &str) -> bool {
    let g = unicode_segmentation::UnicodeSegmentation::graphemes(input, true);
    let v = g.collect::<Vec<&str>>();
    for idx1 in 0..v.len() - 3 {
        for idx2 in (idx1 + 2)..v.len() - 1 {
            let first: String = v[idx1..idx1 + 2].iter().map(|s| s.to_string()).collect();
            let second: String = v[idx2..idx2 + 2].iter().map(|s| s.to_string()).collect();
            if first.eq(&second) {
                return true;
            }
        }
    }
    false
}

fn has_repeat_with_gap(input: &str) -> bool {
    let g =
        unicode_segmentation::UnicodeSegmentation::graphemes(input, true).collect::<Vec<&str>>();
    for idx in 1..g.len() - 2 {
        if g[idx] == g[idx + 2] {
            return true;
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn part1_examples() {
        // `ugknbfddgicrmopn` is nice because it has at least three vowels (`u...i...o...`),
        //     a double letter (`...dd...`), and none of the disallowed substrings.
        assert_eq!(is_nice1("ugknbfddgicrmopn"), true);

        // `aaa` is nice because it has at least three vowels and a double letter, even though
        //     the letters used by different rules overlap.
        assert_eq!(is_nice1("aaa"), true);

        // `jchzalrnumimnmhp` is naughty because it has no double letter.
        assert_eq!(is_nice1("jchzalrnumimnmhp"), false);

        // `haegwjzuvuyypxyu` is naughty because it contains the string `xy`.
        assert_eq!(is_nice1("haegwjzuvuyypxyu"), false);

        // `dvszwmarrgswjxmb` is naughty because it contains only one vowel.
        assert_eq!(is_nice1("dvszwmarrgswjxmb"), false);
    }

    #[test]
    fn part2_examples() {
        // `qjhvhtzxzqqjkmpb` is nice because is has a pair that appears twice (`qj`) and a letter
        // that repeats with exactly one letter between them (`zxz`).
        assert_eq!(is_nice2("qjhvhtzxzqqjkmpb"), true, "a");

        // `xxyxx` is nice because it has a pair that appears twice and a letter that repeats with one
        // between, even though the letters used by each rule overlap.
        assert_eq!(is_nice2("xxyxx"), true, "b");

        // `uurcxstgmygtbstg` is naughty because it has a pair (`tg`) but no repeat with a single
        //  letter between them.
        assert_eq!(is_nice2("uurcxstgmygtbstg"), false, "c");

        // `ieodomkazucvgmuy` is naughty because it has a repeating letter with one between (`odo`),
        //     but no pair that appears twice.
        assert_eq!(is_nice2("ieodomkazucvgmuy"), false, "f");
    }

    #[test]
    fn test_emojis() {
        assert_eq!(has_double_letter("🐂🐂"), true);
    }
}