Skip to content

Commit 74536c7

Browse files
committed
cost
1 parent 62ae0b7 commit 74536c7

File tree

8 files changed

+103
-121
lines changed

8 files changed

+103
-121
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818

1919
- run: bun install --frozen-lockfile
2020

21-
- run: cargo test
21+
- run: cargo nextest run
2222
working-directory: cargo
2323

2424
- run: cargo fmt --check

cargo/snk-grid/src/color.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ impl Color {
1919
pub fn is_empty(&self) -> bool {
2020
*self == Color::Empty
2121
}
22-
pub fn cost(&self) -> u32 {
23-
match self {
24-
Color::Empty => 0,
25-
Color::Color1 => 1,
26-
Color::Color2 => 128,
27-
Color::Color3 => 128 * 128,
28-
Color::Color4 => 128 * 128 * 128,
29-
}
30-
}
3122
}
3223

3324
#[test]
@@ -37,8 +28,3 @@ fn it_should_sort_cell() {
3728
assert_eq!(Color::Color2 < Color::Color3, true);
3829
assert_eq!(Color::Color3 < Color::Color4, true);
3930
}
40-
#[test]
41-
fn it_should_sum_cost() {
42-
// it should not panic
43-
let _somehow_max_cost = Color::Color4.cost() * 350;
44-
}

cargo/snk-grid/src/grid_ascii.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ pub fn grid_from_ascii<T: Copy + Default + From<char>>(ascii: &str) -> Grid<T> {
2424
grid
2525
}
2626

