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"));
}
amp;follow=1'>Make author consistent and update URLsJune McEnroe 2019-07-12Move to www/text.causal.agencyJune McEnroe 2019-07-12Add new causal.agency with shotty shotsJune McEnroe 2019-07-12Use -s to infer terminal sizeJune McEnroe 2019-07-12Add DCH to shottyJune McEnroe 2019-07-12Support insert mode in shottyJune McEnroe 2019-07-11Don't do carriage return on line feedJune McEnroe 2019-07-11Interpret 256color-style SGRsJune McEnroe 2019-07-11Use inline style rather than <b>, <i>, <u>June McEnroe 2019-07-11Factor out clearJune McEnroe 2019-07-11Add bright option to shottyJune McEnroe 2019-07-11Output <b>, <i>, <u> in shottyJune McEnroe 2019-07-10Ignore SM and RMJune McEnroe 2019-07-09Add shotty man page and build itJune McEnroe 2019-07-09Add up -cJune McEnroe 2019-07-09Add options for default colors to shottyJune McEnroe 2019-07-08Use char literals consistentlyJune McEnroe