diff options
| author | DJ O'Leary <dijitol@proton.me> | 2025-12-03 20:17:14 +0100 |
|---|---|---|
| committer | DJ O'Leary <dijitol@proton.me> | 2025-12-03 20:17:33 +0100 |
| commit | 4efc4e08ae62945002d35404c108e8c724da0cb3 (patch) | |
| tree | c7bdce807e68f3accf06849582a3848cf9dbc086 /2025/ocaml/lib | |
| parent | 2c07926330771788a0a9b789b7397a159475bea7 (diff) | |
refactor(2025d2p1): use Seq instead of List so that elements can be transformed one at a time
this reduces allocation overhead
Diffstat (limited to '2025/ocaml/lib')
| -rw-r--r-- | 2025/ocaml/lib/application/d02p01.ml | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/2025/ocaml/lib/application/d02p01.ml b/2025/ocaml/lib/application/d02p01.ml index 2d833be..ce8778d 100644 --- a/2025/ocaml/lib/application/d02p01.ml +++ b/2025/ocaml/lib/application/d02p01.ml @@ -5,19 +5,20 @@ open Domain.D02p01 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)) +let pp_lines = Seq.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 -> + Seq.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))) +let pp_ranges = Seq.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 collect_all = + Seq.concat_map (fun range -> List.to_seq (Range.collect range)) -let pp_collection = List.iter (fun c -> Logs.debug (fun m -> m "%s" (ID.pp c))) +let pp_collection = Seq.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 = @@ -36,11 +37,11 @@ let is_invalid = ID.validate rules let passthrough_with_logs logger lst = logger lst ; lst let solve input () = - input |> input_to_lines + 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 - |> List.filter is_invalid - |> List.fold_left (fun acc id -> acc + ID.to_int id) 0 + |> Seq.filter is_invalid + |> Seq.fold_left (fun acc id -> acc + ID.to_int id) 0 |
