summaryrefslogtreecommitdiff
path: root/ocaml/lib/domain/d01p01.ml
diff options
context:
space:
mode:
authorDJ O'Leary <dijitol@proton.me>2026-06-26 18:04:09 +0200
committerDJ O'Leary <dijitol@proton.me>2026-06-26 18:04:09 +0200
commit6d8fb621d1e93b83786890d6d9cb6f0d8defa8ae (patch)
tree17c90d3984c114aa023600cc5bec6f6004b02243 /ocaml/lib/domain/d01p01.ml
parentad4ea5ca4bc9f7540ee1d6a4fa2d881709dc6459 (diff)
refactor!: invert repo directory structure
now is language first
Diffstat (limited to 'ocaml/lib/domain/d01p01.ml')
-rw-r--r--ocaml/lib/domain/d01p01.ml76
1 files changed, 76 insertions, 0 deletions
diff --git a/ocaml/lib/domain/d01p01.ml b/ocaml/lib/domain/d01p01.ml
new file mode 100644
index 0000000..6b531f4
--- /dev/null
+++ b/ocaml/lib/domain/d01p01.ml
@@ -0,0 +1,76 @@
+module Dial : sig
+ (** [t] is a dial between 0 and 99 inclusive *)
+ type t
+
+ (** [direction] is a direction the dial can be turned *)
+ type direction = Left of int | Right of int
+
+ val start : t
+ (** [start] is the initial state of the dial *)
+
+ val dial_out_of_range : string
+ (** [dial_out_of_range] is the error message when the dial is out of range *)
+
+ val turn : t -> direction -> t
+ (** [turn current direction] is the new state of the dial after turning [direction] from [current] *)
+
+ val to_string : t -> string
+ (** [to_string t] is the string representation of [t] *)
+
+ val to_int : t -> int
+ (** [to_int t] is the integer representation of [t] *)
+
+ val pp_direction : direction -> string
+end = struct
+ (** [t] is an int between 0 and 99 inclusive *)
+ type t = int
+
+ type direction = Left of int | Right of int
+
+ let start = 50
+
+ let dial_out_of_range = "dial out of range"
+
+ (** [next n] is the new state of the dial after turning [Right].
+ Raises: [dial_out_of_range] if [n] is greater than 99. *)
+ let next = function
+ | n when n < 99 ->
+ n + 1
+ | 99 ->
+ 0
+ | _ ->
+ failwith dial_out_of_range
+
+ (** [prev n] is the new state of the dial after turning [Left].
+ Raises: [dial_out_of_range] if [n] is less than 0. *)
+ let prev = function
+ | n when n > 0 ->
+ n - 1
+ | 0 ->
+ 99
+ | _ ->
+ failwith dial_out_of_range
+
+ (** [turn current direction] is the new state of the dial after turning [direction] from [current].
+ Raises: [dial_out_of_range] if the dial is moved to an invalid state. *)
+ let rec turn current direction =
+ match direction with
+ | Left 0 ->
+ current
+ | Left n ->
+ turn (prev current) (Left (n - 1))
+ | Right 0 ->
+ current
+ | Right n ->
+ turn (next current) (Right (n - 1))
+
+ let to_string = string_of_int
+
+ let to_int t = t
+
+ let pp_direction = function
+ | Left n ->
+ "Left " ^ string_of_int n
+ | Right n ->
+ "Right " ^ string_of_int n
+end