summaryrefslogtreecommitdiff
path: root/2017/src/bin/day_4.rs
blob: a9a098aefa15b8f6e7f9a5356448d1bc4b048e1b (plain)
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
extern crate advent_of_code_2017;
use advent_of_code_2017::*;

fn main() {
    let args = AdventArgs::init();

    let valid_count = args.input.iter()
        .map(|line| {
            let words = line.split_whitespace().map(|x| x.to_string()).collect::<Vec<String>>();
            if args.part == 1 {
                let mut deduped_words = words.clone();
                deduped_words.sort();
                deduped_words.dedup();
                words.len() == deduped_words.len()
            } else {
                !words.iter().enumerate().any(|(i, word1)| {
                    words.iter().enumerate().any(|(j, word2)| {
                        i != j && is_anagram(word1, word2)
                    })
                })
            }
        })
        .filter(|&valid| valid)
        .count();

    println!("Valid count: {}", valid_count);
}

fn is_anagram(word1: &str, word2: &str) -> bool {
    let mut chars1 = word1.chars().collect::<Vec<_>>();
    chars1.sort();
    let mut chars2 = word2.chars().collect::<Vec<_>>();
    chars2.sort();

    chars1.len() == chars2.len() &&
        chars1.iter().zip(chars2.iter()).all(|(c1, c2)| c1 == c2)
}