summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2025/ocaml/lib/application/d01p02.ml51
-rw-r--r--2025/ocaml/lib/application/d01p02.mli4
-rw-r--r--2025/ocaml/lib/domain/d01p02.ml95
-rw-r--r--2025/ocaml/test/application/dune3
-rw-r--r--2025/ocaml/test/application/test_d01p02.ml21
-rw-r--r--2025/ocaml/test/domain/dune3
-rw-r--r--2025/ocaml/test/domain/test_d01p02.ml41
7 files changed, 216 insertions, 2 deletions
diff --git a/2025/ocaml/lib/application/d01p02.ml b/2025/ocaml/lib/application/d01p02.ml
new file mode 100644
index 0000000..6f935a4
--- /dev/null
+++ b/2025/ocaml/lib/application/d01p02.ml
@@ -0,0 +1,51 @@
+open Domain.D01p02
+
+let rec input_to_lines input =
+ let lines =
+ String.split_on_char '\n' input |> List.filter (fun l -> l <> "")
+ in
+ pp_lines lines ; lines
+
+and pp_lines = function
+ | [] ->
+ ()
+ | h :: t ->
+ Logs.debug (fun m -> m "Line=%s" h) ;
+ pp_lines t
+
+let parse_line line =
+ Scanf.sscanf line "%c%d" (fun d n ->
+ match d with
+ | 'L' ->
+ Dial.Left n
+ | 'R' ->
+ Dial.Right n
+ | _ ->
+ failwith "invalid direction" )
+
+let lines_to_turns =
+ List.map (fun (l : string) ->
+ Logs.debug (fun m -> m "parsing %s into turn" l) ;
+ let turn = parse_line l in
+ Logs.debug (fun m -> m "%s" (Dial.pp_direction turn)) ;
+ turn )
+
+let rec turn_aux count dial = function
+ | [] ->
+ count
+ | h :: t -> (
+ let dial_and_count = Turner.turn dial count h in
+ match dial_and_count with
+ | dial', count' ->
+ Logs.debug (fun m ->
+ m "(dial=%d, count=%d)" (Dial.to_int dial') count' ) ;
+ turn_aux count' dial' t )
+
+let turn current = turn_aux 0 current
+
+let solve input () =
+ let lines = input_to_lines input in
+ let turns = lines_to_turns lines in
+ let start = Dial.start in
+ let answer = turn start turns in
+ answer
diff --git a/2025/ocaml/lib/application/d01p02.mli b/2025/ocaml/lib/application/d01p02.mli
new file mode 100644
index 0000000..33aa63f
--- /dev/null
+++ b/2025/ocaml/lib/application/d01p02.mli
@@ -0,0 +1,4 @@
+val solve : string -> unit -> int
+(** [solve input ()] is the solution for the second part of the first day of advent of code 2025 for [input].
+ The solution for an empty input is 0.
+ Requires: [input] to be a multiline string using `\n` as it's line separator. *)
diff --git a/2025/ocaml/lib/domain/d01p02.ml b/2025/ocaml/lib/domain/d01p02.ml
new file mode 100644
index 0000000..f71e965
--- /dev/null
+++ b/2025/ocaml/lib/domain/d01p02.ml
@@ -0,0 +1,95 @@
+module Dial : sig
+ (** [t] is a dial between 0 and 99 inclusive *)
+ type t
+
+ (** [direction] is a direction the dial can be turned *)
+ type direction = Left of int | Right of int
+
+ val start : t
+ (** [start] is the initial state of the dial *)
+
+ val dial_out_of_range : string
+ (** [dial_out_of_range] is the error message when the dial is out of range *)
+
+ val prev : t -> t
+
+ val next : t -> t
+
+ val is_zero : t -> bool
+
+ val to_string : t -> string
+ (** [to_string t] is the string representation of [t] *)
+
+ val to_int : t -> int
+ (** [to_int t] is the integer representation of [t] *)
+
+ val pp_direction : direction -> string
+end = struct
+ (** [t] is an int between 0 and 99 inclusive *)
+ type t = int
+
+ type direction = Left of int | Right of int
+
+ let start = 50
+
+ let dial_out_of_range = "dial out of range"
+
+ (** [next n] is the new state of the dial after turning [Right].
+ Raises: [dial_out_of_range] if [n] is greater than 99. *)
+ let next = function
+ | n when n < 99 ->
+ n + 1
+ | 99 ->
+ 0
+ | _ ->
+ failwith dial_out_of_range
+
+ (** [prev n] is the new state of the dial after turning [Left].
+ Raises: [dial_out_of_range] if [n] is less than 0. *)
+ let prev = function
+ | n when n > 0 ->
+ n - 1
+ | 0 ->
+ 99
+ | _ ->
+ failwith dial_out_of_range
+
+ let is_zero = function 0 -> true | _ -> false
+
+ let to_string = string_of_int
+
+ let to_int t = t
+
+ let pp_direction = function
+ | Left n ->
+ "Left " ^ string_of_int n
+ | Right n ->
+ "Right " ^ string_of_int n
+end
+
+module Turner : sig
+ val turn : Dial.t -> int -> Dial.direction -> Dial.t * int
+ (** [turn dial direction] is the new state of the dial after turning [direction] on [dial] *)
+end = struct
+ (** [turn dial direction] is the new state of the dial after turning [direction] on [dial].
+ Raises: [dial_out_of_range] if the dial is moved to an invalid state. *)
+ let rec turn dial zero_count direction =
+ let open Dial in
+ match direction with
+ | Left 0 ->
+ (dial, zero_count)
+ | Left n ->
+ let dial' = prev dial in
+ let zero_count' =
+ if Dial.is_zero dial' then zero_count + 1 else zero_count
+ in
+ turn dial' zero_count' (Left (n - 1))
+ | Right 0 ->
+ (dial, zero_count)
+ | Right n ->
+ let dial' = next dial in
+ let zero_count' =
+ if Dial.is_zero dial' then zero_count + 1 else zero_count
+ in
+ turn dial' zero_count' (Right (n - 1))
+end
diff --git a/2025/ocaml/test/application/dune b/2025/ocaml/test/application/dune
index fc384d8..53166ad 100644
--- a/2025/ocaml/test/application/dune
+++ b/2025/ocaml/test/application/dune
@@ -1,6 +1,7 @@
(tests
(names
- test_d01p01)
+ test_d01p01
+ test_d01p02)
(libraries
application
domain
diff --git a/2025/ocaml/test/application/test_d01p02.ml b/2025/ocaml/test/application/test_d01p02.ml
new file mode 100644
index 0000000..e29c932
--- /dev/null
+++ b/2025/ocaml/test/application/test_d01p02.ml
@@ -0,0 +1,21 @@
+open Application.D01p02
+
+let example_data = {|L68
+L30
+R48
+L5
+R60
+L55
+L1
+L99
+R14
+L82|}
+
+let test_full_given_example () =
+ Alcotest.(check int) "full given example" 6 (solve example_data ())
+
+let () =
+ let open Alcotest in
+ run "D01p02"
+ [ ( "given-example"
+ , [test_case "full given example" `Quick test_full_given_example] ) ]
diff --git a/2025/ocaml/test/domain/dune b/2025/ocaml/test/domain/dune
index e3dda1a..7630ea4 100644
--- a/2025/ocaml/test/domain/dune
+++ b/2025/ocaml/test/domain/dune
@@ -1,6 +1,7 @@
(tests
(names
- test_d01p01)
+ test_d01p01
+ test_d01p02)
(libraries
domain
alcotest))
diff --git a/2025/ocaml/test/domain/test_d01p02.ml b/2025/ocaml/test/domain/test_d01p02.ml
new file mode 100644
index 0000000..98c06c3
--- /dev/null
+++ b/2025/ocaml/test/domain/test_d01p02.ml
@@ -0,0 +1,41 @@
+open Domain.D01p02
+
+let test_start () =
+ Alcotest.(check int) "incorrect starting state" 50 (Dial.to_int Dial.start)
+
+let test_turn errmsg exp dir =
+ Alcotest.(check int)
+ errmsg exp
+ (Dial.to_int (fst (Turner.turn Dial.start 0 dir)))
+
+let test_turn_left_one () =
+ test_turn "incorrect result when turning left one step" 49 (Dial.Left 1)
+
+let test_turn_left_100 () =
+ test_turn "incorrect result when turning left 100 steps" 50 (Dial.Left 100)
+
+let test_turn_right_one () =
+ test_turn "incorrect result when turning right one step" 51 (Dial.Right 1)
+
+let test_turn_right_100 () =
+ test_turn "incorrect result when turning right 100 steps" 50 (Dial.Right 100)
+
+let test_zero_count errmsg exp dir =
+ Alcotest.(check int) errmsg exp (snd (Turner.turn Dial.start 0 dir))
+
+let test_zero_count_right_1000 () =
+ test_zero_count "zero MUST be counted each time it is passed" 10
+ (Dial.Right 1000)
+
+let () =
+ let open Alcotest in
+ run "D01p02"
+ [ ("initial-state", [test_case "starting state" `Quick test_start])
+ ; ( "turning-dial-regression"
+ , [ test_case "turning left one step" `Quick test_turn_left_one
+ ; test_case "turning right one step" `Quick test_turn_right_one
+ ; test_case "turning left 100 steps" `Quick test_turn_left_100
+ ; test_case "turning right 100 steps" `Quick test_turn_right_100 ] )
+ ; ( "zero-count"
+ , [ test_case "zero count after turning right 1000 steps" `Quick
+ test_zero_count_right_1000 ] ) ]