some formatting and some optimizations

This commit is contained in:
Dylan "smellyfis" Thies
2022-12-12 16:03:42 -05:00
parent 4ef68d5f36
commit b85cc13465
3 changed files with 47 additions and 47 deletions

View File

@@ -7,24 +7,25 @@ fn main() -> std::io::Result<()> {
let reader = BufReader::new(file);
/*
let value = reader
.lines()
.map(|line| {
let line = line.unwrap();
let (comp1, comp2) = line.split_at(line.len() / 2);
let duplicate = comp2.chars().find(|c| comp1.contains(*c)).unwrap();
match duplicate {
n @'a'..='z' => (n as i32) - ('a' as i32) + 1_i32,
n @ 'A'..='Z' => (n as i32) - ('A' as i32) + 27_i32,
_ => 0,
}
})
.sum::<i32>();
println!("Part 1: {value}");
*/
let value = reader
.lines()
.map(|line| {
let line = line.unwrap();
let (comp1, comp2) = line.split_at(line.len() / 2);
let duplicate = comp2.chars().find(|c| comp1.contains(*c)).unwrap();
match duplicate {
n @'a'..='z' => (n as i32) - ('a' as i32) + 1_i32,
n @ 'A'..='Z' => (n as i32) - ('A' as i32) + 27_i32,
_ => 0,
}
})
.sum::<i32>();
println!("Part 1: {value}");
*/
//part 2
// fold the lines into groups of three
let value = reader.lines()
let value = reader
.lines()
.fold(Vec::new(), |mut acc: Vec<Vec<String>>, line| {
if acc.is_empty() || acc.last().unwrap().len() == 3 {
acc.push(Vec::new())
@@ -38,7 +39,8 @@ fn main() -> std::io::Result<()> {
[g1, g2, g3] => (g1, g2, g3),
_ => panic!("not get here"),
};
match g1.chars()
match g1
.chars()
.fold(Vec::new(), |mut combo: Vec<char>, ch| {
if g2.contains(ch) {
combo.push(ch)
@@ -46,14 +48,15 @@ fn main() -> std::io::Result<()> {
combo
})
.iter()
.find(|c| {
g3.contains(**c)
}).unwrap(){
.find(|c| g3.contains(**c))
.unwrap()
{
n @ 'a'..='z' => (*n as i32) - ('a' as i32) + 1_i32,
n @ 'A'..='Z' => (*n as i32) - ('A' as i32) + 27_i32,
_ => 0,
}
}).sum::<i32>();
})
.sum::<i32>();
println!("Part 2: {value}");
// find common letter in the groups
// find common letters in the first 2 then find the common in the third