diff --git a/README.md b/README.md index 2cf5231..17cda32 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ This project does not contain the puzzle or example inputs as per the [copyright :mag_right: Puzzle: , :white_check_mark: Solver: [`Cafeteria`](src/solvers/cafeteria.rs) +### Day 6: Trash Compactor + +:mag_right: Puzzle: , :white_check_mark: Solver: [`Trash Compactor`](src/solvers/trash_compactor.rs) + ## Tests The package contains unit tests for each solver to help troubleshoot issues and prevent regressions. These tests cover the solutions for provided examples and full data inputs. The solutions used within the tests are user-specific. diff --git a/src/main.rs b/src/main.rs index 5aed89f..edfe8fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,4 +7,5 @@ fn main() { solvers::printing_department::PrintingDepartment::new(), )); solvers::run(Box::new(solvers::cafeteria::Cafeteria::new())); + solvers::run(Box::new(solvers::trash_compactor::TrashCompactor::new())); } diff --git a/src/solvers.rs b/src/solvers.rs index a6489a1..003defd 100644 --- a/src/solvers.rs +++ b/src/solvers.rs @@ -5,6 +5,7 @@ use std::path::Path; pub mod cafeteria; pub mod printing_department; +pub mod trash_compactor; pub trait Solver { fn get_puzzle_index(&self) -> u8; diff --git a/src/solvers/trash_compactor.rs b/src/solvers/trash_compactor.rs new file mode 100644 index 0000000..640f85a --- /dev/null +++ b/src/solvers/trash_compactor.rs @@ -0,0 +1,51 @@ +use crate::solvers::Solver; +use grid::*; +use std::fs::File; +use std::io; + +pub struct TrashCompactor { + part1: u64, + part2: u64, +} + +impl Solver for TrashCompactor { + fn get_puzzle_index(&self) -> u8 { + 6 + } + fn get_puzzle_name(&self) -> &str { + "Trash Compactor" + } + fn get_input_filename(&self) -> &str { + "trash_compactor.txt" + } + fn get_part1(&self) -> u64 { + self.part1 + } + fn get_part2(&self) -> u64 { + self.part2 + } + fn process_data(&mut self, lines: io::Lines>) { + let mut grid = Grid::new(0, 0); + for line in lines.map_while(Result::ok) { + let v: Vec<&str> = line.split(' ').filter(|s| !s.is_empty()).collect(); + match v[0].parse::() { + Ok(_) => grid.push_row(v.iter().map(|n| n.parse::().unwrap()).collect()), + Err(_) => { + for (i, sign) in v.iter().enumerate() { + if *sign == "+" { + self.part1 += grid.iter_col(i).sum::(); + } else { + self.part1 += grid.iter_col(i).product::(); + } + } + } + } + } + } +} + +impl TrashCompactor { + pub fn new() -> TrashCompactor { + TrashCompactor { part1: 0, part2: 0 } + } +}