Compare commits

..

1 commit

Author SHA1 Message Date
Tristan Daniël Maat 6921d46db3
Complete day 10 2020-12-10 23:52:10 +00:00

View file

@ -7,7 +7,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Part 1
let result = chain_all(&jolts);
println!("{}", result.0 * result.2);
println!("{}", result.0 * result.1);
// Part 2
let result = count_possible_chains(&jolts)?;
@ -55,11 +55,9 @@ fn count_possible_chains(jolts: &Vec<u64>) -> Result<u64, std::num::TryFromIntEr
.product()
}
fn chain_all(jolts: &Vec<u64>) -> (usize, usize, usize) {
fn chain_all(jolts: &Vec<u64>) -> (usize, usize) {
let mut jolts = jolts.to_vec();
// Add input jolts
jolts.push(0);
// Add output jolts
jolts.sort();
jolts.push(jolts.last().expect("Need some jolts") + 3);
@ -75,10 +73,9 @@ fn chain_all(jolts: &Vec<u64>) -> (usize, usize, usize) {
.collect();
let jump_1s = jolt_jumps.iter().filter(|e| **e == 1).count();
let jump_2s = jolt_jumps.iter().filter(|e| **e == 2).count();
let jump_3s = jolt_jumps.iter().filter(|e| **e == 3).count();
(jump_1s, jump_2s, jump_3s)
(jump_1s, jump_3s)
}
fn parse_jolts(input: &str) -> Result<Vec<u64>, std::num::ParseIntError> {
@ -110,7 +107,7 @@ mod tests {
let jolts = parse_jolts(input)?;
let result = chain_all(&jolts);
assert_eq!(result.0 * result.2, 7 * 5);
assert_eq!(result.0 * result.1, 7 * 5);
Ok(())
}
@ -154,7 +151,7 @@ mod tests {
let jolts = parse_jolts(input)?;
let result = chain_all(&jolts);
assert_eq!(result.0 * result.2, 22 * 10);
assert_eq!(result.0 * result.1, 22 * 10);
Ok(())
}