Browse Source

solution day 8 in progress

AbderFaiz 4 months ago
parent
commit
2f19823c78
2 changed files with 33 additions and 4 deletions
  1. 1 1
      1/solve_2.ml
  2. 32 3
      8/solve_1.ml

+ 1 - 1
1/solve_2.ml

@@ -23,4 +23,4 @@ let rec solve accum = function
 
 let () =
   let (li1,li2) = construct_list [] [] in
-    printf "Total: %d\n" (solve 0 (li1,li2))
+    printf "Total: %d\n" (solve 0 (li1,li2))

+ 32 - 3
8/solve_1.ml

@@ -1,5 +1,7 @@
 open Stdio
-(* open Str *)
+open Str
+module CharSet = Set.Make(Char)
+module IntSet = Set.Make(Int)
 
 let input_info = 
   let rec one_string inp nb_rows nb_columns = 
@@ -10,13 +12,40 @@ let input_info =
   in one_string "" 0 0
 ;;
 
-let grid = (fst input_info);;
+let input = (fst input_info);;
 let nb_rows = (fst (snd input_info));;
 let nb_columns =(snd (snd input_info));;
 
+let encode_movement direction pos = 
+  match direction with
+  | '^' -> let new_pos = pos - nb_columns in
+           if (new_pos < 0) then -1 else new_pos
+  | 'v' -> let new_pos = (pos + nb_columns) in
+           if (new_pos < (nb_rows*nb_columns)) then -1  else new_pos
+  | '<' -> let new_pos = (pos - 1) in
+           if (new_pos mod nb_columns == nb_columns -1) then -1 else
+           if (new_pos < 0) then -1 else new_pos
+  | '>' -> let new_pos = (pos + 1) in
+           if (new_pos mod nb_columns == 0) then -1 else 
+           if (new_pos >= (nb_rows*nb_columns)) then -1 else new_pos
+  | _ -> -1 (*Not supposed to happen*)
+;;
+
+let get_antennas =
+    let rec aux pos accum=
+    try
+      let pos_antenna = search_forward (regexp {|[^.]|}) input pos in
+      aux (pos_antenna + 1) (CharSet.add input.[pos_antenna] accum) 
+    with Not_found -> accum
+  in
+  aux 0 CharSet.empty
+;;
+
 let solve = 0
 ;;
 
-let () = printf "%d %d\n\n%s" nb_rows nb_columns grid;
+let () = printf "Antennas : ";
+         CharSet.iter (fun a -> printf "%c " a) get_antennas;
+         printf "\n";
 printf "Total: %d\n" solve;;