This commit is contained in:
TriMill 2022-12-03 00:37:49 -05:00
parent b9db43d7c1
commit 40f684fa6e
2 changed files with 39 additions and 0 deletions

19
examples/day03_1.rs Normal file
View File

@ -0,0 +1,19 @@
use std::collections::HashSet;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let lines: Vec<String> = std::io::stdin().lines().map(|x| x.unwrap()).collect();
let mut result = 0;
for line in lines {
let chars: Vec<char> = line.chars().collect();
let a: HashSet<char> = (chars[0..chars.len()/2]).iter().map(|x| *x).collect();
let b: HashSet<char> = (chars[chars.len()/2..]).iter().map(|x| *x).collect();
let c = *a.intersection(&b).next().unwrap();
result += match c {
'a'..='z' => (c as u32) - ('a' as u32) + 1,
'A'..='Z' => (c as u32) - ('A' as u32) + 27,
_ => todo!(),
};
}
println!("{:?}", result);
Ok(())
}

20
examples/day03_2.rs Normal file
View File

@ -0,0 +1,20 @@
use std::collections::HashSet;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let lines: Vec<String> = std::io::stdin().lines().map(|x| x.unwrap()).collect();
let mut result = 0;
for window in lines.chunks(3) {
let a: HashSet<char> = (window[0]).chars().collect();
let b: HashSet<char> = (window[1]).chars().collect();
let c: HashSet<char> = (window[2]).chars().collect();
let d: HashSet<char> = a.intersection(&b).map(|x| *x).collect();
let mut e = d.intersection(&c);
result += match *e.next().unwrap() {
w @ 'a'..='z' => (w as u32) - ('a' as u32) + 1,
w @ 'A'..='Z' => (w as u32) - ('A' as u32) + 27,
_ => todo!(),
};
}
println!("{:?}", result);
Ok(())
}