refactored day3 to reduce unneeded mem allocations.
ran rustfmt on all files.
This commit is contained in:
		
							parent
							
								
									6c35a97904
								
							
						
					
					
						commit
						05d08c134b
					
				
					 4 changed files with 15 additions and 32 deletions
				
			
		| 
						 | 
				
			
			@ -24,7 +24,6 @@ pub fn parse(input: &str) -> Vec<Strategy> {
 | 
			
		|||
        .collect()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use super::*;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)]
 | 
			
		||||
| 
						 | 
				
			
			@ -30,7 +29,7 @@ mod tests {
 | 
			
		|||
    fn test_calc_score() {
 | 
			
		||||
        let input = Strategy {
 | 
			
		||||
            elf: Play::Scissors,
 | 
			
		||||
                    you: Play::Scissors
 | 
			
		||||
            you: Play::Scissors,
 | 
			
		||||
        };
 | 
			
		||||
        assert_eq!(calc_score(&input), 6);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<Group> {
 | 
			
		||||
    let mut output: Vec<Group> = 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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue