-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_04.rs
67 lines (58 loc) · 1.72 KB
/
day_04.rs
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
//! https://adventofcode.com/2017/day/4
//! https://adventofcode.com/2017/day/4/input
use std::{fs::read_to_string, time::Instant};
fn parse(input: &str) -> Vec<Vec<&str>> {
input
.lines()
.map(|line| line.split_whitespace().collect())
.collect()
}
pub mod part1 {
use std::collections::HashSet;
use super::parse;
pub fn solve(input: &str) -> usize {
let passphrases = parse(input);
passphrases
.iter()
.filter(|passphrase| {
passphrase.iter().collect::<HashSet<_>>().len() == passphrase.len()
})
.count()
}
}
pub mod part2 {
use std::collections::HashSet;
use super::parse;
pub fn solve(input: &str) -> usize {
let passphrases = parse(input);
passphrases
.iter()
.filter(|passphrase| {
passphrase
.iter()
.map(|word| {
let mut chars: Vec<_> = word.chars().collect();
chars.sort();
chars
})
.collect::<HashSet<_>>()
.len()
== passphrase.len()
})
.count()
}
}
pub fn main(test: bool) {
let test_input = "a ab abc abd abf abj".to_owned();
let puzzle_input = if test {
test_input
} else {
read_to_string("../inputs/2017/day_04_input.txt").unwrap()
};
let start = Instant::now();
println!("{}", part1::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
let start = Instant::now();
println!("{}", part2::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
}