Skip to content

Commit db28309

Browse files
committed
.
1 parent 621d78b commit db28309

File tree

5 files changed

+75
-64
lines changed

5 files changed

+75
-64
lines changed

packages/solver-r/src/grid.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,15 @@ impl Grid {
4848
}
4949
}
5050

51+
pub const DIRECTION_RIGHT: Point = Point { x: 1, y: 0 };
52+
pub const DIRECTION_LEFT: Point = Point { x: -1, y: 0 };
53+
pub const DIRECTION_UP: Point = Point { x: 0, y: 1 };
54+
pub const DIRECTION_DOWN: Point = Point { x: 0, y: -1 };
5155
pub const DIRECTIONS: [Point; 4] = [
52-
Point { x: 1, y: 0 },
53-
Point { x: -1, y: 0 },
54-
Point { x: 0, y: 1 },
55-
Point { x: 0, y: -1 },
56+
DIRECTION_RIGHT,
57+
DIRECTION_LEFT,
58+
DIRECTION_UP,
59+
DIRECTION_DOWN,
5660
];
5761

5862
#[test]

packages/solver-r/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod grid;
22
mod snake;
3+
mod snake_compact;
34
mod snake_walk;
45
mod solver;
56

packages/solver-r/src/snake-compact.rs

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

packages/solver-r/src/snake.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,64 @@
1-
use crate::grid::Point;
1+
use crate::grid::{Point, DIRECTIONS, DIRECTION_DOWN, DIRECTION_LEFT, DIRECTION_UP};
22

33
/**
44
* head is at 0
55
*/
66
pub type Snake = Vec<Point>;
7+
8+
pub fn move_snake(s: &mut Snake, dir: &Point) -> () {
9+
let mut e = s.pop().unwrap();
10+
e.x = s[0].x + dir.x;
11+
e.y = s[0].y + dir.y;
12+
s.insert(0, e);
13+
}
14+
pub fn will_self_collide(s: &Snake, dir: &Point) -> bool {
15+
let next_head = Point {
16+
x: s[0].x + dir.x,
17+
y: s[0].y + dir.y,
18+
};
19+
20+
(&s[0..(s.len() - 1)]).contains(&next_head)
21+
}
22+
23+
#[test]
24+
fn it_should_detect_self_collide() {
25+
let mut s = vec![
26+
//
27+
Point { x: 6, y: 0 },
28+
Point { x: 5, y: 0 },
29+
Point { x: 4, y: 0 },
30+
Point { x: 3, y: 0 },
31+
Point { x: 2, y: 0 },
32+
Point { x: 1, y: 0 },
33+
];
34+
35+
move_snake(&mut s, &DIRECTION_UP);
36+
move_snake(&mut s, &DIRECTION_LEFT);
37+
38+
assert_eq!(will_self_collide(&s, &DIRECTION_DOWN), true);
39+
40+
move_snake(&mut s, &DIRECTION_LEFT);
41+
42+
assert_eq!(will_self_collide(&s, &DIRECTION_DOWN), false);
43+
}
44+
#[test]
45+
fn it_should_move_snake() {
46+
let mut s = vec![
47+
//
48+
Point { x: 3, y: 0 },
49+
Point { x: 2, y: 0 },
50+
Point { x: 1, y: 0 },
51+
];
52+
53+
move_snake(&mut s, &DIRECTION_UP);
54+
55+
assert_eq!(
56+
s,
57+
vec![
58+
//
59+
Point { x: 3, y: 1 },
60+
Point { x: 3, y: 0 },
61+
Point { x: 2, y: 0 },
62+
]
63+
);
64+
}

packages/solver-r/src/snake_walk.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ pub fn get_route_to_eat_all(
99
initial_snake: &Snake,
1010
cells_to_eat: HashSet<Point>,
1111
) -> Vec<Snake> {
12+
let mut targets = cells_to_eat.clone();
13+
14+
while let Some(p) = targets.iter().next().cloned() {
15+
targets.remove(&p);
16+
17+
let mut open_list: HashSet<Snake> = HashSet::new();
18+
}
1219
for dir in DIRECTIONS {}
1320
Vec::new()
1421
}

0 commit comments

Comments
 (0)