diff --git a/src/main.rs b/src/main.rs index 7481c80..d302c5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -322,8 +322,22 @@ impl Game { hasher.finish() }; + #[derive(PartialEq)] + struct Ordf32(f32); + impl Eq for Ordf32 {} + impl PartialOrd for Ordf32 { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } + } + impl Ord for Ordf32 { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.0.total_cmp(&other.0) + } + } + let mut states = Vec::new(); - let mut todo = BinaryHeap::<((i64, i64), i64, usize)>::new(); + let mut todo = BinaryHeap::<(Ordf32, usize, usize)>::new(); let mut had = HashSet::new(); let mut ctr = 0; @@ -332,7 +346,7 @@ impl Game { parent_index: 0, m: Move::new(0, 0), }); - todo.push(((0, 0), 1000000000, 0)); + todo.push((Ordf32(0.0), 1000000000, 0)); while let Some((score, depth, state_index)) = todo.pop() { ctr += 1; @@ -373,36 +387,8 @@ impl Game { working_instance.towers = state.towers.clone(); working_instance.make_move(m).unwrap(); - let brs: i64 = { - let this = &working_instance; - let mut seen_bottom_rings = HashSet::new(); - this.towers - .iter() - .map(|i| { - let this = &i; - let mut ring_type = None; - let mut burried_score = 0; - - for ring in this.rings.iter().rev().flatten() { - if let Some(existing_ring_type) = ring_type - && existing_ring_type != *ring - { - burried_score += 1; - } - ring_type = Some(*ring); - } - if let Some(Some(ring)) = this.rings.last() { - if seen_bottom_rings.contains(ring) { - burried_score += 1; - } else { - seen_bottom_rings.insert(ring); - } - } - - burried_score - }) - .sum() - }; + let num_solved = working_instance.num_solved(); + let brs = working_instance.burried_ring_score(); let new_state_hash = hash_state(&working_instance.towers); if had.contains(&new_state_hash) { @@ -416,8 +402,9 @@ impl Game { m, }); - let score = (depth) - (brs); - todo.push(((score, -depth), depth - 1, new_state_index)); + let score = (num_solved as f32 * 10.0) + (-(brs as f32) * 100.0); + + todo.push((Ordf32(score), depth - 1, new_state_index)); } }