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/turner.ml | |
| parent | 6d8fb621d1e93b83786890d6d9cb6f0d8defa8ae (diff) | |
refactor!: change repo directory structure for ocaml
Diffstat (limited to 'ocaml/lib/year2025/day01/part02/turner.ml')
| -rw-r--r-- | ocaml/lib/year2025/day01/part02/turner.ml | 28 |
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 |
