Explorar el Código

Solution Day 6 Part 1

AbdoFizzy hace 5 meses
padre
commit
8a65348a8c
Se han modificado 3 ficheros con 45 adiciones y 1 borrados
  1. 3 0
      6/dune
  2. 41 0
      6/solve_1.ml
  3. 1 1
      README.md

+ 3 - 0
6/dune

@@ -0,0 +1,3 @@
+(executables
+ (names  solve_1 solve_2)
+ (libraries base stdio str))

+ 41 - 0
6/solve_1.ml

@@ -0,0 +1,41 @@
+open Stdio
+open Str
+
+let input_info = 
+  let rec one_string inp nb_rows nb_columns = 
+    let line = In_channel.input_line In_channel.stdin in
+    match line with
+    | None -> (inp, (nb_rows, nb_columns))
+    | Some x -> one_string (inp ^ x) (nb_rows + 1) (String.length x)
+  in one_string "" 0 0
+;;
+
+let input = fst input_info;;
+let nb_rows = (fst (snd input_info));;
+let nb_columns = (snd (snd input_info));;
+
+
+let rec run_guard pos direction l_pos = 
+  if (pos < 0 || pos >= (nb_rows*nb_columns)) then (List.tl l_pos) else
+  match direction with
+  | '^' -> let new_pos = (pos - nb_columns) in
+           if (new_pos >= 0 && input.[new_pos] == '#') then run_guard pos '>' l_pos else run_guard new_pos '^' (new_pos::l_pos)
+  | 'v' -> let new_pos = (pos + nb_columns) in
+           if (new_pos < (nb_rows*nb_columns) && input.[new_pos] == '#') then run_guard pos '<' l_pos  else run_guard new_pos 'v' (new_pos::l_pos)
+  | '<' -> let new_pos = (pos - 1) in
+           if (new_pos >= 0 && input.[new_pos] == '#') then run_guard pos '^' l_pos else run_guard new_pos '<' (new_pos::l_pos)
+  | '>' -> let new_pos = (pos + 1) in
+           if (new_pos < (nb_rows*nb_columns) && input.[new_pos] == '#') then run_guard pos 'v' l_pos else run_guard new_pos '>' (new_pos::l_pos)
+  | _ -> [] (*Not supposed to happen*)
+
+let rec eliminate_duplicates = function
+| a :: (b :: _ as t) -> if a = b then eliminate_duplicates t else a :: eliminate_duplicates t
+| smaller -> smaller;;
+
+let solve =
+    let initial_pos = search_forward (regexp {|\^\|<\|>\|v|}) input 0 in
+    let guard_direction = input.[initial_pos] in
+    let list_of_positions = (List.sort compare (run_guard initial_pos guard_direction [initial_pos]))in
+  List.length (eliminate_duplicates list_of_positions);;
+
+let () = printf "Total: %d\n" solve;;

+ 1 - 1
README.md

@@ -47,7 +47,7 @@ replace `<part_number>` with 1 for Part 1 or 2 for Part 2
 | 3   | ✅     | ✅     |
 | 4   | ✅     | ✅     |
 | 5   | ✅     | ✅     |
-| 6   |      | ⬜     |
+| 6   |      | ⬜     |
 | 7   | ⬜     | ⬜     |
 | 8   | ⬜     | ⬜     |
 | ... | ...    | ...    |