day 9 part 1 (not working)
This commit is contained in:
parent
24b88815f2
commit
5bcc40b030
105
2021/src/day9.rs
Normal file
105
2021/src/day9.rs
Normal file
@ -0,0 +1,105 @@
|
||||
use color_eyre::eyre;
|
||||
use eyre::eyre;
|
||||
|
||||
|
||||
// #[derive(Eq, PartialEq)]
|
||||
enum Elevation {
|
||||
High,
|
||||
Mid,
|
||||
Low,
|
||||
}
|
||||
use Elevation::*;
|
||||
|
||||
|
||||
fn load(data: &str) -> eyre::Result<Vec<Vec<u32>>> {
|
||||
let mut heights = Vec::new();
|
||||
for line in data.lines() {
|
||||
let mut row = Vec::new();
|
||||
for c in line.chars() {
|
||||
match c.to_digit(10) {
|
||||
Some(d) => row.push(d),
|
||||
None => return Err(eyre!("Invalid character (not a digit): {}", c)),
|
||||
}
|
||||
}
|
||||
heights.push(row);
|
||||
}
|
||||
Ok(heights)
|
||||
}
|
||||
|
||||
|
||||
fn cmp_horizontal(row: &[u32], pos: usize) -> Elevation {
|
||||
let prev = if pos == 0 {None} else {Some(row[pos - 1])};
|
||||
let next = if pos == row.len() - 1 {None} else {Some(row[pos + 1])};
|
||||
let current = row[pos];
|
||||
|
||||
if current > prev.unwrap_or(0) && current > next.unwrap_or(0) {
|
||||
High
|
||||
}
|
||||
else if current < prev.unwrap_or(9) && current < next.unwrap_or(9) {
|
||||
Low
|
||||
}
|
||||
else {
|
||||
Mid
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn scan(map: &[Vec<u32>]) -> Vec<Vec<Elevation>> {
|
||||
let mut result: Vec<Vec<Elevation>> = Vec::new();
|
||||
for (r, row) in map.iter().enumerate() {
|
||||
let mut scanned_row = Vec::new();
|
||||
for (col, height) in row.iter().enumerate() {
|
||||
let elev = match cmp_horizontal(row, col) {
|
||||
High => {
|
||||
if r == 0 || height > &map[r - 1][col] {
|
||||
High
|
||||
}
|
||||
else {
|
||||
Mid
|
||||
}
|
||||
},
|
||||
Low => {
|
||||
if r == 0 || height < &map[r - 1][col] {
|
||||
Low
|
||||
}
|
||||
else {
|
||||
Mid
|
||||
}
|
||||
},
|
||||
Mid => Mid,
|
||||
};
|
||||
|
||||
if r > 0 {
|
||||
let above = &result[r - 1][col];
|
||||
if let (&High, &High) | (&Low, &Low) = (&elev, above) {
|
||||
result[r - 1][col] = Mid;
|
||||
}
|
||||
}
|
||||
scanned_row.push(elev);
|
||||
}
|
||||
result.push(scanned_row);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
|
||||
fn part1(map: &[Vec<u32>], compares: &[Vec<Elevation>]) -> u32 {
|
||||
let mut result = 0;
|
||||
for (r, row) in compares.iter().enumerate() {
|
||||
for (col, elev) in row.iter().enumerate() {
|
||||
if let Low = elev {
|
||||
result += map[r][col] + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
|
||||
pub fn run(data: &str) -> eyre::Result<(u32, u32)> {
|
||||
let map = load(data)?;
|
||||
let compares = scan(&map);
|
||||
|
||||
let one = part1(&map, &compares);
|
||||
Ok((one, 0))
|
||||
}
|
@ -4,14 +4,14 @@ use color_eyre::eyre;
|
||||
|
||||
mod lib;
|
||||
use lib::load;
|
||||
mod day8;
|
||||
mod day9;
|
||||
|
||||
|
||||
fn main() -> eyre::Result<()> {
|
||||
let data = load("data/08.txt")?;
|
||||
let data = load("data/09.txt")?;
|
||||
|
||||
let start = Instant::now();
|
||||
let (one, two) = day8::run(&data)?;
|
||||
let (one, two) = day9::run(&data)?;
|
||||
let (dur, unit) = format_ns(start.elapsed().as_nanos());
|
||||
|
||||
let precision = 2.0 - dur.log10().floor();
|
||||
|
Loading…
x
Reference in New Issue
Block a user