improve performance of 2016 day 1 part 2 and format fixes

This commit is contained in:
2023-11-23 09:22:23 +01:00
parent ef042f87db
commit 663375d8f8
4 changed files with 32 additions and 20 deletions

View File

@ -16,10 +16,16 @@ pub fn part2(input: &str) -> impl std::fmt::Display {
#[test]
fn test_part1() {
assert_eq!("24000", part1("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string())
assert_eq!(
"24000",
part1("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string()
)
}
#[test]
fn test_part2() {
assert_eq!("45000", part2("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string())
}
assert_eq!(
"45000",
part2("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string()
)
}

View File

@ -1,22 +1,22 @@
enum Move {
Rock = 1,
Paper = 2,
Scissors = 3
Scissors = 3,
}
enum GameResult {
Loss = 0,
Draw = 3,
Win = 6
Win = 6,
}
impl From<&str> for Move {
fn from(value: &str) -> Self {
match value {
"A" | "X" => Self::Rock,
"B" | "Y" => Self::Paper,
"C" | "Z" => Self::Scissors,
_ => unreachable!()
"A" | "X" => Self::Rock,
"B" | "Y" => Self::Paper,
"C" | "Z" => Self::Scissors,
_ => unreachable!(),
}
}
}
@ -27,18 +27,21 @@ impl From<&str> for GameResult {
"X" => Self::Loss,
"Y" => Self::Draw,
"Z" => Self::Win,
_ => unreachable!()
_ => unreachable!(),
}
}
}
fn calc(input: &str, calc: fn(&str, &str) -> u32) -> u32 {
input.lines().map(|l| {
let Some((l, r)) = l.split_once(' ') else {
unreachable!()
};
(calc)(l, r)
}).sum()
input
.lines()
.map(|l| {
let Some((l, r)) = l.split_once(' ') else {
unreachable!()
};
(calc)(l, r)
})
.sum()
}
pub fn part1(input: &str) -> impl std::fmt::Display {
@ -89,4 +92,4 @@ fn test_part1() {
#[test]
fn test_part2() {
assert_eq!("12", part2("A Y\nB X\nC Z").to_string())
}
}