From 05d08c134b0d6b2453f69a8a7c11b6a992dd9e31 Mon Sep 17 00:00:00 2001 From: gabe Date: Sat, 3 Dec 2022 02:26:42 -0600 Subject: [PATCH] refactored day3 to reduce unneeded mem allocations. ran rustfmt on all files. --- src/day02/part1.rs | 1 - src/day02/utilities.rs | 15 +++++++-------- src/day03/part1.rs | 8 ++------ src/day03/part2.rs | 23 ++++++----------------- 4 files changed, 15 insertions(+), 32 deletions(-) diff --git a/src/day02/part1.rs b/src/day02/part1.rs index 9db3aa7..8901bec 100644 --- a/src/day02/part1.rs +++ b/src/day02/part1.rs @@ -24,7 +24,6 @@ pub fn parse(input: &str) -> Vec { .collect() } - #[cfg(test)] mod tests { use super::*; diff --git a/src/day02/utilities.rs b/src/day02/utilities.rs index b85b25e..3ecf4eb 100644 --- a/src/day02/utilities.rs +++ b/src/day02/utilities.rs @@ -11,15 +11,14 @@ pub struct Strategy { pub you: Play, } -pub fn calc_score(input: &Strategy) -> usize{ +pub fn calc_score(input: &Strategy) -> usize { //an enum wins if (you-elf)%3 = 1, looses if it = 2 (match (input.you as i8 - input.elf as i8).rem_euclid(3) { 1 => 6, 2 => 0, 0 => 3, - _ => unreachable!("you were {}, elf was {}", input.you as i8, input.elf as i8) - //play enum has value corresponding to its score. - })+input.you as usize + _ => unreachable!("you were {}, elf was {}", input.you as i8, input.elf as i8), + }) + input.you as usize //play enum has value corresponding to its score. } #[cfg(test)] @@ -28,10 +27,10 @@ mod tests { #[test] fn test_calc_score() { - let input = Strategy { - elf: Play::Scissors, - you: Play::Scissors - }; + let input = Strategy { + elf: Play::Scissors, + you: Play::Scissors, + }; assert_eq!(calc_score(&input), 6); } } diff --git a/src/day03/part1.rs b/src/day03/part1.rs index ed23028..4bffe88 100644 --- a/src/day03/part1.rs +++ b/src/day03/part1.rs @@ -3,19 +3,15 @@ use crate::utilities::*; pub fn part1(input: &[Rucksack]) -> usize { input .iter() - .map(|rucksack| { - let intersection: Vec<&char> = rucksack.0.intersection(&rucksack.1).collect(); - assert!(intersection.len() == 1); - intersection[0] - }) + .map(|rucksack| rucksack.0.intersection(&rucksack.1).next().unwrap()) .map(find_char_score) .sum() } #[cfg(test)] mod tests { - use std::collections::HashSet; use super::*; + use std::collections::HashSet; #[test] fn test_part1() { diff --git a/src/day03/part2.rs b/src/day03/part2.rs index b0bd75b..cf38cf0 100644 --- a/src/day03/part2.rs +++ b/src/day03/part2.rs @@ -13,31 +13,20 @@ pub fn part2(input: &[Rucksack]) -> usize { .iter() .filter(|c| group.1.contains(c)) .filter(|c| group.2.contains(c)) - .map(|x| x.to_owned()) .next() .unwrap() - }).map(|c| find_char_score(&c)).sum() + }) + .map(find_char_score) + .sum() } fn seperate_groups(input: &[Rucksack]) -> Vec { let mut output: Vec = Vec::new(); for group in input.chunks_exact(3) { output.push(Group( - group[0] - .0 - .union(&group[0].1) - .map(|x| x.to_owned()) - .collect(), - group[1] - .0 - .union(&group[1].1) - .map(|x| x.to_owned()) - .collect(), - group[2] - .0 - .union(&group[2].1) - .map(|x| x.to_owned()) - .collect(), + group[0].0.union(&group[0].1).copied().collect(), + group[1].0.union(&group[1].1).copied().collect(), + group[2].0.union(&group[2].1).copied().collect(), )); } output