Skip to content

Commit

Permalink
finished day 5 part 2
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Läufer <[email protected]>
  • Loading branch information
klaeufer committed Mar 19, 2024
1 parent bff8be6 commit 25cd706
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions src/bin/day5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ static NUMBER: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\d+)").unwrap());
fn main() -> Result<()> {
env_logger::init();

let x1 = process_part_1(lines_from_file("data/day5example.txt").unwrap().by_ref());
println!("Day 5 part 1 example: {:?}", x1);

let s1 = process_part_1(lines_from_file("data/day5input.txt").unwrap().by_ref());
println!("Day 5 part 1 solution: {:?}", s1);
process("Day 5 example", lines_from_file("data/day5example.txt").unwrap().by_ref());
process("Day 5 solution", lines_from_file("data/day5input.txt").unwrap().by_ref());

Ok(())
}
Expand All @@ -38,9 +35,11 @@ fn make_vec(lines: &mut impl Iterator<Item = String>) -> Vec<usize> {
}

fn make_map(lines: &mut impl Iterator<Item = String>) -> Option<Box<dyn Fn(usize) -> usize>> {
let header = lines.next()?; // Skip section header, assuming it's always there.
// skip section header, assuming it's always there
let header = lines.next()?;
info!("section header: {:?}", header);

// collect ranges (triplets) for this section
let ranges = lines
.take_while(|line| !line.trim().is_empty())
.map(|line| {
Expand All @@ -52,9 +51,9 @@ fn make_map(lines: &mut impl Iterator<Item = String>) -> Option<Box<dyn Fn(usize
(numbers[0], numbers[1], numbers[2])
})
.collect::<Vec<(usize, usize, usize)>>();

info!("ranges: {:?}", ranges);

// represent as a function
Some(Box::new(move |i| {
ranges
.iter()
Expand All @@ -63,35 +62,40 @@ fn make_map(lines: &mut impl Iterator<Item = String>) -> Option<Box<dyn Fn(usize
}))
}

fn process_part_1(input: &mut impl Iterator<Item = String>) -> usize {
info!("process_part_1: start");
fn process(label: &str, input: &mut impl Iterator<Item = String>) {
info!("process: start");

let seeds = make_vec(input);
info!("seeds: {:?}", seeds);
let all_maps = std::iter::successors(make_map(input), |_| make_map(input))

// create all maps and collect them into a vector
let all_maps =
std::iter::successors(make_map(input), |_| make_map(input))
.collect::<Vec<_>>();
info!("all_maps: {:?}", all_maps.len());

// compose all maps into a single function
let seed_to_location = all_maps
.into_iter()
.rev()
.reduce(|f, g| Box::new(move |x| f(g(x)))).unwrap();
seeds.iter().map(|&x| seed_to_location(x)).min().unwrap()
}

/*
def processPart1(input: Iterator[String]) =
val seeds = makeSeq(input)
val allMaps = Iterator.continually(makeMap(input)).takeWhile(_.nonEmpty)
val seedToLocation = allMaps.map(_.get).toSeq.reverse.reduce(_.compose(_))
seeds.map(seedToLocation).min
def processPart2(input: Iterator[String]) =
val seeds = makeSeq(input)
val allMaps = Iterator.continually(makeMap(input)).takeWhile(_.nonEmpty)
val seedToLocation = allMaps.map(_.get).toSeq.reverse.reduce(_.compose(_))
seeds.sliding(2, 2).map: p =>
(p.head until p.head + p.last)
// .tapEach(println)
.map(seedToLocation).min
.tapEach(println)
.min
*/
// part 1: find the minimum location for the given seeds
let part1 = seeds
.iter()
.map(|&x| seed_to_location(x))
.min().unwrap();
println!("{} part 1: {}", label, part1);

// part 2: find the minimum location for the given seeds interpreted as ranges
let part2 = seeds
.chunks(2)
.map(|chunk| {
(chunk[0]..chunk[0] + chunk[1])
.map(|x| seed_to_location(x))
.min()
.unwrap()
})
.min().unwrap();
println!("{} part 2: {}", label, part2);
}

0 comments on commit 25cd706

Please sign in to comment.