1
0

Add solver constants

This commit is contained in:
Stefan Müller
2025-12-09 10:54:02 +01:00
parent b981b0b5fe
commit 77743a16dd
4 changed files with 18 additions and 4 deletions
+6 -1
View File
@@ -5,6 +5,10 @@ use std::io::BufRead;
pub struct Cafeteria {} pub struct Cafeteria {}
impl Cafeteria {
const INTERVAL_SPLIT_CHAR: char = '-';
}
impl Solver for Cafeteria { impl Solver for Cafeteria {
const PUZZLE_INDEX: u8 = 5; const PUZZLE_INDEX: u8 = 5;
const PUZZLE_NAME: &'static str = "Cafeteria"; const PUZZLE_NAME: &'static str = "Cafeteria";
@@ -17,7 +21,8 @@ impl Solver for Cafeteria {
.lines() .lines()
.map_while(|x| x.ok().filter(|s| !s.is_empty())) .map_while(|x| x.ok().filter(|s| !s.is_empty()))
{ {
let values: Vec<u64> = line.split('-').map_while(|s| s.parse().ok()).collect(); // TODO: This code should move into new common::interval::MultiInterval struct.
let values: Vec<u64> = line.split(Self::INTERVAL_SPLIT_CHAR).map_while(|s| s.parse().ok()).collect();
let mut interval = Interval::new(values[0], values[1]); let mut interval = Interval::new(values[0], values[1]);
let mut to_delete = Vec::new(); let mut to_delete = Vec::new();
+2 -1
View File
@@ -8,6 +8,7 @@ pub struct Playground {
} }
impl Playground { impl Playground {
const POINT_SPLIT_CHAR: char = ',';
const CIRCUITS_TAKE: usize = 3; const CIRCUITS_TAKE: usize = 3;
pub fn new(connections: Option<usize>) -> Playground { pub fn new(connections: Option<usize>) -> Playground {
@@ -20,7 +21,7 @@ impl Playground {
fn junction_boxes<R: BufRead>(reader: R) -> Vec<Point3> { fn junction_boxes<R: BufRead>(reader: R) -> Vec<Point3> {
let mut junction_boxes = Vec::new(); let mut junction_boxes = Vec::new();
for line in reader.lines().map_while(Result::ok) { for line in reader.lines().map_while(Result::ok) {
let j: Vec<u64> = line.split(',').map(|s| s.parse().unwrap()).collect(); let j: Vec<u64> = line.split(Self::POINT_SPLIT_CHAR).map(|s| s.parse().unwrap()).collect();
junction_boxes.push(Point3(j[0], j[1], j[2])); junction_boxes.push(Point3(j[0], j[1], j[2]));
} }
junction_boxes junction_boxes
+5 -1
View File
@@ -4,6 +4,10 @@ use std::io::BufRead;
pub struct PrintingDepartment {} pub struct PrintingDepartment {}
impl PrintingDepartment {
const PAPER_ROLL_CHAR: char = '@';
}
impl Solver for PrintingDepartment { impl Solver for PrintingDepartment {
const PUZZLE_INDEX: u8 = 4; const PUZZLE_INDEX: u8 = 4;
const PUZZLE_NAME: &'static str = "Printing Department"; const PUZZLE_NAME: &'static str = "Printing Department";
@@ -11,7 +15,7 @@ impl Solver for PrintingDepartment {
fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64) { fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64) {
let mut grid = Grid::new(0, 0); let mut grid = Grid::new(0, 0);
for line in reader.lines().map_while(Result::ok) { for line in reader.lines().map_while(Result::ok) {
grid.push_row(line.bytes().map(|c| c == b'@').collect()); grid.push_row(line.bytes().map(|c| c == Self::PAPER_ROLL_CHAR as u8).collect());
} }
let mut part1 = 0; let mut part1 = 0;
+5 -1
View File
@@ -4,6 +4,10 @@ use std::io::BufRead;
pub struct TrashCompactor {} pub struct TrashCompactor {}
impl TrashCompactor {
const NUMBER_SPLIT_CHAR: char = ' ';
}
impl Solver for TrashCompactor { impl Solver for TrashCompactor {
const PUZZLE_INDEX: u8 = 6; const PUZZLE_INDEX: u8 = 6;
const PUZZLE_NAME: &'static str = "Trash Compactor"; const PUZZLE_NAME: &'static str = "Trash Compactor";
@@ -12,7 +16,7 @@ impl Solver for TrashCompactor {
let mut part1 = 0; let mut part1 = 0;
let mut grid = Grid::new(0, 0); let mut grid = Grid::new(0, 0);
for line in reader.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(); let v: Vec<&str> = line.split(Self::NUMBER_SPLIT_CHAR).filter(|s| !s.is_empty()).collect();
match v[0].parse::<u32>() { match v[0].parse::<u32>() {
Ok(_) => grid.push_row(v.iter().map(|n| n.parse::<u64>().unwrap()).collect()), Ok(_) => grid.push_row(v.iter().map(|n| n.parse::<u64>().unwrap()).collect()),
Err(_) => { Err(_) => {