diff options
| author | DJ O'Leary <dijitol@proton.me> | 2026-06-27 00:11:43 +0200 |
|---|---|---|
| committer | DJ O'Leary <dijitol@proton.me> | 2026-06-27 00:11:43 +0200 |
| commit | 976048a85403fbb0751c34fa42703a3c47f6e918 (patch) | |
| tree | 46e321d8120c45e318ead66f60165b0b854c85a1 /ocaml/lib | |
| parent | 6d8fb621d1e93b83786890d6d9cb6f0d8defa8ae (diff) | |
refactor!: change repo directory structure for ocaml
Diffstat (limited to 'ocaml/lib')
| -rw-r--r-- | ocaml/lib/application/d05p01.ml | 3 | ||||
| -rw-r--r-- | ocaml/lib/application/d05p01.mli | 4 | ||||
| -rw-r--r-- | ocaml/lib/infrastructure/dune | 15 | ||||
| -rw-r--r-- | ocaml/lib/infrastructure/file.mli | 3 | ||||
| -rw-r--r-- | ocaml/lib/presentation/cli.ml | 62 | ||||
| -rw-r--r-- | ocaml/lib/presentation/dune | 16 | ||||
| -rw-r--r-- | ocaml/lib/shared/dune (renamed from ocaml/lib/domain/dune) | 9 | ||||
| -rw-r--r-- | ocaml/lib/shared/file.ml (renamed from ocaml/lib/infrastructure/file.ml) | 2 | ||||
| -rw-r--r-- | ocaml/lib/shared/matrix.ml (renamed from ocaml/lib/domain/matrix.ml) | 0 | ||||
| -rw-r--r-- | ocaml/lib/shared/parser.ml (renamed from ocaml/lib/application/parser.ml) | 0 | ||||
| -rw-r--r-- | ocaml/lib/shared/range.ml (renamed from ocaml/lib/domain/range.ml) | 0 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day01/part01/dial.ml (renamed from ocaml/lib/domain/d01p01.ml) | 0 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day01/part01/solver.ml (renamed from ocaml/lib/application/d01p01.ml) | 2 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day01/part01/solver.mli (renamed from ocaml/lib/application/d01p01.mli) | 0 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day01/part02/dial.ml (renamed from ocaml/lib/domain/d01p02.ml) | 27 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day01/part02/solver.ml (renamed from ocaml/lib/application/d01p02.ml) | 3 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day01/part02/solver.mli (renamed from ocaml/lib/application/d01p02.mli) | 0 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day01/part02/turner.ml | 28 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day02/part01/id.ml | 16 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day02/part01/id.mli (renamed from ocaml/lib/domain/d02p01.mli) | 14 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day02/part01/range.ml (renamed from ocaml/lib/domain/d02p02.ml) | 13 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day02/part01/range.mli | 15 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day02/part01/solver.ml (renamed from ocaml/lib/application/d02p01.ml) | 3 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day02/part01/solver.mli (renamed from ocaml/lib/application/d02p01.mli) | 0 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day02/part02/id.ml | 12 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day02/part02/id.mli (renamed from ocaml/lib/domain/d02p02.mli) | 14 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day02/part02/range.ml (renamed from ocaml/lib/domain/d02p01.ml) | 17 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day02/part02/range.mli | 15 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day02/part02/solver.ml (renamed from ocaml/lib/application/d02p02.ml) | 3 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day02/part02/solver.mli (renamed from ocaml/lib/application/d02p02.mli) | 0 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day03/part01/battery.ml | 13 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day03/part01/battery.mli | 13 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day03/part01/battery_bank.ml (renamed from ocaml/lib/domain/d03p01.ml) | 43 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day03/part01/battery_bank.mli | 18 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day03/part01/solver.ml (renamed from ocaml/lib/application/d03p01.ml) | 2 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day03/part01/solver.mli (renamed from ocaml/lib/application/d03p01.mli) | 0 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day03/part02/battery.ml | 25 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day03/part02/battery.mli | 13 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day03/part02/battery_bank.ml (renamed from ocaml/lib/domain/d03p02.ml) | 43 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day03/part02/battery_bank.mli | 18 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day03/part02/solver.ml (renamed from ocaml/lib/application/d03p02.ml) | 2 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day03/part02/solver.mli (renamed from ocaml/lib/application/d03p02.mli) | 0 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day04/part01/solver.ml (renamed from ocaml/lib/application/d04p01.ml) | 10 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day04/part01/solver.mli (renamed from ocaml/lib/application/d04p01.mli) | 0 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day04/part02/solver.ml (renamed from ocaml/lib/application/d04p02.ml) | 14 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day04/part02/solver.mli (renamed from ocaml/lib/application/d04p02.mli) | 0 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day06/part01/solver.ml (renamed from ocaml/lib/application/d06p01.ml) | 4 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day06/part01/solver.mli (renamed from ocaml/lib/application/d06p01.mli) | 0 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day06/part02/solver.ml (renamed from ocaml/lib/application/d06p02.ml) | 4 | ||||
| -rw-r--r-- | ocaml/lib/year2025/day06/part02/solver.mli (renamed from ocaml/lib/application/d06p02.mli) | 0 | ||||
| -rw-r--r-- | ocaml/lib/year2025/dune (renamed from ocaml/lib/application/dune) | 10 |
51 files changed, 229 insertions, 299 deletions
diff --git a/ocaml/lib/application/d05p01.ml b/ocaml/lib/application/d05p01.ml deleted file mode 100644 index 4bf79ab..0000000 --- a/ocaml/lib/application/d05p01.ml +++ /dev/null @@ -1,3 +0,0 @@ -let err_empty_input = "input is empty" - -let solve input () = if input = "" then Error err_empty_input else Ok 0 diff --git a/ocaml/lib/application/d05p01.mli b/ocaml/lib/application/d05p01.mli deleted file mode 100644 index 85c3dd5..0000000 --- a/ocaml/lib/application/d05p01.mli +++ /dev/null @@ -1,4 +0,0 @@ -val solve : string -> unit -> (int, string) result -(** [solve input ()] is the solution for the first part of the five day of advent of code 2025 for [input]. - The solution for an empty input is [Error msg]. - Requires: [input] to be a string. *) diff --git a/ocaml/lib/infrastructure/dune b/ocaml/lib/infrastructure/dune deleted file mode 100644 index b9d250f..0000000 --- a/ocaml/lib/infrastructure/dune +++ /dev/null @@ -1,15 +0,0 @@ -(library - (name infrastructure) - (libraries - application - domain - fmt - logs) - (inline_tests) - (preprocess - (pps - ppx_deriving.show - ppx_deriving.eq - ppx_deriving.ord - ppx_inline_test - ppx_expect))) diff --git a/ocaml/lib/infrastructure/file.mli b/ocaml/lib/infrastructure/file.mli deleted file mode 100644 index 17d4495..0000000 --- a/ocaml/lib/infrastructure/file.mli +++ /dev/null @@ -1,3 +0,0 @@ -val read : string -> string -(** [read filename] is the contents of [filename] as a string. - The empty string is returned if [filename] does not exist. *) diff --git a/ocaml/lib/presentation/cli.ml b/ocaml/lib/presentation/cli.ml deleted file mode 100644 index 7864e7f..0000000 --- a/ocaml/lib/presentation/cli.ml +++ /dev/null @@ -1,62 +0,0 @@ -(* Business logic behind CLI *) - -let run_solver name solver filepath = - let content = Infrastructure.File.read filepath in - match solver content () with - | Error msg -> - Logs.app (fun m -> m "[%s] Err=%s" name msg) - | Ok answer -> - Logs.app (fun m -> m "[%s] ANSWER=%d" name answer) - -let run_day _ = function - | 1 -> - let filepath = "../_input/day_01.txt" in - run_solver "D01P01" Application.D01p01.solve filepath ; - run_solver "D01P02" Application.D01p02.solve filepath ; - 0 - | 2 -> - let filepath = "../_input/day_02.txt" in - run_solver "D02P01" Application.D02p01.solve filepath ; - run_solver "D02P02" Application.D02p02.solve filepath ; - 0 - | 3 -> - let filepath = "../_input/day_03.txt" in - run_solver "D03P01" Application.D03p01.solve filepath ; - run_solver "D03P02" Application.D03p02.solve filepath ; - 0 - | 4 -> - let filepath = "../_input/day_04.txt" in - run_solver "D04P01" Application.D04p01.solve filepath ; - run_solver "D04P02" Application.D04p02.solve filepath ; - 0 - | 5 -> - let filepath = "../_input/day_05.txt" in - run_solver "D05P01" Application.D05p01.solve filepath ; - 0 - | 6 -> - let filepath = "../_input/day_06.txt" in - run_solver "D06P01" Application.D06p01.solve filepath ; - run_solver "D06P02" Application.D06p02.solve filepath ; - 0 - | _ -> - failwith "day not implemented" - -(* Command line interface *) - -open Cmdliner - -let setup_log style_renderer = Fmt_tty.setup_std_outputs ?style_renderer () - -let setup_log_t = Term.(const setup_log $ Fmt_cli.style_renderer ()) - -let day_arg = - let doc = "Day to run. MUST be in range of 1-12." in - Arg.(value & pos 0 int 1 & info [] ~docv:"DAY" ~doc) - -let day_t = Term.(const run_day $ setup_log_t $ day_arg) - -let cmd = - let info = Cmd.info "aoc" ~version:"0.1.0" in - Cmd.v info day_t - -let run () = Cmd.eval' cmd diff --git a/ocaml/lib/presentation/dune b/ocaml/lib/presentation/dune deleted file mode 100644 index ae1fcb1..0000000 --- a/ocaml/lib/presentation/dune +++ /dev/null @@ -1,16 +0,0 @@ -(library - (name presentation) - (libraries - application - domain - infrastructure - cmdliner - fmt.cli - fmt.tty) - (inline_tests) - (preprocess - (pps - ppx_deriving.show - ppx_deriving.eq - ppx_deriving.ord - ppx_expect))) diff --git a/ocaml/lib/domain/dune b/ocaml/lib/shared/dune index 5b13d31..f71a5fe 100644 --- a/ocaml/lib/domain/dune +++ b/ocaml/lib/shared/dune @@ -1,13 +1,10 @@ (library - (name domain) + (name shared) (inline_tests) - (libraries - fmt - logs) (preprocess (pps ppx_deriving.show ppx_deriving.eq ppx_deriving.ord - ppx_inline_test - ppx_expect))) + ppx_expect + ppx_inline_test))) diff --git a/ocaml/lib/infrastructure/file.ml b/ocaml/lib/shared/file.ml index 3b49c01..74b5abc 100644 --- a/ocaml/lib/infrastructure/file.ml +++ b/ocaml/lib/shared/file.ml @@ -1,3 +1,5 @@ +(** [read filename] is the contents of [filename] as a string. + The empty string is returned if [filename] does not exist. *) let read filename = let chan = In_channel.open_text filename in let content = In_channel.input_all chan in diff --git a/ocaml/lib/domain/matrix.ml b/ocaml/lib/shared/matrix.ml index fdf476f..fdf476f 100644 --- a/ocaml/lib/domain/matrix.ml +++ b/ocaml/lib/shared/matrix.ml diff --git a/ocaml/lib/application/parser.ml b/ocaml/lib/shared/parser.ml index 099900b..099900b 100644 --- a/ocaml/lib/application/parser.ml +++ b/ocaml/lib/shared/parser.ml diff --git a/ocaml/lib/domain/range.ml b/ocaml/lib/shared/range.ml index a1bdc00..a1bdc00 100644 --- a/ocaml/lib/domain/range.ml +++ b/ocaml/lib/shared/range.ml diff --git a/ocaml/lib/domain/d01p01.ml b/ocaml/lib/year2025/day01/part01/dial.ml index 6b531f4..6b531f4 100644 --- a/ocaml/lib/domain/d01p01.ml +++ b/ocaml/lib/year2025/day01/part01/dial.ml diff --git a/ocaml/lib/application/d01p01.ml b/ocaml/lib/year2025/day01/part01/solver.ml index 206e757..f4f0dde 100644 --- a/ocaml/lib/application/d01p01.ml +++ b/ocaml/lib/year2025/day01/part01/solver.ml @@ -1,4 +1,4 @@ -open Domain.D01p01 +open Dial let err_empty_input = "input is empty" diff --git a/ocaml/lib/application/d01p01.mli b/ocaml/lib/year2025/day01/part01/solver.mli index 2eecf19..2eecf19 100644 --- a/ocaml/lib/application/d01p01.mli +++ b/ocaml/lib/year2025/day01/part01/solver.mli diff --git a/ocaml/lib/domain/d01p02.ml b/ocaml/lib/year2025/day01/part02/dial.ml index f71e965..e061d6d 100644 --- a/ocaml/lib/domain/d01p02.ml +++ b/ocaml/lib/year2025/day01/part02/dial.ml @@ -66,30 +66,3 @@ end = struct | 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/ocaml/lib/application/d01p02.ml b/ocaml/lib/year2025/day01/part02/solver.ml index 7910486..452209f 100644 --- a/ocaml/lib/application/d01p02.ml +++ b/ocaml/lib/year2025/day01/part02/solver.ml @@ -1,4 +1,5 @@ -open Domain.D01p02 +open Dial +open Turner let err_empty_input = "input is empty" diff --git a/ocaml/lib/application/d01p02.mli b/ocaml/lib/year2025/day01/part02/solver.mli index 9e9a2f2..9e9a2f2 100644 --- a/ocaml/lib/application/d01p02.mli +++ b/ocaml/lib/year2025/day01/part02/solver.mli diff --git a/ocaml/lib/year2025/day01/part02/turner.ml b/ocaml/lib/year2025/day01/part02/turner.ml new file mode 100644 index 0000000..7477093 --- /dev/null +++ b/ocaml/lib/year2025/day01/part02/turner.ml @@ -0,0 +1,28 @@ +open Dial + +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/ocaml/lib/year2025/day02/part01/id.ml b/ocaml/lib/year2025/day02/part01/id.ml new file mode 100644 index 0000000..b7a3bd2 --- /dev/null +++ b/ocaml/lib/year2025/day02/part01/id.ml @@ -0,0 +1,16 @@ +module ID = struct + (** *) + type t = int + + let create num = num + + let validate rules id = List.fold_left (fun acc r -> acc && r id) true rules + + let incr = ( + ) 1 + + let to_int id = id + + let to_string = string_of_int + + let pp id = "ID=" ^ string_of_int id +end diff --git a/ocaml/lib/domain/d02p01.mli b/ocaml/lib/year2025/day02/part01/id.mli index 55665fe..63d5942 100644 --- a/ocaml/lib/domain/d02p01.mli +++ b/ocaml/lib/year2025/day02/part01/id.mli @@ -20,17 +20,3 @@ module ID : sig val pp : t -> string (** [pp id] is a pretty string representing [id] *) end - -module Range : sig - (** [t] is a range of values *) - type t - - val create : int * int -> t - (** [create (s, e)] is a range starting at [s] and ending at [e]. *) - - val collect : t -> ID.t list - (** [collect r] is a list containing all elements within the range (inclusive). *) - - val pp : t -> string - (** [pp r] is a pretty string representing [r] *) -end diff --git a/ocaml/lib/domain/d02p02.ml b/ocaml/lib/year2025/day02/part01/range.ml index b2b55f1..ba27568 100644 --- a/ocaml/lib/domain/d02p02.ml +++ b/ocaml/lib/year2025/day02/part01/range.ml @@ -1,15 +1,4 @@ -module ID = struct - (** *) - type t = int - - let create num = num - - let incr = ( + ) 1 - - let to_string = string_of_int - - let pp id = "ID=" ^ string_of_int id -end +open Id module Range = struct type t = ID.t * ID.t diff --git a/ocaml/lib/year2025/day02/part01/range.mli b/ocaml/lib/year2025/day02/part01/range.mli new file mode 100644 index 0000000..e525e43 --- /dev/null +++ b/ocaml/lib/year2025/day02/part01/range.mli @@ -0,0 +1,15 @@ +open Id + +module Range : sig + (** [t] is a range of values *) + type t + + val create : int * int -> t + (** [create (s, e)] is a range starting at [s] and ending at [e]. *) + + val collect : t -> ID.t list + (** [collect r] is a list containing all elements within the range (inclusive). *) + + val pp : t -> string + (** [pp r] is a pretty string representing [r] *) +end diff --git a/ocaml/lib/application/d02p01.ml b/ocaml/lib/year2025/day02/part01/solver.ml index f313149..417aa0d 100644 --- a/ocaml/lib/application/d02p01.ml +++ b/ocaml/lib/year2025/day02/part01/solver.ml @@ -1,4 +1,5 @@ -open Domain.D02p01 +open Id +open Range let err_empty_input = "input is empty" diff --git a/ocaml/lib/application/d02p01.mli b/ocaml/lib/year2025/day02/part01/solver.mli index 8a8eb32..8a8eb32 100644 --- a/ocaml/lib/application/d02p01.mli +++ b/ocaml/lib/year2025/day02/part01/solver.mli diff --git a/ocaml/lib/year2025/day02/part02/id.ml b/ocaml/lib/year2025/day02/part02/id.ml new file mode 100644 index 0000000..f26c395 --- /dev/null +++ b/ocaml/lib/year2025/day02/part02/id.ml @@ -0,0 +1,12 @@ +module ID = struct + (** *) + type t = int + + let create num = num + + let incr = ( + ) 1 + + let to_string = string_of_int + + let pp id = "ID=" ^ string_of_int id +end diff --git a/ocaml/lib/domain/d02p02.mli b/ocaml/lib/year2025/day02/part02/id.mli index f918ae1..a512593 100644 --- a/ocaml/lib/domain/d02p02.mli +++ b/ocaml/lib/year2025/day02/part02/id.mli @@ -14,17 +14,3 @@ module ID : sig val pp : t -> string (** [pp id] is a pretty string representing [id] *) end - -module Range : sig - (** [t] is a range of values *) - type t - - val create : int * int -> t - (** [create (s, e)] is a range starting at [s] and ending at [e]. *) - - val collect : t -> ID.t list - (** [collect r] is a list containing all elements within the range (inclusive). *) - - val pp : t -> string - (** [pp r] is a pretty string representing [r] *) -end diff --git a/ocaml/lib/domain/d02p01.ml b/ocaml/lib/year2025/day02/part02/range.ml index 725d4b1..ba27568 100644 --- a/ocaml/lib/domain/d02p01.ml +++ b/ocaml/lib/year2025/day02/part02/range.ml @@ -1,19 +1,4 @@ -module ID = struct - (** *) - type t = int - - let create num = num - - let validate rules id = List.fold_left (fun acc r -> acc && r id) true rules - - let incr = ( + ) 1 - - let to_int id = id - - let to_string = string_of_int - - let pp id = "ID=" ^ string_of_int id -end +open Id module Range = struct type t = ID.t * ID.t diff --git a/ocaml/lib/year2025/day02/part02/range.mli b/ocaml/lib/year2025/day02/part02/range.mli new file mode 100644 index 0000000..e525e43 --- /dev/null +++ b/ocaml/lib/year2025/day02/part02/range.mli @@ -0,0 +1,15 @@ +open Id + +module Range : sig + (** [t] is a range of values *) + type t + + val create : int * int -> t + (** [create (s, e)] is a range starting at [s] and ending at [e]. *) + + val collect : t -> ID.t list + (** [collect r] is a list containing all elements within the range (inclusive). *) + + val pp : t -> string + (** [pp r] is a pretty string representing [r] *) +end diff --git a/ocaml/lib/application/d02p02.ml b/ocaml/lib/year2025/day02/part02/solver.ml index d062f7b..f5fdf0c 100644 --- a/ocaml/lib/application/d02p02.ml +++ b/ocaml/lib/year2025/day02/part02/solver.ml @@ -1,4 +1,5 @@ -open Domain.D02p02 +open Id +open Range (* Errors *) diff --git a/ocaml/lib/application/d02p02.mli b/ocaml/lib/year2025/day02/part02/solver.mli index 8a8eb32..8a8eb32 100644 --- a/ocaml/lib/application/d02p02.mli +++ b/ocaml/lib/year2025/day02/part02/solver.mli diff --git a/ocaml/lib/year2025/day03/part01/battery.ml b/ocaml/lib/year2025/day03/part01/battery.ml new file mode 100644 index 0000000..cb2d9e5 --- /dev/null +++ b/ocaml/lib/year2025/day03/part01/battery.ml @@ -0,0 +1,13 @@ +module Battery = struct + type t = int + + let of_int i = i + + let to_int b = b + + let to_string = string_of_int + + let compare = Int.compare + + let pp = string_of_int +end diff --git a/ocaml/lib/year2025/day03/part01/battery.mli b/ocaml/lib/year2025/day03/part01/battery.mli new file mode 100644 index 0000000..e00940b --- /dev/null +++ b/ocaml/lib/year2025/day03/part01/battery.mli @@ -0,0 +1,13 @@ +module Battery : sig + type t + + val of_int : int -> t + + val to_int : t -> int + + val to_string : t -> string + + val compare : t -> t -> int + + val pp : t -> string +end diff --git a/ocaml/lib/domain/d03p01.ml b/ocaml/lib/year2025/day03/part01/battery_bank.ml index 1367712..7ebce75 100644 --- a/ocaml/lib/domain/d03p01.ml +++ b/ocaml/lib/year2025/day03/part01/battery_bank.ml @@ -1,45 +1,6 @@ -module Battery : sig - type t +open Battery - val of_int : int -> t - - val to_int : t -> int - - val to_string : t -> string - - val compare : t -> t -> int - - val pp : t -> string -end = struct - type t = int - - let of_int i = i - - let to_int b = b - - let to_string = string_of_int - - let compare = Int.compare - - let pp = string_of_int -end - -module BatteryBank : sig - type t - - val highest_joltage : t -> int - (** [highest_joltage bb] is an integer created from the two highest - digits in [bb]. - Raises: [Assert_failure] if [bb] is empty. *) - - val of_string : string -> t - - val of_list : Battery.t list -> t - - val to_list : t -> Battery.t list - - val pp : t -> string -end = struct +module BatteryBank = struct type t = Battery.t list let rec highest_joltage bb = diff --git a/ocaml/lib/year2025/day03/part01/battery_bank.mli b/ocaml/lib/year2025/day03/part01/battery_bank.mli new file mode 100644 index 0000000..bffc4cb --- /dev/null +++ b/ocaml/lib/year2025/day03/part01/battery_bank.mli @@ -0,0 +1,18 @@ +open Battery + +module BatteryBank : sig + type t + + val highest_joltage : t -> int + (** [highest_joltage bb] is an integer created from the two highest + digits in [bb]. + Raises: [Assert_failure] if [bb] is empty. *) + + val of_string : string -> t + + val of_list : Battery.t list -> t + + val to_list : t -> Battery.t list + + val pp : t -> string +end diff --git a/ocaml/lib/application/d03p01.ml b/ocaml/lib/year2025/day03/part01/solver.ml index 522f5d0..4a06e83 100644 --- a/ocaml/lib/application/d03p01.ml +++ b/ocaml/lib/year2025/day03/part01/solver.ml @@ -1,4 +1,4 @@ -open Domain.D03p01 +open Battery_bank let err_empty_input = "input is empty" diff --git a/ocaml/lib/application/d03p01.mli b/ocaml/lib/year2025/day03/part01/solver.mli index 31dd0cd..31dd0cd 100644 --- a/ocaml/lib/application/d03p01.mli +++ b/ocaml/lib/year2025/day03/part01/solver.mli diff --git a/ocaml/lib/year2025/day03/part02/battery.ml b/ocaml/lib/year2025/day03/part02/battery.ml new file mode 100644 index 0000000..e8f0443 --- /dev/null +++ b/ocaml/lib/year2025/day03/part02/battery.ml @@ -0,0 +1,25 @@ +module Battery : sig + type t + + val of_int : int -> t + + val to_int : t -> int + + val to_string : t -> string + + val compare : t -> t -> int + + val pp : t -> string +end = struct + type t = int + + let of_int i = i + + let to_int b = b + + let to_string = string_of_int + + let compare = Int.compare + + let pp = string_of_int +end diff --git a/ocaml/lib/year2025/day03/part02/battery.mli b/ocaml/lib/year2025/day03/part02/battery.mli new file mode 100644 index 0000000..e00940b --- /dev/null +++ b/ocaml/lib/year2025/day03/part02/battery.mli @@ -0,0 +1,13 @@ +module Battery : sig + type t + + val of_int : int -> t + + val to_int : t -> int + + val to_string : t -> string + + val compare : t -> t -> int + + val pp : t -> string +end diff --git a/ocaml/lib/domain/d03p02.ml b/ocaml/lib/year2025/day03/part02/battery_bank.ml index 88257d0..d7dc213 100644 --- a/ocaml/lib/domain/d03p02.ml +++ b/ocaml/lib/year2025/day03/part02/battery_bank.ml @@ -1,45 +1,6 @@ -module Battery : sig - type t +open Battery - val of_int : int -> t - - val to_int : t -> int - - val to_string : t -> string - - val compare : t -> t -> int - - val pp : t -> string -end = struct - type t = int - - let of_int i = i - - let to_int b = b - - let to_string = string_of_int - - let compare = Int.compare - - let pp = string_of_int -end - -module BatteryBank : sig - type t - - val highest_joltage : t -> int - (** [highest_joltage bb] is an integer created from the two highest - digits in [bb]. - Raises: [Assert_failure] if [bb] is empty. *) - - val of_string : string -> t - - val of_list : Battery.t list -> t - - val to_list : t -> Battery.t list - - val pp : t -> string -end = struct +module BatteryBank = struct type t = Battery.t list type highest = {one: Battery.t option; two: Battery.t option} diff --git a/ocaml/lib/year2025/day03/part02/battery_bank.mli b/ocaml/lib/year2025/day03/part02/battery_bank.mli new file mode 100644 index 0000000..bffc4cb --- /dev/null +++ b/ocaml/lib/year2025/day03/part02/battery_bank.mli @@ -0,0 +1,18 @@ +open Battery + +module BatteryBank : sig + type t + + val highest_joltage : t -> int + (** [highest_joltage bb] is an integer created from the two highest + digits in [bb]. + Raises: [Assert_failure] if [bb] is empty. *) + + val of_string : string -> t + + val of_list : Battery.t list -> t + + val to_list : t -> Battery.t list + + val pp : t -> string +end diff --git a/ocaml/lib/application/d03p02.ml b/ocaml/lib/year2025/day03/part02/solver.ml index 743c658..4a06e83 100644 --- a/ocaml/lib/application/d03p02.ml +++ b/ocaml/lib/year2025/day03/part02/solver.ml @@ -1,4 +1,4 @@ -open Domain.D03p02 +open Battery_bank let err_empty_input = "input is empty" diff --git a/ocaml/lib/application/d03p02.mli b/ocaml/lib/year2025/day03/part02/solver.mli index 93d3b98..93d3b98 100644 --- a/ocaml/lib/application/d03p02.mli +++ b/ocaml/lib/year2025/day03/part02/solver.mli diff --git a/ocaml/lib/application/d04p01.ml b/ocaml/lib/year2025/day04/part01/solver.ml index 7c69de4..7cc54f6 100644 --- a/ocaml/lib/application/d04p01.ml +++ b/ocaml/lib/year2025/day04/part01/solver.ml @@ -1,21 +1,21 @@ -open Domain - let err_empty_input = "input is empty" let rec solve input () = if input = "" then Error err_empty_input else - let matrix = Matrix.M.of_string input in + let matrix = Shared.Matrix.M.of_string input in let count_paper lst = List.length @@ List.filter (fun c -> c <> ".") lst in let accessible_rolls_of_paper = ref 0 in for y = 0 to Array.length matrix - 1 do for x = 0 to Array.length matrix.(y) - 1 do if matrix.(y).(x) = "." then () else - let neighbour_coords = Matrix.M.get_valid_neighbours matrix x y in + let neighbour_coords = + Shared.Matrix.M.get_valid_neighbours matrix x y + in let neighbours = List.map - (fun (x', y') -> Matrix.M.get matrix x' y') + (fun (x', y') -> Shared.Matrix.M.get matrix x' y') neighbour_coords in let paper_count = count_paper neighbours in diff --git a/ocaml/lib/application/d04p01.mli b/ocaml/lib/year2025/day04/part01/solver.mli index 873acff..873acff 100644 --- a/ocaml/lib/application/d04p01.mli +++ b/ocaml/lib/year2025/day04/part01/solver.mli diff --git a/ocaml/lib/application/d04p02.ml b/ocaml/lib/year2025/day04/part02/solver.ml index 75b1236..e92c982 100644 --- a/ocaml/lib/application/d04p02.ml +++ b/ocaml/lib/year2025/day04/part02/solver.ml @@ -1,5 +1,3 @@ -open Domain - let err_empty_input = "input is empty" let rec count_accessible_rolls matrix = @@ -21,9 +19,13 @@ and count_accessible_rolls_aux matrix count_paper accessible_rolls_of_paper = for x = 0 to Array.length matrix.(y) - 1 do if matrix.(y).(x) = "." then () else - let neighbour_coords = Matrix.M.get_valid_neighbours matrix x y in + let neighbour_coords = + Shared.Matrix.M.get_valid_neighbours matrix x y + in let neighbours = - List.map (fun (nx, ny) -> Matrix.M.get matrix nx ny) neighbour_coords + List.map + (fun (nx, ny) -> Shared.Matrix.M.get matrix nx ny) + neighbour_coords in let paper_count = count_paper neighbours in let is_accessible = paper_count < 4 in @@ -34,7 +36,7 @@ and count_accessible_rolls_aux matrix count_paper accessible_rolls_of_paper = done done ; List.iter - (fun (x', y') -> Matrix.M.set matrix x' y' ".") + (fun (x', y') -> Shared.Matrix.M.set matrix x' y' ".") !accessible_rolls_of_paper and print_iteration_debug_msg x y neighbour_coords neighbours paper_count @@ -59,6 +61,6 @@ and print_iteration_debug_msg x y neighbour_coords neighbours paper_count let solve input () = if input = "" then Error err_empty_input else - let matrix = Matrix.M.of_string input in + let matrix = Shared.Matrix.M.of_string input in let accessible_rolls_of_paper = count_accessible_rolls matrix in Ok accessible_rolls_of_paper diff --git a/ocaml/lib/application/d04p02.mli b/ocaml/lib/year2025/day04/part02/solver.mli index 388ec56..388ec56 100644 --- a/ocaml/lib/application/d04p02.mli +++ b/ocaml/lib/year2025/day04/part02/solver.mli diff --git a/ocaml/lib/application/d06p01.ml b/ocaml/lib/year2025/day06/part01/solver.ml index 9425810..d992ac2 100644 --- a/ocaml/lib/application/d06p01.ml +++ b/ocaml/lib/year2025/day06/part01/solver.ml @@ -1,7 +1,7 @@ let err_empty_input = "input is empty" let log_matrix matrix = - let open Domain.Matrix in + let open Shared.Matrix in Logs.debug (fun m -> m "%s" (M.of_list matrix |> M.pp (fun elt -> elt))) let log_list lst = @@ -13,7 +13,7 @@ type operation = Mult | Add let solve input () = if input = "" then Error err_empty_input else - let input' = Parser.columns_of_string input in + let input' = Shared.Parser.columns_of_string input in assert (input' <> []) ; log_matrix input' ; let nums = diff --git a/ocaml/lib/application/d06p01.mli b/ocaml/lib/year2025/day06/part01/solver.mli index b3abc1e..b3abc1e 100644 --- a/ocaml/lib/application/d06p01.mli +++ b/ocaml/lib/year2025/day06/part01/solver.mli diff --git a/ocaml/lib/application/d06p02.ml b/ocaml/lib/year2025/day06/part02/solver.ml index 9425810..d992ac2 100644 --- a/ocaml/lib/application/d06p02.ml +++ b/ocaml/lib/year2025/day06/part02/solver.ml @@ -1,7 +1,7 @@ let err_empty_input = "input is empty" let log_matrix matrix = - let open Domain.Matrix in + let open Shared.Matrix in Logs.debug (fun m -> m "%s" (M.of_list matrix |> M.pp (fun elt -> elt))) let log_list lst = @@ -13,7 +13,7 @@ type operation = Mult | Add let solve input () = if input = "" then Error err_empty_input else - let input' = Parser.columns_of_string input in + let input' = Shared.Parser.columns_of_string input in assert (input' <> []) ; log_matrix input' ; let nums = diff --git a/ocaml/lib/application/d06p02.mli b/ocaml/lib/year2025/day06/part02/solver.mli index 49444de..49444de 100644 --- a/ocaml/lib/application/d06p02.mli +++ b/ocaml/lib/year2025/day06/part02/solver.mli diff --git a/ocaml/lib/application/dune b/ocaml/lib/year2025/dune index b78613a..99d5c0a 100644 --- a/ocaml/lib/application/dune +++ b/ocaml/lib/year2025/dune @@ -1,7 +1,9 @@ +(include_subdirs qualified) + (library - (name application) + (name year2025) (libraries - domain + shared fmt logs) (inline_tests) @@ -10,5 +12,5 @@ ppx_deriving.show ppx_deriving.eq ppx_deriving.ord - ppx_inline_test - ppx_expect))) + ppx_expect + ppx_inline_test))) |
