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"));
}
48cfaa659397fd0db0a38bef75f08194f366d&follow=1'>Install up CGI with mode 700June McEnroe It should never be served as a regular file. 2020-07-14Check that there is upload dataJune McEnroe 2020-07-14Add CGI upload program for temp.causal.agencyJune McEnroe 2020-07-10Add The Broken KingdomsJune McEnroe 2020-07-08Add facebook and twitter to title user-agentJune McEnroe This fixes fetching tweets again! https://github.com/thelounge/thelounge/pull/ 3602 (Intentionally breaking the link so GitHub doesn't add a "referenced this PR" thing?) 2020-06-30Tweak causal.agency for mostly no reasonJune McEnroe 2020-06-30Add Ancillary JusticeJune McEnroe 2020-06-26Add password non-manager to planJune McEnroe I've had this idea for years... 2020-06-26Tweak TF2 sensitivities once moreJune McEnroe 2020-06-19Add note about litterbox bot useJune McEnroe 2020-06-19Publish "IRC suite"June McEnroe 2020-06-17Add errors to link.shJune McEnroeubject'>Match URLs inside parens or with paired parens insideJune McEnroe 2020-02-10Duplicate effective URL before passing it back to curlJune McEnroe Apparently sometimes it didn't like receiving its own internal storage to parse again. Understandable. 2020-02-09Add To Be Taught, If FortunateJune McEnroe 2020-02-04Add The Future of Another TimelineJune McEnroe Wow. One of the best I've read. 2020-01-31Reorganize the Makefile for the umpteenth timeJune McEnroe Broke out LDLIBS for each bin, and made everything more uniform. 2020-01-28Change scout sensitivity to 1.4June McEnroe idk it seems to work. 2020-01-28Import shows.txtJune McEnroe