Add solution for "Day 6: Trash Compactor", part 1
This commit is contained in:
@@ -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()));
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<io::BufReader<File>>) {
|
||||
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::<u32>() {
|
||||
Ok(_) => grid.push_row(v.iter().map(|n| n.parse::<u64>().unwrap()).collect()),
|
||||
Err(_) => {
|
||||
for (i, sign) in v.iter().enumerate() {
|
||||
if *sign == "+" {
|
||||
self.part1 += grid.iter_col(i).sum::<u64>();
|
||||
} else {
|
||||
self.part1 += grid.iter_col(i).product::<u64>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TrashCompactor {
|
||||
pub fn new() -> TrashCompactor {
|
||||
TrashCompactor { part1: 0, part2: 0 }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user