diff options
| author | DJ O'Leary <dijitol@proton.me> | 2025-12-02 00:09:58 +0100 |
|---|---|---|
| committer | DJ O'Leary <dijitol@proton.me> | 2025-12-02 00:09:58 +0100 |
| commit | 121eba870e6faee3eb0985b53451c6a76e74a6c5 (patch) | |
| tree | a7a05c04df4902595b80abfdf3eff53a7d348c81 /2025/ocaml/lib/application | |
| parent | 6174e9bb0eeddb57f50c31d6de67c6327f4f39c7 (diff) | |
feat(2025d1p2): implement solution to problem
Diffstat (limited to '2025/ocaml/lib/application')
| -rw-r--r-- | 2025/ocaml/lib/application/d01p02.ml | 51 | ||||
| -rw-r--r-- | 2025/ocaml/lib/application/d01p02.mli | 4 |
2 files changed, 55 insertions, 0 deletions
diff --git a/2025/ocaml/lib/application/d01p02.ml b/2025/ocaml/lib/application/d01p02.ml new file mode 100644 index 0000000..6f935a4 --- /dev/null +++ b/2025/ocaml/lib/application/d01p02.ml @@ -0,0 +1,51 @@ +open Domain.D01p02 + +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 dial = function + | [] -> + count + | h :: t -> ( + let dial_and_count = Turner.turn dial count h in + match dial_and_count with + | dial', count' -> + Logs.debug (fun m -> + m "(dial=%d, count=%d)" (Dial.to_int dial') count' ) ; + turn_aux count' dial' 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/d01p02.mli b/2025/ocaml/lib/application/d01p02.mli new file mode 100644 index 0000000..33aa63f --- /dev/null +++ b/2025/ocaml/lib/application/d01p02.mli @@ -0,0 +1,4 @@ +val solve : string -> unit -> int +(** [solve input ()] is the solution for the second part of the first day of advent of code 2025 for [input]. + The solution for an empty input is 0. + Requires: [input] to be a multiline string using `\n` as it's line separator. *) |
