Skip to content

Commit

Permalink
day 17: cleanup a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
anisse committed Dec 17, 2023
1 parent b27ef63 commit 088f00d
Showing 1 changed file with 9 additions and 48 deletions.
57 changes: 9 additions & 48 deletions src/bin/day17.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ fn main() {
}
type Map = Vec<Vec<u8>>;
type MapRef<'a> = &'a [Vec<u8>];
//type MapRefMut<'a> = &'a mut [Vec<u8>];
fn parse(input: &str) -> Map {
input
.lines()
Expand All @@ -27,7 +26,6 @@ fn part1(map: MapRef) -> usize {
fn shortest_path_common(map: MapRef, move_range: Range<usize>) -> usize {
let mut cost_map = vec![vec![Cost::default(); map[0].len()]; map.len()];
let mut queue = BinaryHeap::new();
//let mut mincost = usize::MAX;
let end = Pos {
row: map.len() - 1,
col: map[0].len() - 1,
Expand All @@ -37,35 +35,28 @@ fn shortest_path_common(map: MapRef, move_range: Range<usize>) -> usize {
pos: Pos { row: 0, col: 0 },
cost: 0,
dir: UP,
//last_cur_dir: 0,
};
queue.push(Reverse(state.clone()));
state.dir = LEFT;
queue.push(Reverse(state));

while let Some(Reverse(cur)) = queue.pop() {
let cost_pos = &cost_map[cur.pos.row][cur.pos.col];
if cost_pos.current(cur.dir /*, cur.last_cur_dir*/) < cur.cost {
if cost_pos.current(cur.dir) < cur.cost {
continue;
}
/*
println!(
"Now evaluating pos {:?}, dir: {}, cost is {}",
cur.pos,
cur.dir,
cur.cost, //cur.last_cur_dir,
cur.cost,
);
*/
cost_map[cur.pos.row][cur.pos.col].set(cur.dir, /*cur.last_cur_dir,*/ cur.cost);
cost_map[cur.pos.row][cur.pos.col].set(cur.dir, cur.cost);
if cur.pos == end {
//println!("END REACHED\n===============================\n");
return cur.cost;
/*
if cur.cost < mincost {
mincost = cur.cost;
}
continue;
*/
}
if cur.cost % 25 == 0 {
println!("Cost {}", cur.cost);
Expand All @@ -84,16 +75,6 @@ fn shortest_path_common(map: MapRef, move_range: Range<usize>) -> usize {
}
for advance in move_range.clone() {
if let Some(pos) = next_pos(&cur.pos, dir, map, advance + 1) {
/*
let last_cur_dir = if dir == cur.dir {
cur.last_cur_dir + 1
} else {
0
};
if last_cur_dir > 2 {
continue;
}
*/
cost += map[pos.row][pos.col] as usize;
/*
println!(
Expand All @@ -105,7 +86,6 @@ fn shortest_path_common(map: MapRef, move_range: Range<usize>) -> usize {
pos: pos.clone(),
dir,
cost,
//last_cur_dir,
}));
} else {
break;
Expand All @@ -114,15 +94,16 @@ fn shortest_path_common(map: MapRef, move_range: Range<usize>) -> usize {
}
}
0
//mincost
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Pos {
row: usize,
col: usize,
}
/*
const RIGHT: u8 = 0;
const DOWN: u8 = 1;
*/
const LEFT: u8 = 2;
const UP: u8 = 3;
fn next_pos(pos: &Pos, dir: u8, map: MapRef, advance: usize) -> Option<Pos> {
Expand All @@ -146,7 +127,6 @@ struct State {
pos: Pos,
cost: usize,
dir: u8,
//last_cur_dir: u8,
}
impl Ord for State {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
Expand All @@ -169,35 +149,16 @@ impl Default for Cost {
}
}
impl Cost {
fn current(&self, dir: u8 /*last_cur_dir: u8*/) -> usize {
fn current(&self, dir: u8) -> usize {
assert!(dir < 4);
//assert!(last_cur_dir < 3);
self.c[dir as usize % 2] /* * 3 + last_cur_dir as usize]*/ as usize
self.c[dir as usize % 2] as usize
}
fn set(&mut self, dir: u8, /*last_cur_dir: u8,*/ val: usize) {
fn set(&mut self, dir: u8, val: usize) {
assert!(dir < 4);
//assert!(last_cur_dir < 3);
assert!(val < u32::MAX as usize);
self.c[dir as usize % 2 /* * 3 + last_cur_dir as usize*/] = val as u32;
}
}
/*
#[derive(Debug, Clone)]
struct Cost {
cost: usize,
dir: u8,
last_cur_dir: u8,
}
impl Default for Cost {
fn default() -> Self {
Cost {
cost: usize::MAX,
dir: 5, // nonexistent
last_cur_dir: 0,
}
self.c[dir as usize % 2] = val as u32;
}
}
*/

fn part2(map: MapRef) -> usize {
shortest_path_common(map, 3..10)
Expand Down

0 comments on commit 088f00d

Please sign in to comment.