refactored day3 to reduce unneeded mem allocations.
ran rustfmt on all files.
This commit is contained in:
parent
6c35a97904
commit
05d08c134b
|
@ -24,7 +24,6 @@ pub fn parse(input: &str) -> Vec<Strategy> {
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -17,9 +17,8 @@ pub fn calc_score(input: &Strategy) -> usize{
|
||||||
1 => 6,
|
1 => 6,
|
||||||
2 => 0,
|
2 => 0,
|
||||||
0 => 3,
|
0 => 3,
|
||||||
_ => unreachable!("you were {}, elf was {}", input.you as i8, input.elf as i8)
|
_ => 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 //play enum has value corresponding to its score.
|
||||||
})+input.you as usize
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -30,7 +29,7 @@ mod tests {
|
||||||
fn test_calc_score() {
|
fn test_calc_score() {
|
||||||
let input = Strategy {
|
let input = Strategy {
|
||||||
elf: Play::Scissors,
|
elf: Play::Scissors,
|
||||||
you: Play::Scissors
|
you: Play::Scissors,
|
||||||
};
|
};
|
||||||
assert_eq!(calc_score(&input), 6);
|
assert_eq!(calc_score(&input), 6);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,19 +3,15 @@ use crate::utilities::*;
|
||||||
pub fn part1(input: &[Rucksack]) -> usize {
|
pub fn part1(input: &[Rucksack]) -> usize {
|
||||||
input
|
input
|
||||||
.iter()
|
.iter()
|
||||||
.map(|rucksack| {
|
.map(|rucksack| rucksack.0.intersection(&rucksack.1).next().unwrap())
|
||||||
let intersection: Vec<&char> = rucksack.0.intersection(&rucksack.1).collect();
|
|
||||||
assert!(intersection.len() == 1);
|
|
||||||
intersection[0]
|
|
||||||
})
|
|
||||||
.map(find_char_score)
|
.map(find_char_score)
|
||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::collections::HashSet;
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part1() {
|
fn test_part1() {
|
||||||
|
|
|
@ -13,31 +13,20 @@ pub fn part2(input: &[Rucksack]) -> usize {
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|c| group.1.contains(c))
|
.filter(|c| group.1.contains(c))
|
||||||
.filter(|c| group.2.contains(c))
|
.filter(|c| group.2.contains(c))
|
||||||
.map(|x| x.to_owned())
|
|
||||||
.next()
|
.next()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}).map(|c| find_char_score(&c)).sum()
|
})
|
||||||
|
.map(find_char_score)
|
||||||
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn seperate_groups(input: &[Rucksack]) -> Vec<Group> {
|
fn seperate_groups(input: &[Rucksack]) -> Vec<Group> {
|
||||||
let mut output: Vec<Group> = Vec::new();
|
let mut output: Vec<Group> = Vec::new();
|
||||||
for group in input.chunks_exact(3) {
|
for group in input.chunks_exact(3) {
|
||||||
output.push(Group(
|
output.push(Group(
|
||||||
group[0]
|
group[0].0.union(&group[0].1).copied().collect(),
|
||||||
.0
|
group[1].0.union(&group[1].1).copied().collect(),
|
||||||
.union(&group[0].1)
|
group[2].0.union(&group[2].1).copied().collect(),
|
||||||
.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(),
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
output
|
output
|
||||||
|
|
Loading…
Reference in a new issue