Skip to content

Commit

Permalink
Added grid debug print
Browse files Browse the repository at this point in the history
  • Loading branch information
hsaikia committed Dec 22, 2024
1 parent 01a539c commit 9159b7c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/bin/2024_20/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn shortest_path(map: &Grid<char>) -> Vec<(usize, usize)> {

fn solve<const PICOSECONDS: usize, const CHEAT_PICOSECONDS: usize>(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();
Expand All @@ -62,7 +63,7 @@ fn solve<const PICOSECONDS: usize, const CHEAT_PICOSECONDS: usize>(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);
}
Expand Down
33 changes: 33 additions & 0 deletions src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,39 @@ impl<T: std::fmt::Debug + Clone + Default + PartialEq + Hash> Grid<T> {
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");
Expand Down

0 comments on commit 9159b7c

Please sign in to comment.