Refactor day 9 part 1 in preperation for part 2
parent
bf1614a867
commit
95a984fac4
|
@ -5,3 +5,61 @@ version = 3
|
|||
[[package]]
|
||||
name = "day9"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fb37d2df5df740e582f28f8560cf425f52bb267d872fe58358eadb554909f07a"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.82"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||
|
|
|
@ -6,3 +6,4 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
thiserror = "1.0"
|
||||
|
|
105
day9/src/main.rs
105
day9/src/main.rs
|
@ -2,55 +2,74 @@
|
|||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
enum Error {
|
||||
#[error("Tried to get out of bounds row, {0}")]
|
||||
RowOutOfRange(usize),
|
||||
}
|
||||
|
||||
fn find_low_point_indices(
|
||||
input: &[Vec<u32>],
|
||||
depth: usize,
|
||||
) -> Result<impl Iterator<Item = usize> + '_, Error> {
|
||||
let maybe_row = input.get(depth);
|
||||
if maybe_row.is_none() {
|
||||
return Err(Error::RowOutOfRange(depth));
|
||||
}
|
||||
|
||||
let row = maybe_row.unwrap();
|
||||
let iter = row.iter().enumerate().filter_map(move |(i, &item)| {
|
||||
let adjacent = {
|
||||
let mut res = vec![
|
||||
row.get(i + 1),
|
||||
input.get(depth + 1).map(|below_row| {
|
||||
below_row
|
||||
.get(i)
|
||||
.expect("below row does not match row length")
|
||||
}),
|
||||
];
|
||||
|
||||
if i > 0 {
|
||||
let left = row.get(i - 1);
|
||||
res.push(left);
|
||||
}
|
||||
if depth > 0 {
|
||||
let above = input.get(depth - 1).map(|above_row| {
|
||||
above_row
|
||||
.get(i)
|
||||
.expect("above row does not match row length")
|
||||
});
|
||||
res.push(above);
|
||||
}
|
||||
|
||||
res
|
||||
};
|
||||
|
||||
if adjacent
|
||||
.into_iter()
|
||||
.filter(Option::is_some)
|
||||
.all(|adjacent_item| item < *adjacent_item.unwrap())
|
||||
{
|
||||
Some(i)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
Ok(iter)
|
||||
}
|
||||
|
||||
fn part1(input: &[Vec<u32>]) -> u32 {
|
||||
input
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(depth, row)| {
|
||||
println!("Depth: {}", depth);
|
||||
row.iter()
|
||||
.enumerate()
|
||||
.map(|(i, &item)| {
|
||||
let adjacent = {
|
||||
let mut res = vec![
|
||||
row.get(i + 1),
|
||||
input.get(depth + 1).map(|below_row| {
|
||||
below_row
|
||||
.get(i)
|
||||
.expect("below row does not match row length")
|
||||
}),
|
||||
];
|
||||
|
||||
if i > 0 {
|
||||
let left = row.get(i - 1);
|
||||
res.push(left);
|
||||
}
|
||||
if depth > 0 {
|
||||
let above = input.get(depth - 1).map(|above_row| {
|
||||
above_row
|
||||
.get(i)
|
||||
.expect("above row does not match row length")
|
||||
});
|
||||
res.push(above);
|
||||
}
|
||||
|
||||
res
|
||||
};
|
||||
|
||||
// println!("{}, {}", depth, i);
|
||||
if adjacent
|
||||
.into_iter()
|
||||
.filter(Option::is_some)
|
||||
.all(|adjacent_item| item < *adjacent_item.unwrap())
|
||||
{
|
||||
// println!("{} => 1 + {}", item, height);
|
||||
1 + item
|
||||
} else {
|
||||
0
|
||||
}
|
||||
})
|
||||
.sum()
|
||||
find_low_point_indices(input, depth)
|
||||
.expect("failed to get row for depth analysis")
|
||||
.map(|idx| row[idx] + 1)
|
||||
.sum::<u32>()
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue