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/year2025/day01/part02/solver.ml | |
| parent | 6d8fb621d1e93b83786890d6d9cb6f0d8defa8ae (diff) | |
refactor!: change repo directory structure for ocaml
Diffstat (limited to 'ocaml/lib/year2025/day01/part02/solver.ml')
| -rw-r--r-- | ocaml/lib/year2025/day01/part02/solver.ml | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/ocaml/lib/year2025/day01/part02/solver.ml b/ocaml/lib/year2025/day01/part02/solver.ml new file mode 100644 index 0000000..452209f --- /dev/null +++ b/ocaml/lib/year2025/day01/part02/solver.ml @@ -0,0 +1,56 @@ +open Dial +open Turner + +let err_empty_input = "input is empty" + +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 () = + if input = "" then Error err_empty_input + else + 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 + Ok answer |
