summaryrefslogtreecommitdiff
path: root/ocaml/lib/year2025/day02/part02
diff options
context:
space:
mode:
authorDJ O'Leary <dijitol@proton.me>2026-06-27 00:11:43 +0200
committerDJ O'Leary <dijitol@proton.me>2026-06-27 00:11:43 +0200
commit976048a85403fbb0751c34fa42703a3c47f6e918 (patch)
tree46e321d8120c45e318ead66f60165b0b854c85a1 /ocaml/lib/year2025/day02/part02
parent6d8fb621d1e93b83786890d6d9cb6f0d8defa8ae (diff)
refactor!: change repo directory structure for ocaml
Diffstat (limited to 'ocaml/lib/year2025/day02/part02')
-rw-r--r--ocaml/lib/year2025/day02/part02/id.ml12
-rw-r--r--ocaml/lib/year2025/day02/part02/id.mli16
-rw-r--r--ocaml/lib/year2025/day02/part02/range.ml18
-rw-r--r--ocaml/lib/year2025/day02/part02/range.mli15
-rw-r--r--ocaml/lib/year2025/day02/part02/solver.ml87
-rw-r--r--ocaml/lib/year2025/day02/part02/solver.mli4
6 files changed, 152 insertions, 0 deletions
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/year2025/day02/part02/id.mli b/ocaml/lib/year2025/day02/part02/id.mli
new file mode 100644
index 0000000..a512593
--- /dev/null
+++ b/ocaml/lib/year2025/day02/part02/id.mli
@@ -0,0 +1,16 @@
+module ID : sig
+ (** [t] is a ID within a range *)
+ type t
+
+ val create : int -> t
+ (** [create num] is an ID [t] *)
+
+ val incr : t -> t
+ (** [incr id] is the next [id] in the sequence *)
+
+ val to_string : t -> string
+ (** [to_string id] is an string representation of [id] *)
+
+ val pp : t -> string
+ (** [pp id] is a pretty string representing [id] *)
+end
diff --git a/ocaml/lib/year2025/day02/part02/range.ml b/ocaml/lib/year2025/day02/part02/range.ml
new file mode 100644
index 0000000..ba27568
--- /dev/null
+++ b/ocaml/lib/year2025/day02/part02/range.ml
@@ -0,0 +1,18 @@
+open Id
+
+module Range = struct
+ type t = ID.t * ID.t
+
+ let create (id1, id2) = (ID.create id1, ID.create id2)
+
+ let rec collect range = collect_range_aux [] range
+
+ and collect_range_aux lst (id1, id2) =
+ match id1 <= id2 with
+ | false ->
+ lst
+ | true ->
+ collect_range_aux (id1 :: lst) (ID.incr id1, id2)
+
+ let pp (id1, id2) = "(L" ^ ID.pp id1 ^ ", R" ^ ID.pp id2 ^ ")"
+end
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/year2025/day02/part02/solver.ml b/ocaml/lib/year2025/day02/part02/solver.ml
new file mode 100644
index 0000000..f5fdf0c
--- /dev/null
+++ b/ocaml/lib/year2025/day02/part02/solver.ml
@@ -0,0 +1,87 @@
+open Id
+open Range
+
+(* Errors *)
+
+let err_empty_input = "input is empty"
+
+(* Logging *)
+
+(** [passthrough_with_logs logger lst] is [lst] with the side-effects caused by calling [logger] on [lst]. *)
+let passthrough_with_logs logger lst = logger lst ; lst
+
+(** [input_to_lines input] is a list of strings that have been split on ','.
+ Example: [input_to_lines "12-34,56-78,90-100"] becomes the list \[ "12-34"; "56-78"; "90-100" \].
+ Requires: [input] to be a string in the format "12-34,56-78,90-100". *)
+let input_to_lines input = input |> String.trim |> String.split_on_char ','
+
+let pp_lines = Seq.iter (fun l -> Logs.debug (fun m -> m "line=%s" l))
+
+let pp_ranges = Seq.iter (fun r -> Logs.debug (fun m -> m "%s" (Range.pp r)))
+
+let pp_collection = Seq.iter (fun c -> Logs.debug (fun m -> m "%s" (ID.pp c)))
+
+(* Core Logic *)
+
+(** [lines_to_ranges line] is a list of [Range]s found on [line]. *)
+let lines_to_ranges =
+ Seq.map (fun line ->
+ Scanf.sscanf line "%d-%d" (fun id1 id2 -> Range.create (id1, id2)) )
+
+(** [collect_all ranges] is an unsorted list of [ID]s within the [ranges] (inclusive). *)
+let collect_all =
+ Seq.concat_map (fun range -> List.to_seq (Range.collect range))
+
+(** [string_to_segments count string] is a list containing [count] string segments with of equal length.
+ Requires: length of [string] to be divisible by [count]
+ and [count] to be greater than or equal to zero *)
+let rec string_to_segments s =
+ let segment_lengths = possible_segment_lengths s in
+ List.map (fun c -> string_to_segments_aux [] c s |> List.rev) segment_lengths
+
+(** [possible_segment_lengths s] is a list of segment sizes that fit within [s]
+ that must be checked to rule out a repeating pattern. *)
+and possible_segment_lengths s =
+ List.init (String.length s) (fun x -> x + 1)
+ |> List.filter (fun x -> x != 1 && String.length s mod x = 0)
+
+and string_to_segments_aux segments count string =
+ match List.length segments = count with
+ | true ->
+ segments
+ | false ->
+ let segment_start = String.length string / count * List.length segments in
+ let segment_len = String.length string / count in
+ string_to_segments_aux
+ (String.sub string segment_start segment_len :: segments)
+ count string
+
+(** [cmp_segments segments] is true is all segments are equal *)
+let cmp_segments segments =
+ List.length (List.sort_uniq String.compare segments) = 1
+
+(** [is_invalid id] is [true] for an [id] that is considered invalid. *)
+let is_invalid id =
+ Logs.debug (fun m -> m "CHECKING ID=%s" id) ;
+ let repeating_patterns =
+ id |> string_to_segments |> List.filter cmp_segments
+ in
+ List.iter
+ (fun l -> Logs.debug (fun m -> m "HAS SUBPATTERN=%s" (List.hd l)))
+ repeating_patterns ;
+ List.length repeating_patterns > 0
+
+let solve input () =
+ if input = "" then Error err_empty_input
+ else
+ let answer =
+ input |> input_to_lines |> List.to_seq
+ |> passthrough_with_logs pp_lines
+ |> lines_to_ranges
+ |> passthrough_with_logs pp_ranges
+ |> collect_all
+ |> passthrough_with_logs pp_collection
+ |> Seq.map ID.to_string |> Seq.filter is_invalid
+ |> Seq.fold_left (fun acc id -> acc + int_of_string id) 0
+ in
+ Ok answer
diff --git a/ocaml/lib/year2025/day02/part02/solver.mli b/ocaml/lib/year2025/day02/part02/solver.mli
new file mode 100644
index 0000000..8a8eb32
--- /dev/null
+++ b/ocaml/lib/year2025/day02/part02/solver.mli
@@ -0,0 +1,4 @@
+val solve : string -> unit -> (int, string) result
+(** [solve input ()] is the solution for the first part of the second day of advent of code 2025 for [input].
+ The solution for an empty input is [Error msg].
+ Requires: [input] to be a string. *)