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

@@ -5,16 +5,13 @@ 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 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.is_empty() {
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::<u64>().unwrap();
}

View File

@@ -57,7 +57,7 @@ struct Game{
impl std::str::FromStr for Game {
type Err = HoHoError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let str_split = s.split(" ").collect::<Vec<&str>>();
let str_split = s.split(' ').collect::<Vec<&str>>();
let opponent: Choice = str_split[0].parse()?;
// game1
//let you: Choice = str_split[1].parse()?;
@@ -65,7 +65,7 @@ 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 })
}

View File

@@ -24,7 +24,8 @@ fn main() -> std::io::Result<()> {
*/
//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