27-
pub fn grid_to_ascii<T: Copy + ToString>(grid: &Grid<T>) -> String {
27+
pub fn grid_to_ascii_transformed<T: Copy, F>(grid: &Grid<T>, to_string: F) -> String
28+
where
29+
F: Fn(T) -> String,
30+
{
2831
let mut out: String = String::new();
2932
for y in 0..grid.height {
3033
for x in 0..grid.width {
3134
let value = grid.get(Point {
3235
x: x as i8,
3336
y: y as i8,
3437
});
35-
out.push(value.to_string().chars().nth(0).unwrap());
38+
out.push(to_string(value).chars().nth(0).unwrap());
3639
}
3740
if match out.chars().last() {
3841
Some(c) => c == ' ',
@@ -48,6 +51,10 @@ pub fn grid_to_ascii<T: Copy + ToString>(grid: &Grid<T>) -> String {
4851
out
4952
}
5053

54+
pub fn grid_to_ascii<T: Copy + ToString>(grid: &Grid<T>) -> String {
55+
grid_to_ascii_transformed(grid, |value| value.to_string())
56+
}
57+
5158
impl<T: ToString + Copy> ToString for Grid<T> {
5259
fn to_string(&self) -> String {
5360
grid_to_ascii(self)

cargo/snk-solver/src/collect_cost.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use snk_grid::{
66
};
77
use std::collections::HashSet;
88

9-
use crate::path_to_outside_grid::ExitDirection;
9+
use crate::{cost::Cost, path_to_outside_grid::ExitDirection};
1010

1111
pub struct CollectionCost {
12-
in_cost: u32,
13-
out_cost: u32,
12+
in_cost: Cost,
13+
out_cost: Cost,
1414
}
1515

1616
// pub fn get_collect_cost_(is_outside: F, get_cost: C) -> CollectionCost
@@ -32,7 +32,7 @@ pub fn get_collect_cost(
3232
let mut p = dot;
3333
loop {
3434
let e = exit_grid.get(dot);
35-
if e.cost == 0 {
35+
if e.cost.is_free() {
3636
break;
3737
}
3838
grid.set(p, Color::Empty);
@@ -45,6 +45,6 @@ pub fn get_collect_cost(
4545

4646
CollectionCost {
4747
in_cost,
48-
out_cost: 0,
48+
out_cost: Cost::zero(),
4949
}
5050
}

cargo/snk-solver/src/cost.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use std::ops::{Add, AddAssign, Mul};
2+
3+
use snk_grid::color::Color;
4+
5+
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
6+
pub struct Cost(pub u64);
7+
8+
impl Add for Cost {
9+
type Output = Self;
10+
11+
fn add(self, rhs: Self) -> Self {
12+
Self(self.0 + rhs.0)
13+
}
14+
}
15+
impl AddAssign for Cost {
16+
fn add_assign(&mut self, rhs: Self) {
17+
self.0 += rhs.0;
18+
}
19+
}
20+
impl Mul<u64> for Cost {
21+
type Output = Self;
22+
23+
fn mul(self, rhs: u64) -> Self {
24+
Self(self.0 * rhs)
25+
}
26+
}
27+
28+
impl From<Color> for Cost {
29+
fn from(color: Color) -> Self {
30+
match color {
31+
Color::Empty => Cost(1),
32+
Color::Color1 => Cost(256),
33+
Color::Color2 => Cost(256 * 200),
34+
Color::Color3 => Cost(256 * 200 * 200),
35+
Color::Color4 => Cost(256 * 200 * 200 * 200),
36+
}
37+
}
38+
}
39+
40+
impl Cost {
41+
pub fn zero() -> Self {
42+
Self(0)
43+
}
44+
pub fn max() -> Self {
45+
Self(u64::MAX)
46+
}
47+
pub fn is_free(&self) -> bool {
48+
self.0 < 256
49+
}
50+
}
51+
52+
#[test]
53+
fn it_should_not_overflow() {
54+
// it should not panic
55+
let very_large_cost = Cost::from(Color::Color4) * 256;
56+
assert!(very_large_cost < Cost::max())
57+
}
58+
59+
#[test]
60+
fn it_should_sum_cost() {
61+
let mut c = Cost::zero();
62+
c = c + Color::Color1.into();
63+
assert!(Cost::zero() < c);
64+
}

cargo/snk-solver/src/cost_to_outside.rs

Lines changed: 0 additions & 74 deletions
This file was deleted.

cargo/snk-solver/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// pub mod cave;
22
// pub mod reach_outside;
33
mod collect_cost;
4-
pub mod cost_to_outside;
4+
mod cost;
55
mod fitness;
66
mod path_to_outside_grid;
77
pub mod snake_path;

cargo/snk-solver/src/path_to_outside_grid.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,21 @@ use snk_grid::{
22
color::Color,
33
direction::{Direction, add_direction, iter_directions},
44
grid::{Grid, iter_rectangle_hull},
5+
grid_ascii::grid_to_ascii_transformed,
56
point::Point,
67
};
78
use std::collections::HashSet;
89

10+
use crate::cost::Cost;
11+
912
#[derive(Copy, Clone)]
1013
pub struct ExitDirection {
11-
pub cost: u32,
14+
pub cost: Cost,
1215
pub exit_direction: Direction,
1316
}
1417
impl ExitDirection {
1518
pub fn is_outside(&self) -> bool {
16-
self.cost == 0
17-
}
18-
}
19-
impl ToString for ExitDirection {
20-
fn to_string(&self) -> String {
21-
if self.cost == 0 {
22-
"o".to_string()
23-
} else {
24-
self.exit_direction.to_string()
25-
}
19+
self.cost.is_free()
2620
}
2721
}
2822

@@ -33,7 +27,7 @@ pub fn create_path_to_outside(grid: &Grid<Color>) -> Grid<ExitDirection> {
3327
grid.width,
3428
grid.height,
3529
ExitDirection {
36-
cost: u32::MAX,
30+
cost: Cost::max(),
3731
exit_direction: Direction::UP,
3832
},
3933
);
@@ -53,16 +47,16 @@ pub fn create_path_to_outside(grid: &Grid<Color>) -> Grid<ExitDirection> {
5347
} {
5448
changed.remove(&p);
5549

56-
let cost = if path_to_outside.is_inside(p) {
50+
let cost: Cost = if path_to_outside.is_inside(p) {
5751
path_to_outside.get(p).cost
5852
} else {
59-
0
53+
Cost::zero()
6054
};
6155

6256
for dir in iter_directions() {
6357
let p = add_direction(p, dir);
6458
if path_to_outside.is_inside(p) {
65-
let new_cost = cost + grid.get(p).cost();
59+
let new_cost = cost + grid.get(p).into();
6660

6761
let c = path_to_outside.get_mut(p);
6862

@@ -79,25 +73,30 @@ pub fn create_path_to_outside(grid: &Grid<Color>) -> Grid<ExitDirection> {
7973
}
8074

8175
#[test]
82-
#[ignore]
8376
fn it_should_compute_the_cost_to_outside() {
8477
let grid = Grid::<_>::from(
8578
r#"
8679
_...._
87-
_. .._
88-
_...._
89-
_...._
80+
_. ._
81+
_.. ._
82+
_. _
9083
"#,
9184
);
9285
let pto = create_path_to_outside(&grid);
9386

9487
assert_eq!(
95-
pto.to_string(),
88+
grid_to_ascii_transformed(&pto, |c| {
89+
if c.cost.is_free() {
90+
c.cost.0.to_string()
91+
} else {
92+
"#".to_string()
93+
}
94+
}),
9695
r#"
97-
o↑↑↑→o
98-
o←←→→o
99-
o←↑↓→o
100-
o←↓↓→o
96+
1####1
97+
1#43#1
98+
1##2#1
99+
1#1111
101100
"#
102101
.trim(),
103102
);

0 commit comments

Comments
 (0)