From 6d8fb621d1e93b83786890d6d9cb6f0d8defa8ae Mon Sep 17 00:00:00 2001 From: DJ O'Leary Date: Fri, 26 Jun 2026 18:04:09 +0200 Subject: refactor!: invert repo directory structure now is language first --- ocaml/lib/application/d01p01.ml | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 ocaml/lib/application/d01p01.ml (limited to 'ocaml/lib/application/d01p01.ml') diff --git a/ocaml/lib/application/d01p01.ml b/ocaml/lib/application/d01p01.ml new file mode 100644 index 0000000..206e757 --- /dev/null +++ b/ocaml/lib/application/d01p01.ml @@ -0,0 +1,53 @@ +open Domain.D01p01 + +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 current = function + | [] -> + count + | h :: t -> + let current' = Dial.turn current h in + Logs.debug (fun m -> m "dial at %d" (Dial.to_int current')) ; + if Dial.to_int current' = 0 then turn_aux (count + 1) current' t + else turn_aux count current' 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 -- cgit v1.2.3