mirror of
https://github.com/Smaug123/advent-of-code-2017
synced 2025-10-13 07:18:41 +00:00
Move up to day 11 to the lib/main format, and benchmark day 5 (#3)
This commit is contained in:
85
day_4/src/lib.rs
Normal file
85
day_4/src/lib.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
pub mod day_4 {
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::hash::Hash;
|
||||
|
||||
pub fn input() -> Vec<Vec<&'static str>> {
|
||||
let input = include_str!("../input.txt");
|
||||
input
|
||||
.lines()
|
||||
.map(|l| l.split_whitespace().collect())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn contains_duplicate<I, X>(i: &mut I) -> bool
|
||||
where
|
||||
I: Iterator<Item = X>,
|
||||
X: Eq + Hash,
|
||||
{
|
||||
let mut so_far = HashSet::new();
|
||||
for elt in i {
|
||||
if !so_far.insert(elt) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn part_1(input: &[Vec<&str>]) -> usize {
|
||||
input
|
||||
.iter()
|
||||
.filter(|words| !contains_duplicate(&mut words.iter()))
|
||||
.count()
|
||||
}
|
||||
|
||||
pub fn part_2(input: &[Vec<&str>]) -> usize {
|
||||
input
|
||||
.iter()
|
||||
.filter(|words| {
|
||||
!contains_duplicate(&mut words.iter().map(|&w| {
|
||||
let mut w = w.chars().collect::<Vec<char>>();
|
||||
w.sort_unstable();
|
||||
w
|
||||
}))
|
||||
})
|
||||
.count()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::day_4::*;
|
||||
|
||||
#[test]
|
||||
fn part1_known() {
|
||||
assert_eq!(
|
||||
part_1(&vec![
|
||||
vec!["aa", "bb", "cc", "dd", "ee"],
|
||||
vec!["aa", "bb", "cc", "dd", "aa"],
|
||||
vec!["aa", "bb", "cc", "dd", "aaa"]
|
||||
]),
|
||||
2
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_known() {
|
||||
assert_eq!(
|
||||
part_2(&vec![
|
||||
vec!["abcde", "fghij"],
|
||||
vec!["abcde", "xyz", "ecdab"],
|
||||
vec!["a", "ab", "abc", "abd", "abf", "abj"],
|
||||
vec!["iiii", "oiii", "ooii", "oooi", "oooo"],
|
||||
vec!["oiii", "ioii", "iioi", "iiio"]
|
||||
]),
|
||||
3
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_day_4() {
|
||||
let input = input();
|
||||
assert_eq!(part_1(&input), 325);
|
||||
assert_eq!(part_2(&input), 119);
|
||||
}
|
||||
}
|
@@ -1,88 +1,7 @@
|
||||
use std::collections::HashSet;
|
||||
use std::hash::Hash;
|
||||
|
||||
fn input() -> Vec<Vec<&'static str>> {
|
||||
let input = include_str!("../input.txt");
|
||||
input
|
||||
.lines()
|
||||
.map(|l| l.split_whitespace().collect())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn contains_duplicate<I, X>(i: &mut I) -> bool
|
||||
where
|
||||
I: Iterator<Item = X>,
|
||||
X: Eq + Hash,
|
||||
{
|
||||
let mut so_far = HashSet::new();
|
||||
for elt in i {
|
||||
if !so_far.insert(elt) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn part_1(input: &[Vec<&str>]) -> usize {
|
||||
input
|
||||
.iter()
|
||||
.filter(|words| !contains_duplicate(&mut words.iter()))
|
||||
.count()
|
||||
}
|
||||
|
||||
pub fn part_2(input: &[Vec<&str>]) -> usize {
|
||||
input
|
||||
.iter()
|
||||
.filter(|words| {
|
||||
!contains_duplicate(&mut words.iter().map(|&w| {
|
||||
let mut w = w.chars().collect::<Vec<char>>();
|
||||
w.sort_unstable();
|
||||
w
|
||||
}))
|
||||
})
|
||||
.count()
|
||||
}
|
||||
use day_4::day_4;
|
||||
|
||||
fn main() {
|
||||
let input = input();
|
||||
println!("part 1 => {}", part_1(&input));
|
||||
println!("part 2 => {}", part_2(&input));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn part1_known() {
|
||||
assert_eq!(
|
||||
part_1(&vec![
|
||||
vec!["aa", "bb", "cc", "dd", "ee"],
|
||||
vec!["aa", "bb", "cc", "dd", "aa"],
|
||||
vec!["aa", "bb", "cc", "dd", "aaa"]
|
||||
]),
|
||||
2
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_known() {
|
||||
assert_eq!(
|
||||
part_2(&vec![
|
||||
vec!["abcde", "fghij"],
|
||||
vec!["abcde", "xyz", "ecdab"],
|
||||
vec!["a", "ab", "abc", "abd", "abf", "abj"],
|
||||
vec!["iiii", "oiii", "ooii", "oooi", "oooo"],
|
||||
vec!["oiii", "ioii", "iioi", "iiio"]
|
||||
]),
|
||||
3
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_day_4() {
|
||||
let input = input();
|
||||
assert_eq!(part_1(&input), 325);
|
||||
assert_eq!(part_2(&input), 119);
|
||||
}
|
||||
let input = day_4::input();
|
||||
println!("part 1 => {}", day_4::part_1(&input));
|
||||
println!("part 2 => {}", day_4::part_2(&input));
|
||||
}
|
||||
|
Reference in New Issue
Block a user