diff --git a/examples/brute_force.rs b/examples/brute_force.rs index 69aabba..a03fc49 100644 --- a/examples/brute_force.rs +++ b/examples/brute_force.rs @@ -42,19 +42,20 @@ fn main() { let mut b = args.problem.get_problem(); - println!("{b}"); + b.print(); match args.mode { Mode::Solutions => { while b.next_finish_state() { - println!("{}\n{}\n{}\n{}", b.count(), b.solution_count(), b.cost(), b); + println!("{}\n{}\n{}", b.count(), b.solution_count(), b.cost()); + b.print(); } println!("Solutions: {}\nStates: {}", b.solution_count(), b.count()); } Mode::Step => { while b.next_state() { - println!("{}", b); + b.print(); let mut s = String::new(); let _ = io::stdin().read_line(&mut s); } diff --git a/examples/solve_belt.rs b/examples/solve_belt.rs index da8aea2..54e71cf 100644 --- a/examples/solve_belt.rs +++ b/examples/solve_belt.rs @@ -1,7 +1,6 @@ -use std::io; - use clap::{Parser, ValueEnum}; use factorio_blueprint::belt_finding::{conflict_avoidance::ConflictAvoidance, problems, Problem}; +use std::io; #[derive(ValueEnum, Clone)] enum Mode { @@ -44,40 +43,40 @@ fn main() { match args.mode { Mode::Solve => { - println!("{}", p); + p.print(); p.find_path(); - println!("{}", p); + p.print(); } Mode::ConflictAvoidance => { - println!("{}", p); + p.print(); p.find_path(); - println!("{}", p); + p.print(); p.find_path(); - println!("{}", p); + p.print(); p.find_path(); - println!("{}", p); + p.print(); p.find_path(); - println!("{}", p); + p.print(); let mut c = ConflictAvoidance::new(p); - println!("{}", c); + c.print(); while c.remove_conflict() { - println!("{}", c) + c.print(); } } Mode::ConflictStep => { - println!("{}", p); + p.print(); p.find_path(); - println!("{}", p); + p.print(); p.find_path(); - println!("{}", p); + p.print(); p.find_path(); - println!("{}", p); + p.print(); p.find_path(); - println!("{}", p); + p.print(); let mut c = ConflictAvoidance::new(p); - println!("{}", c); + c.print(); while c.remove_conflict() { - println!("{}", c); + c.print(); let mut s = String::new(); let _ = io::stdin().read_line(&mut s); } diff --git a/src/belt_finding/brute_force.rs b/src/belt_finding/brute_force.rs index 5476823..66f5a7a 100644 --- a/src/belt_finding/brute_force.rs +++ b/src/belt_finding/brute_force.rs @@ -1,13 +1,9 @@ -use std::fmt::Display; - -use termcolor::ColorSpec; - -use crate::misc::Map; - use super::{ common::{print_map, Dimension, Direction, PathField, PositionType}, Position, COLORS, }; +use crate::misc::Map; +use termcolor::ColorSpec; #[derive(Default, Debug, Clone)] pub struct BruteforceField { @@ -446,9 +442,8 @@ impl Bruteforce { }); if self.is_next_free(&pos, &second_last.dir().clockwise()) { return true; - } else { - return self.modify_remove(); } + return self.modify_remove(); } if second_last.dir().clockwise() == dir { @@ -458,9 +453,8 @@ impl Bruteforce { }); if self.is_next_free(&pos, &second_last.dir().counter_clockwise()) { return true; - } else { - return self.modify_remove(); } + return self.modify_remove(); } if second_last.dir().counter_clockwise() == dir @@ -470,9 +464,8 @@ impl Bruteforce { if self.is_next_free(&p, &d) { return true; - } else { - return self.modify_remove(); } + return self.modify_remove(); } } PathField::Underground { pos, dir, len } => { @@ -481,9 +474,8 @@ impl Bruteforce { if self.is_next_free(&p, &d) { return true; - } else { - return self.modify_remove(); } + return self.modify_remove(); } } } @@ -598,13 +590,7 @@ impl Bruteforce { pub fn solution_count(&self) -> u128 { self.solution_count } -} - -impl Display for Bruteforce { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let width_digits = self.map.width.ilog10() + 1; - let height_digits = self.map.height.ilog10() + 1; - + pub fn print(&self) { let mut m: Map> = Map::new(self.map.width, self.map.height); for (i, problem) in self.problems.iter().enumerate() { @@ -681,8 +667,6 @@ impl Display for Bruteforce { (ColorSpec::new(), " ") } }); - - Ok(()) } } diff --git a/src/belt_finding/conflict_avoidance.rs b/src/belt_finding/conflict_avoidance.rs index b8c75fe..02506b0 100644 --- a/src/belt_finding/conflict_avoidance.rs +++ b/src/belt_finding/conflict_avoidance.rs @@ -1,17 +1,10 @@ -use std::{ - fmt::Display, - ops::{RangeBounds, RangeInclusive}, -}; - -use clap::builder::PathBufValueParser; -use termcolor::ColorSpec; - -use crate::{belt_finding::brute_force::BruteforceBuilder, misc::Map}; - use super::{ - common::{print_map, Dimension, Direction, PathField, Position, PositionType}, + common::{print_map, Direction, PathField, Position, PositionType}, Problem, COLORS, }; +use crate::{belt_finding::brute_force::BruteforceBuilder, misc::Map}; +use std::ops::RangeInclusive; +use termcolor::ColorSpec; #[derive(Default)] struct Field { @@ -274,8 +267,8 @@ impl ConflictAvoidance { let mut b = b.build(); - println!("{}", b); - println!("{}", self); + b.print(); + self.print(); let mut min_cost = f64::INFINITY; let mut solutions = Vec::new(); @@ -315,68 +308,62 @@ impl ConflictAvoidance { } return true; - } else { - let mut candidate = Candidate::new( - Position::new( - xrange.start().saturating_sub(1) as PositionType, - *yrange.start() as PositionType, - ), - Position::new(*xrange.end() as PositionType, *yrange.end() as PositionType), - ); - candidate.extend_range(&conflicts); - if candidate != c && !candidates.iter().any(|c| c == &candidate) { - candidates.push(candidate); - } - let mut candidate = Candidate::new( - Position::new( - *xrange.start() as PositionType, - yrange.start().saturating_sub(1) as PositionType, - ), - Position::new(*xrange.end() as PositionType, *yrange.end() as PositionType), - ); - candidate.extend_range(&conflicts); - if candidate != c && !candidates.iter().any(|c| c == &candidate) { - candidates.push(candidate); - } - let mut candidate = Candidate::new( - Position::new( - *xrange.start() as PositionType, - *yrange.start() as PositionType, - ), - Position::new( - usize::min(xrange.end() + 1, self.map.width - 1) as PositionType, - *yrange.end() as PositionType, - ), - ); - candidate.extend_range(&conflicts); - if candidate != c && !candidates.iter().any(|c| c == &candidate) { - candidates.push(candidate); - } - let mut candidate = Candidate::new( - Position::new( - *xrange.start() as PositionType, - *yrange.start() as PositionType, - ), - Position::new( - *xrange.end() as PositionType, - usize::min(yrange.end() + 1, self.map.height - 1) as PositionType, - ), - ); - candidate.extend_range(&conflicts); - if candidate != c && !candidates.iter().any(|c| c == &candidate) { - candidates.push(candidate); - } } + let mut candidate = Candidate::new( + Position::new( + xrange.start().saturating_sub(1) as PositionType, + *yrange.start() as PositionType, + ), + Position::new(*xrange.end() as PositionType, *yrange.end() as PositionType), + ); + candidate.extend_range(&conflicts); + if candidate != c && !candidates.iter().any(|c| c == &candidate) { + candidates.push(candidate); + } + let mut candidate = Candidate::new( + Position::new( + *xrange.start() as PositionType, + yrange.start().saturating_sub(1) as PositionType, + ), + Position::new(*xrange.end() as PositionType, *yrange.end() as PositionType), + ); + candidate.extend_range(&conflicts); + if candidate != c && !candidates.iter().any(|c| c == &candidate) { + candidates.push(candidate); + } + let mut candidate = Candidate::new( + Position::new( + *xrange.start() as PositionType, + *yrange.start() as PositionType, + ), + Position::new( + usize::min(xrange.end() + 1, self.map.width - 1) as PositionType, + *yrange.end() as PositionType, + ), + ); + candidate.extend_range(&conflicts); + if candidate != c && !candidates.iter().any(|c| c == &candidate) { + candidates.push(candidate); + } + let mut candidate = Candidate::new( + Position::new( + *xrange.start() as PositionType, + *yrange.start() as PositionType, + ), + Position::new( + *xrange.end() as PositionType, + usize::min(yrange.end() + 1, self.map.height - 1) as PositionType, + ), + ); + candidate.extend_range(&conflicts); } } pub fn remove_all_conflicts(&mut self) { while self.remove_conflict() {} } -} -impl Display for ConflictAvoidance { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + pub fn print(&self) { let mut m: Map> = Map::new(self.map.width, self.map.height); for (i, problem) in self.belts.iter().enumerate() { @@ -474,7 +461,5 @@ impl Display for ConflictAvoidance { (color, " ") } }); - - Ok(()) } } diff --git a/src/belt_finding/mod.rs b/src/belt_finding/mod.rs index fdf2157..666fc8b 100644 --- a/src/belt_finding/mod.rs +++ b/src/belt_finding/mod.rs @@ -1,12 +1,9 @@ -use termcolor::{Color, ColorSpec}; - -use crate::belt_finding::{brute_force::BruteforceBuilder, common::Direction}; use crate::graph::wheighted_graph::shortest_path::dijkstra; use crate::graph::wheighted_graph::WheightedGraph; use crate::misc::Map; use crate::priority_queue::BinaryHeap; -use std::fmt::Display; -use std::ops::{Deref, Index}; +use std::ops::Index; +use termcolor::{Color, ColorSpec}; use self::common::{print_map, Position, PositionType}; @@ -88,29 +85,8 @@ impl Problem { self.map.get_mut(p.x as usize, p.y as usize).wheight += 200.0; } } -} -static COLORS: Cyclic = Cyclic([ - Color::Red, - Color::Green, - Color::Yellow, - Color::Blue, - Color::Magenta, - Color::Cyan, -]); - -struct Cyclic([T; N]); - -impl Index for Cyclic { - type Output = T; - - fn index(&self, index: usize) -> &Self::Output { - &self.0[index % N] - } -} - -impl Display for Problem { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + pub fn print(&self) { print_map(self.map.width as i32, self.map.height as i32, |x, y| { let mut color = ColorSpec::new(); if let Some(i) = self @@ -152,8 +128,25 @@ impl Display for Problem { (color, " ") } }); + } +} - Ok(()) +static COLORS: Cyclic = Cyclic([ + Color::Red, + Color::Green, + Color::Yellow, + Color::Blue, + Color::Magenta, + Color::Cyan, +]); + +struct Cyclic([T; N]); + +impl Index for Cyclic { + type Output = T; + + fn index(&self, index: usize) -> &Self::Output { + &self.0[index % N] } } diff --git a/src/lib.rs b/src/lib.rs index 4f59bc1..da96512 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,3 @@ -#![feature(slice_first_last_chunk)] - pub mod belt_finding; pub mod blueprint; pub mod graph;