mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 18:29:55 +00:00
2023: Day 6, part 2
This commit is contained in:
parent
f202587c42
commit
87c5a2c02c
1 changed files with 26 additions and 4 deletions
|
@ -9,11 +9,11 @@ fn main() -> Result<(), BoxError> {
|
||||||
.ok_or("missing input file path")?;
|
.ok_or("missing input file path")?;
|
||||||
let input = fs::read_to_string(input_path)?;
|
let input = fs::read_to_string(input_path)?;
|
||||||
let mut lines = input.lines();
|
let mut lines = input.lines();
|
||||||
let times = lines.next().ok_or("missing times")?;
|
let times_text = lines.next().ok_or("missing times")?;
|
||||||
let distances = lines.next().ok_or("missing distances")?;
|
let distances_text = lines.next().ok_or("missing distances")?;
|
||||||
|
|
||||||
let times = parse("Time:", times)?;
|
let times = parse("Time:", times_text)?;
|
||||||
let distances = parse("Distance:", distances)?;
|
let distances = parse("Distance:", distances_text)?;
|
||||||
if times.len() != distances.len() {
|
if times.len() != distances.len() {
|
||||||
return Err("time/distance length mismatch".into());
|
return Err("time/distance length mismatch".into());
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,20 @@ fn main() -> Result<(), BoxError> {
|
||||||
|
|
||||||
println!("Part 1: {}", results.iter().fold(1, |a, b| a * b));
|
println!("Part 1: {}", results.iter().fold(1, |a, b| a * b));
|
||||||
|
|
||||||
|
let time = parse_part2("Time:", times_text)?;
|
||||||
|
let record_distance = parse_part2("Distance:", distances_text)?;
|
||||||
|
|
||||||
|
let mut result = 0;
|
||||||
|
(1..time - 1).for_each(|hold| {
|
||||||
|
let velocity = hold;
|
||||||
|
let distance = (time - hold) * velocity;
|
||||||
|
if distance > record_distance {
|
||||||
|
result += 1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
println!("Part 2: {result}");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,3 +66,11 @@ fn parse(prefix: &str, line: &str) -> Result<Vec<usize>, BoxError> {
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
Ok(numbers)
|
Ok(numbers)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_part2(_prefix: &str, line: &str) -> Result<usize, BoxError> {
|
||||||
|
line.chars()
|
||||||
|
.filter(|c| c.is_ascii_digit())
|
||||||
|
.collect::<String>()
|
||||||
|
.parse()
|
||||||
|
.map_err(BoxError::from)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue