1
0

Add Solver trait and move common functionality out of concrete solvers

This commit is contained in:
Stefan Müller
2025-12-06 01:27:15 +01:00
parent e8a91164d3
commit 6a7bffa42a
5 changed files with 119 additions and 116 deletions
+19 -56
View File
@@ -1,36 +1,30 @@
use crate::common::interval::Interval;
use crate::solvers::Solver;
use std::collections::BTreeSet;
use std::fs::File;
use std::io;
use std::io::BufRead;
use std::path::Path;
pub struct Cafeteria {
part1: u64,
part2: u64,
}
impl Cafeteria {
pub fn new() -> Cafeteria {
Cafeteria { part1: 0, part2: 0 }
impl Solver for Cafeteria {
fn get_puzzle_index(&self) -> u8 {
5
}
pub fn run(&mut self) {
println!("--- Day 5: Cafeteria ---");
// The "../../data" and "../../../data" paths are useful for running the binary from
// "target/release/" directory with "data/" directory in package root or parent directory.
let paths = vec!["./data", "../data", "../../data", "../../../data"];
match read_data_file("cafeteria.txt", &paths) {
Ok(lines) => {
self.process_data(lines);
self.print_result();
}
Err(error) => eprintln!("{}", error),
};
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>>) {
// Builds intervals collection.
let mut intervals: BTreeSet<Interval> = BTreeSet::new();
@@ -83,41 +77,10 @@ impl Cafeteria {
self.part2 = intervals.iter().map(|x| x.len()).sum();
}
}
fn print_result(&self) {
println!("Part 1: {:?}\nPart 2: {:?}\n", self.part1, self.part2);
impl Cafeteria {
pub fn new() -> Cafeteria {
Cafeteria { part1: 0, part2: 0 }
}
}
fn read_data_file<T>(
filename: T,
search_paths: &Vec<T>,
) -> Result<io::Lines<io::BufReader<File>>, String>
where
T: AsRef<Path>,
{
for path in search_paths {
if let Ok(lines) = read_lines(path.as_ref().join(&filename)) {
return Ok(lines);
}
}
let message = search_paths
.iter()
.map(|path| path.as_ref().join(&filename).display().to_string())
.collect::<Vec<_>>()
.join("\n");
Err(format!(
"Cannot open puzzle input file, searched these paths:\n{}",
message
))
}
// This code is taken from https://doc.rust-lang.org/stable/rust-by-example/std_misc/file/read_lines.html
fn read_lines<T>(filename: T) -> io::Result<io::Lines<io::BufReader<File>>>
where
T: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
+19 -56
View File
@@ -1,35 +1,29 @@
use crate::solvers::Solver;
use grid::*;
use std::fs::File;
use std::io;
use std::io::BufRead;
use std::path::Path;
pub struct PrintingDepartment {
part1: u64,
part2: u64,
}
impl PrintingDepartment {
pub fn new() -> PrintingDepartment {
PrintingDepartment { part1: 0, part2: 0 }
impl Solver for PrintingDepartment {
fn get_puzzle_index(&self) -> u8 {
4
}
pub fn run(&mut self) {
println!("--- Day 4: Printing Department ---");
// The "../../data" and "../../../data" paths are useful for running the binary from
// "target/release/" directory with "data/" directory in package root or parent directory.
let paths = vec!["./data", "../data", "../../data", "../../../data"];
match read_data_file("printing_department.txt", &paths) {
Ok(lines) => {
self.process_data(lines);
self.print_result();
}
Err(error) => eprintln!("{}", error),
};
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>>) {
let mut grid = Grid::new(0, 0);
for line in lines.map_while(Result::ok) {
@@ -62,41 +56,10 @@ impl PrintingDepartment {
}
}
}
}
fn print_result(&self) {
println!("Part 1: {:?}\nPart 2: {:?}\n", self.part1, self.part2);
impl PrintingDepartment {
pub fn new() -> PrintingDepartment {
PrintingDepartment { part1: 0, part2: 0 }
}
}
fn read_data_file<T>(
filename: T,
search_paths: &Vec<T>,
) -> Result<io::Lines<io::BufReader<File>>, String>
where
T: AsRef<Path>,
{
for path in search_paths {
if let Ok(lines) = read_lines(path.as_ref().join(&filename)) {
return Ok(lines);
}
}
let message = search_paths
.iter()
.map(|path| path.as_ref().join(&filename).display().to_string())
.collect::<Vec<_>>()
.join("\n");
Err(format!(
"Cannot open puzzle input file, searched these paths:\n{}",
message
))
}
// This code is taken from https://doc.rust-lang.org/stable/rust-by-example/std_misc/file/read_lines.html
fn read_lines<T>(filename: T) -> io::Result<io::Lines<io::BufReader<File>>>
where
T: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}