summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDJ O'Leary <dijitol@proton.me>2025-12-01 14:24:35 +0100
committerDJ O'Leary <dijitol@proton.me>2025-12-01 14:24:35 +0100
commitc725cde0c8badeea5a2b720778a0d68c5054c9c9 (patch)
tree80fbfa9bbddaa049c88edaa7832b6a540748badb
parent04dfeb70d2663b6e46fc29bc72a0fde2b65ed1e6 (diff)
feat(2025d1p1): implement application logic and apply to example
-rw-r--r--2025/ocaml/lib/application/day_1.ml50
-rw-r--r--2025/ocaml/lib/application/day_1.mli4
-rw-r--r--2025/ocaml/lib/presentation/cli.ml15
3 files changed, 67 insertions, 2 deletions
diff --git a/2025/ocaml/lib/application/day_1.ml b/2025/ocaml/lib/application/day_1.ml
index 7ddac8b..bc0d5cc 100644
--- a/2025/ocaml/lib/application/day_1.ml
+++ b/2025/ocaml/lib/application/day_1.ml
@@ -1 +1,49 @@
-let run () = failwith "day 1 not implemented"
+open Domain.Day_1
+
+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 () =
+ 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/day_1.mli b/2025/ocaml/lib/application/day_1.mli
new file mode 100644
index 0000000..73631f1
--- /dev/null
+++ b/2025/ocaml/lib/application/day_1.mli
@@ -0,0 +1,4 @@
+val solve : string -> unit -> int
+(** [solve input ()] is the solution for the first day of advent of code 2025 for [input].
+ The solution for an empty input is [Domain.Day_1.Dial.(to_int start)].
+ Requires: [input] to be a multiline string using `\n` as it's line separator. *)
diff --git a/2025/ocaml/lib/presentation/cli.ml b/2025/ocaml/lib/presentation/cli.ml
index 08b1552..6344d07 100644
--- a/2025/ocaml/lib/presentation/cli.ml
+++ b/2025/ocaml/lib/presentation/cli.ml
@@ -1,6 +1,19 @@
+let day_1_example = {|L68
+L30
+R48
+L5
+R60
+L55
+L1
+L99
+R14
+L82|}
+
let run_day = function
| 1 ->
- Application.Day_1.run ()
+ let answer = Application.Day_1.solve day_1_example () in
+ Logs.app (fun m -> m "%d" answer) ;
+ 0
| _ ->
failwith "day not implemented"