diff --git a/day1/src/main.rs b/day1/src/main.rs index 713913f..f5ce130 100644 --- a/day1/src/main.rs +++ b/day1/src/main.rs @@ -1,25 +1,22 @@ use std::fs::File; use std::io::{prelude::*, BufReader}; -fn main() -> std::io::Result<()>{ +fn main() -> std::io::Result<()> { let file = File::open("input")?; let reader = BufReader::new(file); - let mut elves = reader.lines() - .fold(Vec::new(), |mut acc, line| { - let line = line.unwrap(); - //empty lines mean new elf - if line.is_empty() || acc.is_empty() { - acc.push(0_u64); - } - + let mut elves = reader.lines().fold(vec![0_u64], |mut acc, line| { + let line = line.unwrap(); + //empty lines mean new elf + if line.is_empty() { + acc.push(0_u64); + } else { // the first time through is an edge case preventing an else here - if ! line.is_empty() { - let last = acc.last_mut().unwrap(); - *last += line.parse::().unwrap(); - } - acc - }); + let last = acc.last_mut().unwrap(); + *last += line.parse::().unwrap(); + } + acc + }); //order the elves since we don't care about position anymore elves.sort(); @@ -29,7 +26,7 @@ fn main() -> std::io::Result<()>{ println!("Part 1: {}", elves[max]); //Part 2 is get the sum of the largest 3 - let counts: u64 = elves[(max-2)..].iter().sum(); + let counts: u64 = elves[(max - 2)..].iter().sum(); println!("Part 2: {counts}"); Ok(()) diff --git a/day2/src/main.rs b/day2/src/main.rs index 6ba7817..c70f211 100644 --- a/day2/src/main.rs +++ b/day2/src/main.rs @@ -2,10 +2,10 @@ use std::fs::File; use std::io::{prelude::*, BufReader}; #[derive(Debug)] -struct HoHoError{} +struct HoHoError {} #[derive(PartialEq)] -enum Choice{ +enum Choice { Rock = 1, Paper, Scissors, @@ -18,7 +18,7 @@ impl std::str::FromStr for Choice { "A" | "X" => Ok(Choice::Rock), "B" | "Y" => Ok(Choice::Paper), "C" | "Z" => Ok(Choice::Scissors), - _ => Err(HoHoError{}), + _ => Err(HoHoError {}), } } } @@ -33,7 +33,7 @@ impl Choice { } 0 } - fn beats (&self) -> Choice { + fn beats(&self) -> Choice { match self { Choice::Rock => Choice::Scissors, Choice::Paper => Choice::Rock, @@ -49,7 +49,7 @@ impl Choice { } } -struct Game{ +struct Game { pub opponent: Choice, pub you: Choice, } @@ -57,7 +57,7 @@ struct Game{ impl std::str::FromStr for Game { type Err = HoHoError; fn from_str(s: &str) -> Result { - let str_split = s.split(" ").collect::>(); + let str_split = s.split(' ').collect::>(); let opponent: Choice = str_split[0].parse()?; // game1 //let you: Choice = str_split[1].parse()?; @@ -65,14 +65,14 @@ impl std::str::FromStr for Game { "X" => opponent.beats(), "Y" => str_split[0].parse()?, "Z" => opponent.loses(), - _ => return Err(HoHoError{}) + _ => return Err(HoHoError {}), }; - Ok(Game{opponent, you}) + Ok(Game { opponent, you }) } } impl Game { - fn outcome (&self) -> i32 { + fn outcome(&self) -> i32 { self.you.cmp(&self.opponent) } @@ -82,7 +82,7 @@ impl Game { } } -fn main() -> std::io::Result<()>{ +fn main() -> std::io::Result<()> { //read in file let file = File::open("input")?; let reader = BufReader::new(file); diff --git a/day3/src/main.rs b/day3/src/main.rs index d02816a..6654da9 100644 --- a/day3/src/main.rs +++ b/day3/src/main.rs @@ -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::(); - 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::(); + 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>, 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, 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::(); + }) + .sum::(); println!("Part 2: {value}"); // find common letter in the groups // find common letters in the first 2 then find the common in the third