diff --git a/Cargo.lock b/Cargo.lock index 9ff464e..eadc986 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -456,9 +456,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.3" +version = "1.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" +checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" dependencies = [ "aho-corasick", "memchr", diff --git a/examples/priority_queue_test.rs b/examples/priority_queue_test.rs index a8a739b..6a11667 100644 --- a/examples/priority_queue_test.rs +++ b/examples/priority_queue_test.rs @@ -13,7 +13,7 @@ where loop { input.clear(); - std::io::stdin().read_line(&mut input); + let _ = std::io::stdin().read_line(&mut input); let (cmd, arg) = input.trim().split_once(' ').unwrap_or((input.trim(), "")); // dbg!(cmd, arg); diff --git a/examples/solve_belt.rs b/examples/solve_belt.rs index 48cedc9..8313d50 100644 --- a/examples/solve_belt.rs +++ b/examples/solve_belt.rs @@ -11,7 +11,7 @@ enum Mode { #[derive(ValueEnum, Clone)] enum ProblemCase { - simple, + Simple, Level1, Level2, Level3, @@ -21,7 +21,7 @@ enum ProblemCase { impl ProblemCase { fn get_problem(&self) -> Problem { match self { - ProblemCase::simple => problems::simple(), + ProblemCase::Simple => problems::simple(), ProblemCase::Level1 => problems::belt_madness_level_1(), ProblemCase::Level2 => problems::belt_madness_level_2(), ProblemCase::Level3 => problems::belt_madness_level_3(), diff --git a/src/belt_finding/brute_force.rs b/src/belt_finding/brute_force.rs index 91db44b..d0ecb19 100644 --- a/src/belt_finding/brute_force.rs +++ b/src/belt_finding/brute_force.rs @@ -1,5 +1,5 @@ use super::{ - common::{print_map, Dimension, Direction, PathField, PositionType}, + common::{print_map, Direction, PathField, PositionType}, Position, COLORS, }; use crate::misc::Map; @@ -66,10 +66,6 @@ impl BruteforceBuilder { pub fn build(self) -> Bruteforce { // dbg!(&self); - let dimension = Dimension { - width: self.map.width, - height: self.map.height, - }; let mut b = Bruteforce { map: self.map, @@ -77,7 +73,6 @@ impl BruteforceBuilder { pointer_stack: vec![0], solution_count: 0, count: 0, - dimension, }; for [start, end] in self.path { @@ -111,7 +106,6 @@ struct Problem { #[derive(Clone)] pub struct Bruteforce { map: Map, - dimension: Dimension, problems: Vec, pointer_stack: Vec, solution_count: u128, @@ -654,7 +648,7 @@ impl Bruteforce { // Print body - print_map(self.map.width as i32, self.map.height as i32, |x, y| { + let _ = print_map(self.map.width as i32, self.map.height as i32, |x, y| { if let Some((i, c)) = m.get(x as usize, y as usize) { let mut color = ColorSpec::new(); color.set_fg(Some(COLORS[*i])); diff --git a/src/belt_finding/common.rs b/src/belt_finding/common.rs index 5233802..fde0ded 100644 --- a/src/belt_finding/common.rs +++ b/src/belt_finding/common.rs @@ -1,7 +1,6 @@ use core::panic; -use std::io::Write; +use std::io::{self, Write}; -use base64::write; use termcolor::{ColorSpec, StandardStream, WriteColor}; pub type PositionType = i32; @@ -203,7 +202,7 @@ impl PathField { } } -pub fn print_map(width: i32, height: i32, f: F) +pub fn print_map(width: i32, height: i32, f: F) -> io::Result<()> where F: Fn(i32, i32) -> (ColorSpec, &'static str), { @@ -218,35 +217,35 @@ where //padding for _ in 0..height_digits { - write!(stdout, " "); + write!(stdout, " ")?; } for x in 0..width { let digits = x / (i32::pow(10, d)); if digits == 0 && d > 0 { - write!(stdout, " "); + write!(stdout, " ")?; } else { write!( stdout, "{}", char::from_u32((digits % 10) as u32 + 48).unwrap() - ); + )?; } } - writeln!(stdout); + writeln!(stdout)?; } for y in 0..height { - write!(stdout, "{:1$}", y, height_digits as usize); + write!(stdout, "{:1$}", y, height_digits as usize)?; for x in 0..width { let (c, s) = f(x, y); - stdout.set_color(&c); - write!(stdout, "{:1}", s); - stdout.reset(); + stdout.set_color(&c)?; + write!(stdout, "{:1}", s)?; + stdout.reset()?; } - writeln!(stdout, "{:1$}", y, height_digits as usize); + writeln!(stdout, "{:1$}", y, height_digits as usize)?; } for i in 0..width_digits { @@ -254,21 +253,23 @@ where //padding for _ in 0..height_digits { - write!(stdout, " "); + write!(stdout, " ")?; } for x in 0..width { let digits = x / (i32::pow(10, d)); if digits == 0 && d > 0 { - write!(stdout, " "); + write!(stdout, " ")?; } else { write!( stdout, "{}", char::from_u32((digits % 10) as u32 + 48).unwrap() - ); + )?; } } - writeln!(stdout); + writeln!(stdout)?; } + + Ok(()) } diff --git a/src/belt_finding/conflict_avoidance.rs b/src/belt_finding/conflict_avoidance.rs index 3e6f796..717643e 100644 --- a/src/belt_finding/conflict_avoidance.rs +++ b/src/belt_finding/conflict_avoidance.rs @@ -76,7 +76,7 @@ impl ConflictAvoidance { } let mut belts = Vec::new(); for i in 0..problem.path.len() { - dbg!(&problem.path[i]); + // dbg!(&problem.path[i]); let mut p = Vec::new(); p.push(PathField::Belt { @@ -87,7 +87,7 @@ impl ConflictAvoidance { for (pos, dir) in &problem.path[i][1..] { let start = p.last().unwrap().end_pos(); let start = start.0.in_direction(&start.1, 1); - dbg!(start, pos); + // dbg!(start, pos); if &start == pos { p.push(PathField::Belt { pos: *pos, @@ -97,7 +97,10 @@ impl ConflictAvoidance { p.push(PathField::Underground { pos: start, dir: *dir, - len: u8::max((start.x - pos.x).abs() as u8, (start.y - pos.y).abs() as u8), + len: u8::max( + (start.x - pos.x).unsigned_abs() as u8, + (start.y - pos.y).unsigned_abs() as u8, + ), }) } } @@ -111,7 +114,6 @@ impl ConflictAvoidance { // pos: problem.end[i].0, // dir: problem.end[i].1, // }); - dbg!(&p); belts.push(p); } @@ -157,7 +159,7 @@ impl ConflictAvoidance { let mut mapping = Vec::new(); let offset = Position::new(*xrange.start() as i32 - 1, *yrange.start() as i32 - 1); - dbg!(&xrange, &yrange); + // dbg!(&xrange, &yrange); for (i, path) in self.belts.iter().enumerate() { // index of first PathField where the next position is in the area @@ -181,8 +183,6 @@ impl ConflictAvoidance { }) .map(|rev_i| path.len() - rev_i - 1); - dbg!(start_index, end_index); - if let Some((start_index, end_index)) = Option::zip(start_index, end_index) { // dbg!(start_index, end_index, path[start_index], path[end_index]); @@ -268,7 +268,7 @@ impl ConflictAvoidance { Some( mapping .into_iter() - .zip(solutions.into_iter()) + .zip(solutions) .map(|((path_id, start, end), path)| BruteForceEntry { path_id, start, @@ -305,16 +305,16 @@ impl ConflictAvoidance { } } - for y in 0..self.map.height { - for x in 0..self.map.width { - if *conflicts.get(x, y) > 1 { - print!("#"); - } else { - print!(" "); - } - } - println!(); - } + // for y in 0..self.map.height { + // for x in 0..self.map.width { + // if *conflicts.get(x, y) > 1 { + // print!("#"); + // } else { + // print!(" "); + // } + // } + // println!(); + // } let mut candidates = Vec::new(); @@ -506,7 +506,7 @@ impl ConflictAvoidance { } // Print body - print_map(self.map.width as i32, self.map.height as i32, |x, y| { + let _ = print_map(self.map.width as i32, self.map.height as i32, |x, y| { let mut color = ColorSpec::new(); if let Some((xrange, yrange)) = &self.range { diff --git a/src/belt_finding/mod.rs b/src/belt_finding/mod.rs index 5bc0df6..c030b4c 100644 --- a/src/belt_finding/mod.rs +++ b/src/belt_finding/mod.rs @@ -1,13 +1,12 @@ use crate::graph::wheighted_graph::WheightedGraph; use crate::misc::Map; -use crate::priority_queue::BinaryHeap; use crate::{ graph::wheighted_graph::shortest_path::dijkstra, priority_queue::fibonacci_heap::FibonacciHeap, }; use std::ops::Index; use termcolor::{Color, ColorSpec}; -use self::common::{print_map, Direction, PathField, Position, PositionType}; +use self::common::{print_map, Direction, Position, PositionType}; pub mod brute_force; pub mod common; @@ -89,7 +88,7 @@ impl Problem { } pub fn print(&self) { - print_map(self.map.width as i32, self.map.height as i32, |x, y| { + let _ = print_map(self.map.width as i32, self.map.height as i32, |x, y| { let mut color = ColorSpec::new(); if let Some(i) = self .start @@ -158,18 +157,13 @@ impl<'a> WheightedGraph for MapInternal<'a> { fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> { let next = node.0.in_direction(&node.1, 1); - if next - .in_range( - &Position::new(0, 0), - &Position::new( - self.map.width as PositionType, - self.map.height as PositionType, - ), - ) - .is_none() - { - return None; - } + next.in_range( + &Position::new(0, 0), + &Position::new( + self.map.width as PositionType, + self.map.height as PositionType, + ), + )?; if self.map.get(next.x as usize, next.y as usize).blocked && self.end != (next, node.1) { return None; } @@ -183,17 +177,13 @@ impl<'a> WheightedGraph for MapInternal<'a> { let mut count = 2; for l in 2..=6 { let n = node.0.in_direction(&node.1, l); - if n.in_range( + n.in_range( &Position::new(0, 0), &Position::new( self.map.width as PositionType, self.map.height as PositionType, ), - ) - .is_none() - { - return None; - } + )?; if !self.map.get(n.x as usize, n.y as usize).blocked { count += 1; if count == num { diff --git a/src/priority_queue/fibonacci_heap.rs b/src/priority_queue/fibonacci_heap.rs index a181df0..39f1867 100644 --- a/src/priority_queue/fibonacci_heap.rs +++ b/src/priority_queue/fibonacci_heap.rs @@ -1,7 +1,5 @@ use std::fmt::Debug; -use crate::misc::{Arena, ArenaKey}; - use super::PriorityQueue; type Index = u32;