Skip to content

Commit 29bf40b

Browse files
committed
.
1 parent 163f60b commit 29bf40b

7 files changed

Lines changed: 81 additions & 16 deletions

File tree

cargo/snk-grid/src/grid.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ impl<T: Copy> Grid<T> {
2222
return (x as usize) * (self.height as usize) + (y as usize);
2323
}
2424

25+
pub fn copy(&mut self, other: &Self) -> () {
26+
self.cells.clone_from_slice(&other.cells);
27+
}
2528
pub fn fill(&mut self, value: T) -> () {
2629
self.cells.fill(value);
2730
}

cargo/snk-js/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn get_best_tunnel_to_collect_point(grid: &IColorGrid, to: &IPoint) -> Vec<I
5454
let grid = snk_grid::grid::Grid::from(grid);
5555
let exit_grid = snk_solver::exit_grid::ExitGrid::create_from_grid_color(&grid);
5656
let res =
57-
snk_solver::collect_cost::get_best_tunnel_to_collect_point(&grid, &exit_grid, to.into());
57+
snk_solver::best_tunnel::get_best_tunnel_to_collect_point(&grid, &exit_grid, to.into());
5858

5959
log::info!("{:?} {:?} {:?}", res.path, res.in_cost, res.out_cost);
6060

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use snk_grid::{color::Color, grid::Grid, point::Point, snake::Snake4};
1+
use snk_grid::{
2+
color::Color, grid::Grid, grid_samples::get_grid_sample, point::Point, snake::Snake4,
3+
};
24

35
use crate::{cost::Cost, exit_grid::ExitGrid, snake_path_to_outside::get_snake_path_to_outside};
46

@@ -9,13 +11,8 @@ pub struct Tunnel {
911
pub out_cost: Cost,
1012
}
1113

12-
// pub fn get_collect_cost_(is_outside: F, get_cost: C) -> CollectionCost
13-
// where
14-
// F: Fn(Point) -> bool,
15-
// F: Fn(Point) -> u32,
16-
// {
17-
// }
18-
14+
///
15+
/// it's not accurate and might report worse tunnel
1916
pub fn get_best_tunnel_to_collect_point(
2017
grid: &Grid<Color>,
2118
exit_grid: &ExitGrid,
@@ -149,9 +146,29 @@ _ _
149146

150147
assert_eq!(grid.get_color(Point { x: 6, y: 3 }), Color::Color1);
151148

152-
let pto = ExitGrid::create_from_grid_color(&grid);
149+
let exit_grid = ExitGrid::create_from_grid_color(&grid);
150+
151+
let tunnel = get_best_tunnel_to_collect_point(&grid, &exit_grid, Point { x: 6, y: 3 });
152+
assert_eq!(tunnel.in_cost.get_color_count(Color::Color4), 1);
153+
assert_eq!(tunnel.out_cost.get_color_count(Color::Color4), 0);
154+
155+
assert_eq!(tunnel.in_cost.get_color_count(Color::Color1), 1);
156+
assert_eq!(tunnel.out_cost.get_color_count(Color::Color1), 0);
157+
}
158+
159+
#[test]
160+
fn it_should_get_snake_path_to_outside_3() {
161+
let grid = get_grid_sample(snk_grid::grid_samples::SampleGrid::Caves);
162+
163+
assert_eq!(grid.get_color(Point { x: 29, y: 3 }), Color::Empty);
164+
assert_eq!(grid.get_color(Point { x: 29, y: 4 }), Color::Color4);
165+
166+
let exit_grid = ExitGrid::create_from_grid_color(&grid);
167+
168+
let tunnel = get_best_tunnel_to_collect_point(&grid, &exit_grid, Point { x: 29, y: 3 });
169+
170+
println!("{:?}", tunnel);
153171

154-
let tunnel = get_best_tunnel_to_collect_point(&grid, &pto, Point { x: 6, y: 3 });
155172
assert_eq!(tunnel.in_cost.get_color_count(Color::Color4), 1);
156173
assert_eq!(tunnel.out_cost.get_color_count(Color::Color4), 0);
157174

cargo/snk-solver/src/cost.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ impl Cost {
5050
pub fn is_free(&self) -> bool {
5151
self.0 < 256
5252
}
53+
pub fn set_empty_to_zero(&self) -> Self {
54+
Self((self.0 / 256) * 256)
55+
}
5356

5457
// return the count for the given color
5558
pub fn get_color_count(&self, color: Color) -> u64 {

cargo/snk-solver/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// pub mod cave;
22
// pub mod reach_outside;
3-
pub mod collect_cost;
3+
pub mod best_tunnel;
44
mod cost;
55
pub mod exit_grid;
66
mod fitness;

cargo/snk-solver/src/snake_path_to_outside.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ where
8686
mod tests {
8787
use super::*;
8888

89-
use snk_grid::{color::Color, grid::Grid, point::Point, snake::Snake4};
89+
use snk_grid::{
90+
color::Color, grid::Grid, grid_samples::get_grid_sample, point::Point, snake::Snake4,
91+
};
9092

9193
use crate::exit_grid::ExitGrid;
9294

cargo/snk-solver/src/solver.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,47 @@
1-
use snk_grid::{color::Color, direction::Direction, grid::Grid, snake::Snake4};
1+
use std::collections::HashMap;
22

3-
pub fn solve(grid: &Grid<Color>, snake: &Snake4) -> Vec<Direction> {
4-
vec![]
3+
use snk_grid::{
4+
color::Color,
5+
direction::Direction,
6+
grid::{Grid, iter_rectangle_fill},
7+
point::Point,
8+
snake::Snake4,
9+
};
10+
11+
use crate::{
12+
best_tunnel::{Tunnel, get_best_tunnel_to_collect_point},
13+
cost::Cost,
14+
exit_grid::ExitGrid,
15+
};
16+
17+
pub const COLORS: [Color; 4] = [Color::Color1, Color::Color2, Color::Color3, Color::Color4];
18+
19+
pub fn solve(color_grid: &Grid<Color>, snake: &Snake4) -> Vec<Direction> {
20+
let exit_grid = ExitGrid::create_from_grid_color(color_grid);
21+
22+
let snake = snake.clone();
23+
let path = Vec::new();
24+
25+
for color in COLORS.into_iter() {
26+
let to_collect = iter_rectangle_fill(color_grid.width, color_grid.height)
27+
.filter(|p| color_grid.get_color(*p) == color)
28+
.map(|p| {
29+
let tunnel = get_best_tunnel_to_collect_point(&color_grid, &exit_grid, p);
30+
let score = tunnel.in_cost.set_empty_to_zero() * 2
31+
+ tunnel.out_cost.set_empty_to_zero() * 3;
32+
(p, tunnel, score, exit_grid.get_cost_to_outside(p))
33+
});
34+
35+
let (free_to_collect, mut rest): (Vec<_>, Vec<_>) =
36+
to_collect.partition(|(_, _, global_cost,_)| global_cost.is_free());
37+
38+
// todo!("collect free points");
39+
40+
rest.sort_by(|a, b| a.2.cmp(&b.2));
41+
42+
}
43+
44+
path
545
}
646

747
#[cfg(test)]

0 commit comments

Comments
 (0)