From 9159b7cf2a3b6c866cf8f4b3dbc9aa14a6873d86 Mon Sep 17 00:00:00 2001 From: Himangshu Saikia Date: Sun, 22 Dec 2024 08:29:15 +0100 Subject: [PATCH] Added grid debug print --- src/bin/2024_20/main.rs | 3 ++- src/grid.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/bin/2024_20/main.rs b/src/bin/2024_20/main.rs index 1c18d9f..5b05c5f 100644 --- a/src/bin/2024_20/main.rs +++ b/src/bin/2024_20/main.rs @@ -37,6 +37,7 @@ fn shortest_path(map: &Grid) -> Vec<(usize, usize)> { fn solve(input: &str) -> usize { let map = Grid::from_str(input, |c| c); + // map.debug_print(); let path = shortest_path(&map); let path_d: Vec<((usize, usize), usize)> = path.iter().enumerate().map(|(i, v)| (*v, i)).collect(); @@ -62,7 +63,7 @@ fn solve(input: &str) fn main() { let input = common::get_input(); - println!("{input:?}"); + //println!("{input:?}"); common::timed(&input, solve::<100, 2>, true); common::timed(&input, solve::<100, 20>, false); } diff --git a/src/grid.rs b/src/grid.rs index cc9748c..51d2dce 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -179,6 +179,39 @@ impl Grid { println!(); } + /// Works best only if the cell values fit in a 3-character space + pub fn debug_print(&self) + where + T: std::fmt::Display, + { + let mut ret: String = String::new(); + for r in 0..=2 * self.rows + 1 { + for c in 0..=4 * self.cols { + if r % 2 == 0 { + if c % 4 == 0 { + ret += "+"; + } else { + ret += "-"; + } + } else if c % 4 == 0 { + ret += "|"; + } else if c % 4 == 2 { + if r == 2 * self.rows + 1 { + ret += &format!("{:^3}", (c - 2) / 4); + } else { + ret += &format!("{:^3}", self.values[(r - 1) / 2][(c - 2) / 4]); + } + } + } + if r % 2 == 0 { + ret += "\n"; + } else { + ret += &format!("{}\n", (r - 1) / 2); + } + } + println!("{}", ret); + } + pub fn to_flat_idx(&self, idx: &CellIndex) -> usize { if idx.0 >= self.rows || idx.1 >= self.cols { panic!("Grid index out of bounds");