Pārlūkot izejas kodu

add solution day 3 part 1

AbdoFizzy 6 mēneši atpakaļ
vecāks
revīzija
aea4b55b11
8 mainītis faili ar 94 papildinājumiem un 7 dzēšanām
  1. 3 0
      .gitignore
  2. 3 3
      1/solve_1.ml
  3. 3 3
      1/solve_2.ml
  4. 3 0
      3/dune
  5. 0 0
      3/input
  6. 23 0
      3/solve_1.ml
  7. 58 0
      3/solve_2.ml
  8. 1 1
      README.md

+ 3 - 0
.gitignore

@@ -27,3 +27,6 @@ setup.log
 
 # Local OPAM switch
 _opam/
+
+# tests
+test

+ 3 - 3
1/solve_1.ml

@@ -6,9 +6,9 @@ let rec construct_list l1 l2 =
   let line = In_channel.input_line In_channel.stdin in
   match line with
   | None -> (List.sort compare l1, List.sort compare l2) 
-  | Some x -> if (Str.string_match (regexp {|\([0-9]+\) *\([0-9]+\)|}) x 0) then
-               construct_list ((Float.of_string (Str.matched_group 1 x))::l1)
-                              ((Float.of_string (Str.matched_group 2 x))::l2) 
+  | Some x -> if (string_match (regexp {|\([0-9]+\) *\([0-9]+\)|}) x 0) then
+               construct_list ((Float.of_string (matched_group 1 x))::l1)
+                              ((Float.of_string (matched_group 2 x))::l2) 
               else 
                 (List.sort compare l1, List.sort compare l2) 
 

+ 3 - 3
1/solve_2.ml

@@ -6,9 +6,9 @@ let rec construct_list l1 l2 =
   let line = In_channel.input_line In_channel.stdin in
   match line with
   | None -> (l1, l2) 
-  | Some x -> if (Str.string_match (regexp {|\([0-9]+\) *\([0-9]+\)|}) x 0) then
-               construct_list ((Float.to_int (Float.of_string (Str.matched_group 1 x)))::l1)
-                              ((Float.to_int (Float.of_string (Str.matched_group 2 x)))::l2) 
+  | Some x -> if (string_match (regexp {|\([0-9]+\) *\([0-9]+\)|}) x 0) then
+               construct_list ((Float.to_int (Float.of_string (matched_group 1 x)))::l1)
+                              ((Float.to_int (Float.of_string (matched_group 2 x)))::l2) 
               else 
                 (l1, l2) 
 

+ 3 - 0
3/dune

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

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
3/input


+ 23 - 0
3/solve_1.ml

@@ -0,0 +1,23 @@
+open Stdio
+open Str
+
+let parse_mult_instruction line=
+  let r = regexp {|mul( *\([0-9]+\) *, *\([0-9]+\) *)|} in 
+  let rec aux pos accum=
+    try
+      let _ = search_forward r line pos in
+      let x = float_of_string (matched_group 1 line) in
+      let y = float_of_string (matched_group 2 line) in
+      aux (match_end ()) (accum + int_of_float (x *. y))
+    with Not_found -> accum
+  in
+    aux 0 0
+  ;;
+let rec solve accum =
+  let line = In_channel.input_line In_channel.stdin in
+  match line with
+  | None -> accum
+  | Some x -> solve (accum + parse_mult_instruction x)
+;;
+
+let () =  printf "Total: %d\n" (solve 0)

+ 58 - 0
3/solve_2.ml

@@ -0,0 +1,58 @@
+open Stdio
+open Str
+
+
+let rec construct_list l1=
+  let line = In_channel.input_line In_channel.stdin in
+  match line with
+  | None -> l1
+  | Some x -> let report = List.map int_of_string (Str.split (regexp {| |}) x) in
+              construct_list (report::l1)
+;;
+
+let monotony a b = if ((abs (a - b)) < 4) then 
+  (if (a < b) then "increasing" else 
+  (if (a > b) then "decreasing" else "ko"))
+else "ko"
+;;
+
+
+let rec is_safe status tolerance prev_element l = 
+if (tolerance < 0) then false else 
+(match status with
+| "ko"         -> (match l with
+                  | a :: b :: tl -> (is_safe (monotony prev_element a) (tolerance - 1) 0 (prev_element::b::tl)) ||
+                                    (is_safe (monotony a b) (tolerance - 1) 0 (b::tl))
+                  | _ :: [] | [] -> true)
+| "increasing" -> (match l with
+          | a :: b :: tl -> (if ((monotony a b) == "increasing") then (is_safe "increasing" tolerance a (b::tl)) 
+                            else ((is_safe "increasing" (tolerance-1) 0 (prev_element::b::tl)) || 
+                                  (is_safe "increasing" (tolerance-1) 0 (a::tl))))
+          | _ :: [] | [] -> true)
+| "decreasing" -> (match l with
+          | a :: b :: tl -> (if ((monotony a b) == "decreasing") then (is_safe  "decreasing" tolerance a (b::tl))
+                            else ((is_safe "decreasing" (tolerance-1) 0 (prev_element::b::tl)) || 
+                                  (is_safe "decreasing" (tolerance-1) 0 (a::tl))))
+          | _ :: [] | [] -> true)
+| "confirm_monotony"       -> (match l with
+                                | a :: b :: tl -> let initial_monotonity = (monotony prev_element a) in 
+                                                  let current_monotonity = (monotony a b) in 
+                                                  let is_foul = (if initial_monotonity == current_monotonity then 0 else 1) in
+                                                  (is_safe current_monotonity (tolerance - is_foul) a (a::tl)) ||
+                                                  (is_safe initial_monotonity (tolerance - is_foul) a (prev_element::b::tl))
+                                | _ :: [] | [] -> true)
+| "init" -> (match l with
+            | a :: b :: tl -> is_safe (if ((a == b) || (abs(a - b) >= 4)) then "ko" else "confirm_monotony") tolerance a (b::tl)
+            | _ :: [] | [] -> true)
+| _            -> true )
+;; 
+
+
+let rec solve accum = function
+  | hd :: tl -> if (is_safe "init" 1 0 hd) then solve (accum + 1) tl else solve accum tl
+  | [] -> accum
+;;
+
+let () =
+  let li = construct_list [] in
+    printf "Total: %d\n" (solve 0 li)

+ 1 - 1
README.md

@@ -44,7 +44,7 @@ replace `<part_number>` with 1 for Part 1 or 2 for Part 2
 |-----|--------|--------|
 | 1   | ✅     | ✅     |
 | 2   | ✅     | ✅     |
-| 3   |      | ⬜     |
+| 3   |      | ⬜     |
 | 4   | ⬜     | ⬜     |
 | ... | ...    | ...    |
 | 25  | ⬜     | ⬜     |

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels