summaryrefslogtreecommitdiff
path: root/2025/ocaml
diff options
context:
space:
mode:
authorDJ O'Leary <dijitol@proton.me>2025-12-03 03:33:26 +0100
committerDJ O'Leary <dijitol@proton.me>2025-12-03 03:33:26 +0100
commit87f01faf1430393c1c062da2a9511c6767013ecb (patch)
treed640eeb5894eed898d4144bad105edf46a05c3cd /2025/ocaml
parenta2e648c2c7dc9bce7cd371b695f174181f9284dc (diff)
feat(2025d2p2): copy part 1 solution as part 2 for editing
Diffstat (limited to '2025/ocaml')
-rw-r--r--2025/ocaml/lib/application/d02p02.ml46
-rw-r--r--2025/ocaml/lib/application/d02p02.mli4
-rw-r--r--2025/ocaml/lib/domain/d02p02.ml33
-rw-r--r--2025/ocaml/lib/domain/d02p02.mli36
-rw-r--r--2025/ocaml/lib/presentation/cli.ml1
5 files changed, 120 insertions, 0 deletions
diff --git a/2025/ocaml/lib/application/d02p02.ml b/2025/ocaml/lib/application/d02p02.ml
new file mode 100644
index 0000000..26cf109
--- /dev/null
+++ b/2025/ocaml/lib/application/d02p02.ml
@@ -0,0 +1,46 @@
+open Domain.D02p02
+
+(** [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 = List.iter (fun l -> Logs.debug (fun m -> m "line=%s" l))
+
+(** [lines_to_ranges line] is a list of [Range]s found on [line]. *)
+let lines_to_ranges =
+ List.map (fun line ->
+ Scanf.sscanf line "%d-%d" (fun id1 id2 -> Range.create (id1, id2)) )
+
+let pp_ranges = List.iter (fun r -> Logs.debug (fun m -> m "%s" (Range.pp r)))
+
+(** [collect_all ranges] is an unsorted list of [ID]s within the [ranges] (inclusive). *)
+let collect_all = List.concat_map (fun range -> Range.collect range)
+
+let pp_collection = List.iter (fun c -> Logs.debug (fun m -> m "%s" (ID.pp c)))
+
+(** [rules] is a list of rules that [ID]s must pass to be considered invalid. *)
+let rules =
+ [ (fun id -> String.length (ID.to_string id) mod 2 = 0)
+ ; (fun id ->
+ let id' = ID.to_string id in
+ let id_len = String.length id' in
+ let left = String.sub id' 0 (id_len / 2) in
+ let right = String.sub id' (id_len / 2) (id_len / 2) in
+ String.equal left right ) ]
+
+(** [is_invalid id] is [true] for an [id] that passes all of the rules and is considered invalid. *)
+let is_invalid = ID.validate rules
+
+(** [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
+
+let solve input () =
+ input |> input_to_lines
+ |> passthrough_with_logs pp_lines
+ |> lines_to_ranges
+ |> passthrough_with_logs pp_ranges
+ |> collect_all
+ |> passthrough_with_logs pp_collection
+ |> List.filter is_invalid
+ |> List.fold_left (fun acc id -> acc + ID.to_int id) 0
diff --git a/2025/ocaml/lib/application/d02p02.mli b/2025/ocaml/lib/application/d02p02.mli
new file mode 100644
index 0000000..27f85d4
--- /dev/null
+++ b/2025/ocaml/lib/application/d02p02.mli
@@ -0,0 +1,4 @@
+val solve : string -> unit -> int
+(** [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 0.
+ Requires: [input] to be a string. *)
diff --git a/2025/ocaml/lib/domain/d02p02.ml b/2025/ocaml/lib/domain/d02p02.ml
new file mode 100644
index 0000000..725d4b1
--- /dev/null
+++ b/2025/ocaml/lib/domain/d02p02.ml
@@ -0,0 +1,33 @@
+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
+
+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/2025/ocaml/lib/domain/d02p02.mli b/2025/ocaml/lib/domain/d02p02.mli
new file mode 100644
index 0000000..55665fe
--- /dev/null
+++ b/2025/ocaml/lib/domain/d02p02.mli
@@ -0,0 +1,36 @@
+module ID : sig
+ (** [t] is a ID within a range *)
+ type t
+
+ val create : int -> t
+ (** [create num] is an ID [t] *)
+
+ val validate : (t -> bool) list -> t -> bool
+ (** [validate rules id] is [true] if the [id] passes all rule checks *)
+
+ val incr : t -> t
+ (** [incr id] is the next [id] in the sequence *)
+
+ val to_int : t -> int
+ (** [to_int id] is an integer representation of [id] *)
+
+ 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
+
+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/2025/ocaml/lib/presentation/cli.ml b/2025/ocaml/lib/presentation/cli.ml
index 02c69da..29195b6 100644
--- a/2025/ocaml/lib/presentation/cli.ml
+++ b/2025/ocaml/lib/presentation/cli.ml
@@ -12,6 +12,7 @@ let run_day = function
| 2 ->
let filepath = "../_input/day_02.txt" in
run_solver "D02P01" Application.D02p01.solve filepath ;
+ run_solver "D02P02" Application.D02p02.solve filepath ;
0
| _ ->
failwith "day not implemented"