Add 9 first days
This commit is contained in:
1
6-custom_customs/.gitignore
vendored
Normal file
1
6-custom_customs/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
23
6-custom_customs/Cargo.lock
generated
Normal file
23
6-custom_customs/Cargo.lock
generated
Normal file
@@ -0,0 +1,23 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "custom_customs"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
10
6-custom_customs/Cargo.toml
Normal file
10
6-custom_customs/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "custom_customs"
|
||||
version = "0.1.0"
|
||||
authors = ["Guilhem MARION <gmarion@netc.fr>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.9.0"
|
||||
2199
6-custom_customs/data/input.txt
Normal file
2199
6-custom_customs/data/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
1
6-custom_customs/element
Normal file
1
6-custom_customs/element
Normal file
@@ -0,0 +1 @@
|
||||
[1;31mE: [0mYou must give at least one search pattern[0m
|
||||
1
6-custom_customs/riot
Normal file
1
6-custom_customs/riot
Normal file
@@ -0,0 +1 @@
|
||||
[1;31mE: [0mYou must give at least one search pattern[0m
|
||||
81
6-custom_customs/src/main.rs
Normal file
81
6-custom_customs/src/main.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
use std::{collections::HashSet, todo};
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
fn count_scores(input: &str) -> Vec<usize> {
|
||||
input
|
||||
.split("\n\n")
|
||||
.map(|l| l.chars()
|
||||
.unique()
|
||||
.filter(|&c| c != '\n')
|
||||
.count())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn count_scores_v2(input: &str) -> Vec<usize> {
|
||||
input
|
||||
.split("\n\n")
|
||||
.map(|group| {
|
||||
group
|
||||
.lines()
|
||||
.map(|l| l.chars().collect::<HashSet<char>>())
|
||||
.fold1(|acc: HashSet<char>, hset| {
|
||||
acc.intersection(&hset).cloned().collect()
|
||||
})
|
||||
.unwrap()
|
||||
.len()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input = include_str!("../data/input.txt");
|
||||
let scores = count_scores(input);
|
||||
println!("Sum of scores : {}", scores.iter().sum::<usize>());
|
||||
println!("Sum of scores v2 : {}", count_scores_v2(input).iter().sum::<usize>())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_count_scores() {
|
||||
let input = r#"abc
|
||||
|
||||
a
|
||||
b
|
||||
c
|
||||
|
||||
ab
|
||||
ac
|
||||
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
|
||||
b"#;
|
||||
assert_eq!(count_scores(input).iter().sum::<usize>(), 11)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_count_scores_v2() {
|
||||
let input = r#"abc
|
||||
|
||||
a
|
||||
b
|
||||
c
|
||||
|
||||
ab
|
||||
ac
|
||||
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
|
||||
b"#;
|
||||
assert_eq!(count_scores_v2(input).iter().sum::<usize>(), 6);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user