Day 5 Part 1 complete
This commit is contained in:
@ -1,13 +1,23 @@
|
||||
use super::Solution;
|
||||
use itertools::Itertools;
|
||||
|
||||
pub struct Day05 {}
|
||||
|
||||
impl Solution for Day05 {
|
||||
fn part1(
|
||||
&self,
|
||||
_input: &mut Vec<String>,
|
||||
input: &mut Vec<String>,
|
||||
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
|
||||
Ok(Box::new("Ready"))
|
||||
let (seeds, maps) = self.get_mappings(input)?;
|
||||
let locations = maps.iter().fold(seeds, |seeds, mappings|
|
||||
seeds.iter().map(|&seed|
|
||||
mappings.iter()
|
||||
.find(|&&(_, src, range)| (src..src+range).contains(&seed))
|
||||
.map(|(dst, src, _)| dst + seed - src)
|
||||
.unwrap_or(seed)
|
||||
).collect()
|
||||
);
|
||||
Ok(Box::new(*locations.iter().min().unwrap()))
|
||||
}
|
||||
|
||||
fn part2(
|
||||
@ -22,7 +32,26 @@ impl Solution for Day05 {
|
||||
}
|
||||
}
|
||||
|
||||
impl Day05 {}
|
||||
impl Day05 {
|
||||
fn get_mappings(&self, input: &Vec<String>) -> Result<(Vec<usize>, Vec<Vec<(usize, usize, usize)>>), Box<dyn std::error::Error>> {
|
||||
let seeds: Vec<usize> = input[0].split_whitespace()
|
||||
.skip(1)
|
||||
.map(|s| s.parse().unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let maps: Vec<Vec<(usize, usize, usize)>> = input[2..input.len()].join("\n")
|
||||
.split("\n\n").map(|s|
|
||||
s.split('\n').skip(1).map(|l|
|
||||
l.split_whitespace()
|
||||
.map(|s| s.parse().unwrap())
|
||||
.collect_tuple()
|
||||
.unwrap()
|
||||
).collect::<Vec<_>>()
|
||||
).collect::<Vec<_>>();
|
||||
|
||||
Ok((seeds, maps))
|
||||
}
|
||||
}
|
||||
|
||||
/// Test from puzzle input
|
||||
#[cfg(test)]
|
||||
@ -44,7 +73,7 @@ mod test {
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
assert_eq!(answer, "Ready");
|
||||
assert_eq!(answer, "35");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -18,7 +18,7 @@ impl Solution for Day06 {
|
||||
}
|
||||
|
||||
fn get_day(&self) -> u8 {
|
||||
5
|
||||
6
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user