Skip to content

Commit

Permalink
complete day 9 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Lawrence Niu committed Sep 30, 2022
1 parent 35fa11f commit 00b03c1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 138 deletions.
31 changes: 0 additions & 31 deletions src/digit.rs

This file was deleted.

59 changes: 31 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
mod digit;
mod solver;
mod matrix;

use matrix::Matrix;

use std::fs::File;
use std::io::{BufRead, BufReader};
use std::str;

fn merge_digits(digits: &[u8]) -> u16 {
digits
.iter()
.rev()
.copied()
.enumerate()
.map(|(i, d)| 10u16.pow(i as u32) * (d as u16))
.sum()
}

fn process_lines(reader: impl BufRead) -> anyhow::Result<u64> {
let mut sum = 0u64;
for line_res in reader.lines() {
let line = line_res?;
let mut line_io = line.split('|');
let input = line_io.next().expect("Malformed line input").trim();
let output = line_io.next().expect("Malformed line output").trim();
let input_vec: Vec<&str> = input.split_whitespace().collect();
let key = solver::Key::try_from_input(&input_vec[..])?;
let number_vec = output
.split_whitespace()
.map(|digit| key.solve(digit))
const WIDTH: usize = 100;
let mut raw_map: Vec<u8> = Vec::new();
for line_result in reader.lines() {
let line = line_result?;
let mut row = line
.chars()
.map(|height_char| height_char.to_digit(10).map(|h| h as u8))
.collect::<Option<Vec<u8>>>()
.expect("failed to process output digits");
sum += merge_digits(&number_vec[..]) as u64;
.expect("invalid digit encountered");
raw_map.append(&mut row);
}
Ok(sum)
let map = Matrix::try_from_raw(&raw_map[..], WIDTH)?;
let mut risks = 0u64;
let map_height = map.get_height();
for h in 0..map_height {
for w in 0..WIDTH {
let cave_height = map[(w, h)];
let is_north_higher = 0 == h || cave_height < map[(w, h - 1)];
let is_south_higher = map_height == h + 1 || cave_height < map[(w, h + 1)];
let is_west_higher = 0 == w || cave_height < map[(w - 1, h)];
let is_east_higher = WIDTH == w + 1 || cave_height < map[(w + 1, h)];
if is_north_higher && is_south_higher && is_west_higher && is_east_higher {
risks += (cave_height as u64) + 1;
}
}
}

Ok(risks)
}
fn main() {
const INPUT_PATH: &str = "data/input.txt";
Expand All @@ -41,8 +44,8 @@ fn main() {
Err(err) => {
eprintln!("Could not process file {}:\n {}", INPUT_PATH, err);
}
Ok(output) => {
println!("output sum: {}", output);
Ok(risks) => {
println!("total risk: {}", risks);
}
},
Err(err) => {
Expand Down
53 changes: 53 additions & 0 deletions src/matrix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::error;
use std::fmt;
use std::ops;

#[derive(Debug)]
pub struct InvalidMatrix {}

impl fmt::Display for InvalidMatrix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Matrix is invalid")
}
}

impl error::Error for InvalidMatrix {}

pub struct Matrix {
raw: Vec<u8>,
width: usize,
}

impl ops::Index<(usize, usize)> for Matrix {
type Output = u8;

fn index(&self, index: (usize, usize)) -> &Self::Output {
self.get(index)
.unwrap_or_else(|| panic!("matrix index {:#?} out of bounds", index))
}
}

impl Matrix {
pub fn try_from_raw(raw: &[u8], width: usize) -> Result<Self, InvalidMatrix> {
if 0 != raw.len() % width {
return Err(InvalidMatrix {});
}

Ok(Matrix {
raw: Vec::from(raw),
width,
})
}

pub fn get_height(&self) -> usize {
if 0 == self.width {
0
} else {
self.raw.len() / self.width
}
}

pub fn get(&self, index: (usize, usize)) -> Option<&u8> {
self.raw.get(index.0 + index.1 * self.width)
}
}
79 changes: 0 additions & 79 deletions src/solver.rs

This file was deleted.

0 comments on commit 00b03c1

Please sign in to comment.