From cdbf4be6fa793ca1286604bdc2a1bb5c9e6a197a Mon Sep 17 00:00:00 2001 From: DJ O'Leary Date: Mon, 1 Dec 2025 22:51:45 +0100 Subject: refactor(2025d1p1): rename files to order correctly and include part number --- 2025/ocaml/lib/application/d01p01.ml | 49 +++++++++++++++++++ 2025/ocaml/lib/application/d01p01.mli | 4 ++ 2025/ocaml/lib/application/day_1.ml | 49 ------------------- 2025/ocaml/lib/application/day_1.mli | 4 -- 2025/ocaml/lib/domain/d01p01.ml | 76 +++++++++++++++++++++++++++++ 2025/ocaml/lib/domain/day_1.ml | 76 ----------------------------- 2025/ocaml/test/application/dune | 2 +- 2025/ocaml/test/application/test_d01p01.ml | 21 ++++++++ 2025/ocaml/test/application/test_example.ml | 0 2025/ocaml/test/domain/dune | 2 +- 2025/ocaml/test/domain/test_d01p01.ml | 29 +++++++++++ 2025/ocaml/test/domain/test_example.ml | 0 12 files changed, 181 insertions(+), 131 deletions(-) create mode 100644 2025/ocaml/lib/application/d01p01.ml create mode 100644 2025/ocaml/lib/application/d01p01.mli delete mode 100644 2025/ocaml/lib/application/day_1.ml delete mode 100644 2025/ocaml/lib/application/day_1.mli create mode 100644 2025/ocaml/lib/domain/d01p01.ml delete mode 100644 2025/ocaml/lib/domain/day_1.ml create mode 100644 2025/ocaml/test/application/test_d01p01.ml delete mode 100644 2025/ocaml/test/application/test_example.ml create mode 100644 2025/ocaml/test/domain/test_d01p01.ml delete mode 100644 2025/ocaml/test/domain/test_example.ml (limited to '2025') diff --git a/2025/ocaml/lib/application/d01p01.ml b/2025/ocaml/lib/application/d01p01.ml new file mode 100644 index 0000000..75e1b17 --- /dev/null +++ b/2025/ocaml/lib/application/d01p01.ml @@ -0,0 +1,49 @@ +open Domain.D01p01 + +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 current = function + | [] -> + count + | h :: t -> + let current' = Dial.turn current h in + Logs.debug (fun m -> m "dial at %d" (Dial.to_int current')) ; + if Dial.to_int current' = 0 then turn_aux (count + 1) current' t + else turn_aux count current' 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/d01p01.mli b/2025/ocaml/lib/application/d01p01.mli new file mode 100644 index 0000000..73631f1 --- /dev/null +++ b/2025/ocaml/lib/application/d01p01.mli @@ -0,0 +1,4 @@ +val solve : string -> unit -> int +(** [solve input ()] is the solution for the first day of advent of code 2025 for [input]. + The solution for an empty input is [Domain.Day_1.Dial.(to_int start)]. + Requires: [input] to be a multiline string using `\n` as it's line separator. *) diff --git a/2025/ocaml/lib/application/day_1.ml b/2025/ocaml/lib/application/day_1.ml deleted file mode 100644 index bc0d5cc..0000000 --- a/2025/ocaml/lib/application/day_1.ml +++ /dev/null @@ -1,49 +0,0 @@ -open Domain.Day_1 - -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 current = function - | [] -> - count - | h :: t -> - let current' = Dial.turn current h in - Logs.debug (fun m -> m "dial at %d" (Dial.to_int current')) ; - if Dial.to_int current' = 0 then turn_aux (count + 1) current' t - else turn_aux count current' 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/day_1.mli b/2025/ocaml/lib/application/day_1.mli deleted file mode 100644 index 73631f1..0000000 --- a/2025/ocaml/lib/application/day_1.mli +++ /dev/null @@ -1,4 +0,0 @@ -val solve : string -> unit -> int -(** [solve input ()] is the solution for the first day of advent of code 2025 for [input]. - The solution for an empty input is [Domain.Day_1.Dial.(to_int start)]. - Requires: [input] to be a multiline string using `\n` as it's line separator. *) diff --git a/2025/ocaml/lib/domain/d01p01.ml b/2025/ocaml/lib/domain/d01p01.ml new file mode 100644 index 0000000..6b531f4 --- /dev/null +++ b/2025/ocaml/lib/domain/d01p01.ml @@ -0,0 +1,76 @@ +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 turn : t -> direction -> t + (** [turn current direction] is the new state of the dial after turning [direction] from [current] *) + + 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 + + (** [turn current direction] is the new state of the dial after turning [direction] from [current]. + Raises: [dial_out_of_range] if the dial is moved to an invalid state. *) + let rec turn current direction = + match direction with + | Left 0 -> + current + | Left n -> + turn (prev current) (Left (n - 1)) + | Right 0 -> + current + | Right n -> + turn (next current) (Right (n - 1)) + + 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 diff --git a/2025/ocaml/lib/domain/day_1.ml b/2025/ocaml/lib/domain/day_1.ml deleted file mode 100644 index 6b531f4..0000000 --- a/2025/ocaml/lib/domain/day_1.ml +++ /dev/null @@ -1,76 +0,0 @@ -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 turn : t -> direction -> t - (** [turn current direction] is the new state of the dial after turning [direction] from [current] *) - - 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 - - (** [turn current direction] is the new state of the dial after turning [direction] from [current]. - Raises: [dial_out_of_range] if the dial is moved to an invalid state. *) - let rec turn current direction = - match direction with - | Left 0 -> - current - | Left n -> - turn (prev current) (Left (n - 1)) - | Right 0 -> - current - | Right n -> - turn (next current) (Right (n - 1)) - - 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 diff --git a/2025/ocaml/test/application/dune b/2025/ocaml/test/application/dune index 785d27e..fc384d8 100644 --- a/2025/ocaml/test/application/dune +++ b/2025/ocaml/test/application/dune @@ -1,6 +1,6 @@ (tests (names - test_example) + test_d01p01) (libraries application domain diff --git a/2025/ocaml/test/application/test_d01p01.ml b/2025/ocaml/test/application/test_d01p01.ml new file mode 100644 index 0000000..223fd2b --- /dev/null +++ b/2025/ocaml/test/application/test_d01p01.ml @@ -0,0 +1,21 @@ +open Application.D01p01 + +let example_data = {|L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82|} + +let test_full_given_example () = + Alcotest.(check int) "full given example" 3 (solve example_data ()) + +let () = + let open Alcotest in + run "Day_1" + [ ( "given-example" + , [test_case "full given example" `Quick test_full_given_example] ) ] diff --git a/2025/ocaml/test/application/test_example.ml b/2025/ocaml/test/application/test_example.ml deleted file mode 100644 index e69de29..0000000 diff --git a/2025/ocaml/test/domain/dune b/2025/ocaml/test/domain/dune index 8bd0a52..e3dda1a 100644 --- a/2025/ocaml/test/domain/dune +++ b/2025/ocaml/test/domain/dune @@ -1,6 +1,6 @@ (tests (names - test_example) + test_d01p01) (libraries domain alcotest)) diff --git a/2025/ocaml/test/domain/test_d01p01.ml b/2025/ocaml/test/domain/test_d01p01.ml new file mode 100644 index 0000000..e51d9a6 --- /dev/null +++ b/2025/ocaml/test/domain/test_d01p01.ml @@ -0,0 +1,29 @@ +open Domain.D01p01 + +let test_start () = + Alcotest.(check int) "incorrect starting state" 50 (Dial.to_int Dial.start) + +let test_turn desc exp dir = + Alcotest.(check int) desc exp (Dial.to_int (Dial.turn Dial.start 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 () = + let open Alcotest in + run "Day_1" + [ ("initial-state", [test_case "starting state" `Quick test_start]) + ; ( "turning-dial" + , [ 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 ] ) ] diff --git a/2025/ocaml/test/domain/test_example.ml b/2025/ocaml/test/domain/test_example.ml deleted file mode 100644 index e69de29..0000000 -- cgit v1.2.3