summaryrefslogtreecommitdiff
path: root/ocaml/lib/year2025/day01/part02/turner.ml
diff options
context:
space:
mode:
Diffstat (limited to 'ocaml/lib/year2025/day01/part02/turner.ml')
-rw-r--r--ocaml/lib/year2025/day01/part02/turner.ml28
1 files changed, 28 insertions, 0 deletions
diff --git a/ocaml/lib/year2025/day01/part02/turner.ml b/ocaml/lib/year2025/day01/part02/turner.ml
new file mode 100644
index 0000000..7477093
--- /dev/null
+++ b/ocaml/lib/year2025/day01/part02/turner.ml
@@ -0,0 +1,28 @@
+open Dial
+
+module Turner : sig
+ val turn : Dial.t -> int -> Dial.direction -> Dial.t * int
+ (** [turn dial direction] is the new state of the dial after turning [direction] on [dial] *)
+end = struct
+ (** [turn dial direction] is the new state of the dial after turning [direction] on [dial].
+ Raises: [dial_out_of_range] if the dial is moved to an invalid state. *)
+ let rec turn dial zero_count direction =
+ let open Dial in
+ match direction with
+ | Left 0 ->
+ (dial, zero_count)
+ | Left n ->
+ let dial' = prev dial in
+ let zero_count' =
+ if Dial.is_zero dial' then zero_count + 1 else zero_count
+ in
+ turn dial' zero_count' (Left (n - 1))
+ | Right 0 ->
+ (dial, zero_count)
+ | Right n ->
+ let dial' = next dial in
+ let zero_count' =
+ if Dial.is_zero dial' then zero_count + 1 else zero_count
+ in
+ turn dial' zero_count' (Right (n - 1))
+end