1
0

Refactor Solver trait to streamline usage

This commit is contained in:
Stefan Müller
2025-12-08 14:38:02 +01:00
parent ad64b05db7
commit 496293b33b
5 changed files with 63 additions and 123 deletions
+12 -33
View File
@@ -1,35 +1,19 @@
use crate::common::interval::Interval;
use crate::solvers::Solver;
use std::collections::BTreeSet;
use std::fs::File;
use std::io;
use std::io::BufRead;
pub struct Cafeteria {
part1: u64,
part2: u64,
}
pub struct Cafeteria {}
impl Solver for Cafeteria {
fn get_puzzle_index(&self) -> u8 {
5
}
fn get_puzzle_name(&self) -> &str {
"Cafeteria"
}
fn get_input_filename(&self) -> &str {
"cafeteria.txt"
}
fn get_part1(&self) -> u64 {
self.part1
}
fn get_part2(&self) -> u64 {
self.part2
}
fn process_data(&mut self, mut lines: io::Lines<io::BufReader<File>>) {
const PUZZLE_INDEX: u8 = 5;
const PUZZLE_NAME: &'static str = "Cafeteria";
const INPUT_FILENAME: &'static str = "cafeteria.txt";
fn process_data<R: BufRead>(&self, mut reader: R) -> (u64, u64) {
// Builds intervals collection.
let mut intervals: BTreeSet<Interval> = BTreeSet::new();
for line in lines
.by_ref()
for line in reader.by_ref().lines()
.map_while(|x| x.ok().filter(|s| !s.is_empty()))
{
let values: Vec<u64> = line.split('-').map_while(|s| s.parse().ok()).collect();
@@ -68,19 +52,14 @@ impl Solver for Cafeteria {
}
}
let mut part1 = 0;
// Tests values against intervals.
for value in lines.map_while(|x| x.ok()?.parse::<u64>().ok()) {
for value in reader.lines().map_while(|x| x.ok()?.parse::<u64>().ok()) {
if intervals.iter().any(|interval| interval.contains(value)) {
self.part1 += 1;
part1 += 1;
}
}
self.part2 = intervals.iter().map(|x| x.len()).sum();
}
}
impl Cafeteria {
pub fn new() -> Cafeteria {
Cafeteria { part1: 0, part2: 0 }
(part1, intervals.iter().map(|x| x.len()).sum())
}
}
+11 -30
View File
@@ -1,35 +1,21 @@
use crate::solvers::Solver;
use grid::*;
use std::fs::File;
use std::io;
use std::io::BufRead;
pub struct PrintingDepartment {
part1: u64,
part2: u64,
}
pub struct PrintingDepartment {}
impl Solver for PrintingDepartment {
fn get_puzzle_index(&self) -> u8 {
4
}
fn get_puzzle_name(&self) -> &str {
"Printing Department"
}
fn get_input_filename(&self) -> &str {
"printing_department.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>>) {
const PUZZLE_INDEX: u8 = 4;
const PUZZLE_NAME: &'static str = "Printing Department";
const INPUT_FILENAME: &'static str = "printing_department.txt";
fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64) {
let mut grid = Grid::new(0, 0);
for line in lines.map_while(Result::ok) {
for line in reader.lines().map_while(Result::ok) {
grid.push_row(line.bytes().map(|c| c == b'@').collect());
}
let mut part1 = 0;
'cells: for cell in grid.indexed_iter() {
if *cell.1 {
let mut count = 0;
@@ -52,14 +38,9 @@ impl Solver for PrintingDepartment {
}
}
}
self.part1 += 1;
part1 += 1;
}
}
}
}
impl PrintingDepartment {
pub fn new() -> PrintingDepartment {
PrintingDepartment { part1: 0, part2: 0 }
(part1, 0)
}
}
+12 -31
View File
@@ -1,51 +1,32 @@
use crate::solvers::Solver;
use grid::*;
use std::fs::File;
use std::io;
use std::io::BufRead;
pub struct TrashCompactor {
part1: u64,
part2: u64,
}
pub struct TrashCompactor {}
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>>) {
const PUZZLE_INDEX: u8 = 6;
const PUZZLE_NAME: &'static str = "Trash Compactor";
const INPUT_FILENAME: &'static str = "trash_compactor.txt";
fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64) {
let mut part1 = 0;
let mut grid = Grid::new(0, 0);
for line in lines.map_while(Result::ok) {
for line in reader.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>();
part1 += grid.iter_col(i).sum::<u64>();
} else {
self.part1 += grid.iter_col(i).product::<u64>();
part1 += grid.iter_col(i).product::<u64>();
}
}
}
}
}
}
}
impl TrashCompactor {
pub fn new() -> TrashCompactor {
TrashCompactor { part1: 0, part2: 0 }
(part1, 0)
}
}