day5: problem 2 solution. parallelized!

This commit is contained in:
Keenan Tims
2023-12-06 17:24:24 -08:00
parent 0665e109af
commit 077f4ff8ee
3 changed files with 164 additions and 3 deletions

View File

@ -2,6 +2,9 @@ use std::fs::File;
use std::io::{BufRead, BufReader, Lines};
use std::ops::Range;
use itertools::Itertools;
use rayon::prelude::*;
// --- Day 5: If You Give A Seed A Fertilizer ---
// You take the boat and find the gardener right where you were told he would be:
@ -278,7 +281,12 @@ impl Almanac {
// What is the lowest location number that corresponds to any of the initial seed numbers?
fn problem1_lowest_location(almanac: &Almanac) -> u64 {
almanac.seeds.iter().map(|seed| almanac.lookup(*seed)).min().unwrap()
almanac
.seeds
.iter()
.map(|seed| almanac.lookup(*seed))
.min()
.unwrap()
}
fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
@ -287,15 +295,60 @@ fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
}
// PROBLEM 2 solution
// Everyone will starve if you only plant such a small number of seeds. Re-reading the
// almanac, it looks like the seeds: line actually describes ranges of seed numbers.
// The values on the initial seeds: line come in pairs. Within each pair, the first
// value is the start of the range and the second value is the length of the range. So,
// in the first line of the example above:
// seeds: 79 14 55 13
// This line describes two ranges of seed numbers to be planted in the garden. The first
// range starts with seed number 79 and contains 14 values: 79, 80, ..., 91, 92. The
// second range starts with seed number 55 and contains 13 values: 55, 56, ..., 66, 67.
// Now, rather than considering four seed numbers, you need to consider a total of 27
// seed numbers.
// In the above example, the lowest location number can be obtained from seed number 82,
// which corresponds to soil 84, fertilizer 84, water 84, light 77, temperature 45,
// humidity 46, and location 46. So, the lowest location number is 46.
// Consider all of the initial seed numbers listed in the ranges on the first line of
// the almanac. What is the lowest location number that corresponds to any of the
// initial seed numbers?
fn problem2_lowest_location(almanac: &Almanac) -> u64 {
let seed_ranges = almanac
.seeds
.iter()
.tuples()
.map(|(range_start, length)| *range_start..*range_start + *length);
seed_ranges
.map(|range| {
range
.into_par_iter()
.map(|x| almanac.lookup(x))
.min()
.unwrap()
})
.min()
.unwrap()
}
fn problem2<T: BufRead>(input: Lines<T>) -> u64 {
0
let almanac = Almanac::from(input);
problem2_lowest_location(&almanac)
}
#[cfg(test)]
mod tests {
use std::io::{BufRead, Cursor};
use crate::{Almanac, AlmanacMapping, problem1_lowest_location};
use crate::*;
const EXAMPLE_ALMANAC: &str = &"seeds: 79 14 55 13
@ -359,4 +412,11 @@ humidity-to-location map:
let a = Almanac::from(c.lines());
assert_eq!(problem1_lowest_location(&a), 35);
}
#[test]
fn test_problem2_example() {
let c = Cursor::new(EXAMPLE_ALMANAC);
let a = Almanac::from(c.lines());
assert_eq!(problem2_lowest_location(&a), 46);
}
}