2023/day05.rs

79 lines
2.1 KiB
Rust
Raw Normal View History

2023-12-06 02:06:34 +00:00
fn parse_map(lines: &mut std::str::Lines<'_>) -> Vec<(u64, u64, u64)> {
let mut map = Vec::new();
loop {
let line = lines.next();
if line.is_none() {
break map;
}
let line = line.unwrap();
if line.is_empty() {
lines.next();
break map;
}
let mut tokens = line
.split_ascii_whitespace()
.map(|n| n.parse::<u64>().unwrap());
let numbers = (
tokens.next().unwrap(),
tokens.next().unwrap(),
tokens.next().unwrap(),
);
map.push(numbers);
}
}
fn transform_ranges(map: &[(u64, u64, u64)], ranges: &[(u64, u64)]) -> Vec<(u64, u64)> {
let mut output_ranges = Vec::new();
for (start, length) in ranges {
let (dst_start, src_start, len) = map
.iter()
2023-12-06 02:11:21 +00:00
.find(|(_dst, src, len)| src <= start && *start < (src + len))
2023-12-06 02:06:34 +00:00
.unwrap_or(&(0u64, 0u64, u64::MAX));
let end = start + length;
let src_end = src_start + len;
2023-12-06 02:08:03 +00:00
output_ranges.push((
dst_start + start - src_start,
u64::min(src_end, end) - start,
));
2023-12-06 02:06:34 +00:00
if end > src_end {
output_ranges.extend(transform_ranges(map, &[(src_end, end - src_end)]));
}
}
output_ranges
}
fn do_the_thing(maps: &Vec<Vec<(u64, u64, u64)>>, mut ranges: Vec<(u64, u64)>) -> u64 {
for map in maps {
2023-12-06 02:11:21 +00:00
ranges = transform_ranges(map, &ranges);
2023-12-06 02:06:34 +00:00
}
ranges.iter().min().unwrap().0
}
2023-12-06 02:11:21 +00:00
fn main() {
2023-12-06 02:06:34 +00:00
let mut lines = include_str!("day05.in").lines();
let line = lines.next().unwrap();
let (_, seeds) = line.trim().split_once(": ").unwrap();
let seeds: Vec<u64> = seeds
.split_ascii_whitespace()
.map(|n| n.parse().unwrap())
.collect();
lines.next();
lines.next();
let maps: Vec<Vec<_>> = (0..7).map(|_| parse_map(&mut lines)).collect();
let part1 = do_the_thing(&maps, seeds.iter().map(|&seed| (seed, 1)).collect());
2023-12-06 02:08:03 +00:00
let part2 = do_the_thing(
&maps,
seeds.chunks(2).map(|chunk| (chunk[0], chunk[1])).collect(),
);
2023-12-06 02:06:34 +00:00
dbg!(part1, part2);
}