Add Solver trait and move common functionality out of concrete solvers
This commit is contained in:
+19
-56
@@ -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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user