summary refs log tree commit diff homepage
path: root/2017/src/bin/day04.rs
blob: 784a2465cb3ac1444d5e7bd84d72ee827f12145a (plain) (blame)
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
use std::collections::HashSet;
use std::io::{self, Read};

fn solve1(input: &str) -> u32 {
    let mut valid = 0;
    for phrase in input.lines() {
        let words = phrase.split_whitespace();
        if words.clone().count() == words.collect::<HashSet<_>>().len() {
            valid += 1;
        }
    }
    valid
}

fn solve2(input: &str) -> u32 {
    let mut valid = 0;
    for phrase in input.lines() {
        let count = phrase.split_whitespace().count();
        let words = phrase.split_whitespace()
            .map(str::chars)
            .map(Iterator::collect::<Vec<_>>)
            .map(|mut v| { v.sort(); v })
            .collect::<HashSet<_>>();
        if count == words.len() {
            valid += 1;
        }
    }
    valid
}

fn main() {
    let mut input = String::new();
    io::stdin().read_to_string(&mut input).unwrap();

    println!("Part 1: {}", solve1(&input));
    println!("Part 2: {}", solve2(&input));
}

#[test]
fn part1() {
    assert_eq!(1, solve1("aa bb cc dd ee"));
    assert_eq!(0, solve1("aa bb cc dd aa"));
    assert_eq!(1, solve1("aa bb cc dd aaa"));
}

#[test]
fn part2() {
    assert_eq!(1, solve2("abcde fghij"));
    assert_eq!(0, solve2("abcde xyz ecdab"));
    assert_eq!(1, solve2("a ab abc abd abf abj"));
    assert_eq!(1, solve2("iiii oiii ooii oooi oooo"));
    assert_eq!(0, solve2("oiii ioii iioi iiio"));
}
6d526daae9129da3b5e&follow=1'>Fix = precedence in whenJune McEnroe