@@ -10,16 +10,22 @@ let rec maze_robot = ()
10
10
11
11
let current_pos = ref (0 ,0 )
12
12
let update_current_pos p = (fun c -> match c , ! p with
13
- | 'U' , (a ,b ) -> p := (a+ 1 ,b ); p
14
- | 'D' , (a ,b ) -> p := (a-1 ,b ); p
15
- | 'R' , (a ,b ) -> p := (a,b + 1 ); p
16
- | 'L' , (a ,b ) -> p := (a,b -1 ); p
13
+ | 'U' , (a ,b ) -> p := (a,b + 1 ); p
14
+ | 'D' , (a ,b ) -> p := (a,b -1 ); p
15
+ | 'R' , (a ,b ) -> p := (a+ 1 ,b ); p
16
+ | 'L' , (a ,b ) -> p := (a-1 ,b ); p
17
17
)
18
18
19
19
let pos_visited = ref [(0 ,0 )];;
20
20
21
21
let update_pos_visited p = (fun cp -> (p := (cp :: ! p)); p);;
22
-
22
+
23
+ let rec visited_already pos_list pos = match pos_list with
24
+ | [] -> false
25
+ | h ::t -> if (h = pos) then true else (we_visited_already t pos)
26
+
27
+ let havent_visited_yet pos_list pos = not (we_visited_already pos_list pos)
28
+
23
29
let letters_caught = ref []
24
30
25
31
let remove_first_letter = (fun s -> String. sub s 1 ((String. length s) - 1 ))
@@ -34,7 +40,25 @@ let rec there_is_a_letter_we_can_move_to = (fun s -> match (string_to_list_of_c
34
40
| h :: t -> if (h = '#' || h = ' ' ) then (there_is_a_letter_we_can_move_to (remove_first_letter s))
35
41
else
36
42
true )
37
-
43
+
44
+ let rec join l1 l2 = match l1, l2 with
45
+ | [] , [] -> []
46
+ | h1 ::t1 , h2 ::t2 -> (h1,h2) :: join t1 t2;;
47
+
48
+ let view_to_tuples current_view =
49
+ let l1 = string_to_list_of_chars (remove_first_letter current_view) in
50
+ let l2 = ['U' ;'D' ;'L' ;'R' ] in
51
+ join l1 l2;;
52
+
53
+ (* method below moves to first new square available --> looks Up, then down, then left then right *)
54
+ let rec make_new_move view_tuples current_pos pos_list = match view_tuples, current_pos with
55
+ | [] , _ -> 'K' (* no new move available *)
56
+ | (_ ,'#' ) :: t , _ -> make_new_move t current_pos pos_list
57
+ | ('U' ,_ ) :: t , (x ,y ) -> if (havent_visited_yet ! pos_list (x,y+ 1 )) then 'U' else make_new_move t current_pos pos_list
58
+ | ('D' ,_ ) :: t , (x ,y ) -> if (havent_visited_yet ! pos_list (x,y-1 )) then 'D' else make_new_move t current_pos pos_list
59
+ | ('L' ,_ ) :: t , (x ,y ) -> if (havent_visited_yet ! pos_list (x-1 ,y)) then 'L' else make_new_move t current_pos pos_list
60
+ | ('R' ,_ ) :: t , (x ,y ) -> if (havent_visited_yet ! pos_list (x+ 1 ,y)) then 'R' else make_new_move t current_pos pos_list
61
+
38
62
39
63
let check_if_sitting_on_letter = (fun s -> match (String. get s 0 ) with
40
64
| '#' -> (letters_caught := ! letters_caught)
@@ -59,7 +83,16 @@ let move_to_first_available_space = (fun s ->
59
83
let make_naive_move = (fun s -> (check_if_sitting_on_letter s); if (there_is_a_letter_we_can_move_to s) then (move_to_first_available_letter s)
60
84
else
61
85
(move_to_first_available_space s))
62
-
86
+ let make_slightly_better_move current_pos current_view pos_list =
87
+ check_if_sitting_on_letter current_view;
88
+ if (there_is_a_new_letter current_view current_pos pos_list) then (move_to_first_available_letter s)
89
+ else
90
+ move_to_first_available_space current_view;;
91
+ (*
92
+ let make_even_better_move current_pos current_view pos_list =
93
+ check_if_sitting_on_letter current_view;
94
+ move_to_all_new_locations
95
+ *)
63
96
64
97
let sort_char_list_alphabetically = ()
65
98
let concatenate_list = ()
0 commit comments