summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexandre Jesus <adbjesus@gmail.com>2025-12-03 21:05:57 +0000
committerAlexandre Jesus <adbjesus@gmail.com>2025-12-03 21:05:57 +0000
commit6d80c94eb7ea8ac0421df0832409b63be7c1246a (patch)
tree675ceb3feb257c4986860b4a10de5becc677a94a
parentc260cea076a5dda847e108454aedc10385ea5c10 (diff)
downloadaoc2025-6d80c94eb7ea8ac0421df0832409b63be7c1246a.tar.gz
aoc2025-6d80c94eb7ea8ac0421df0832409b63be7c1246a.zip
Setup ocamlformat
Define a initial setup for ocamlformat. I might still tweak the settings in the future.
-rw-r--r--.ocamlformat3
-rw-r--r--bin/main.ml29
-rw-r--r--lib/day01.ml12
-rw-r--r--lib/day02.ml26
-rw-r--r--lib/day03.ml24
5 files changed, 47 insertions, 47 deletions
diff --git a/.ocamlformat b/.ocamlformat
new file mode 100644
index 0000000..4e76cca
--- /dev/null
+++ b/.ocamlformat
@@ -0,0 +1,3 @@
+profile = default
+version = 0.28.1
+break-infix = fit-or-vertical \ No newline at end of file
diff --git a/bin/main.ml b/bin/main.ml
index 403c606..2769553 100644
--- a/bin/main.ml
+++ b/bin/main.ml
@@ -9,24 +9,33 @@ open Aoc2025
let parse_day s =
match int_of_string_opt s with
| Some n when n >= 1 && n <= 12 -> n
- | _ -> failwith (Format.sprintf "Day '%s' is invalid, expected an integer between 1 and 12" s)
+ | _ ->
+ failwith
+ (Format.sprintf
+ "Day '%s' is invalid, expected an integer between 1 and 12" s)
let parse_part s =
match int_of_string_opt s with
| Some n when n >= 1 && n <= 2 -> n
- | _ -> failwith (Format.sprintf "Part '%s' is invalid, expected an integer between 1 and 2" s)
+ | _ ->
+ failwith
+ (Format.sprintf
+ "Part '%s' is invalid, expected an integer between 1 and 2" s)
let day_part_fn day part =
let day = parse_day day in
let part = parse_part part in
match (day, part) with
- | (1, 1) -> Day01.part1
- | (1, 2) -> Day01.part2
- | (2, 1) -> Day02.part1
- | (2, 2) -> Day02.part2
- | (3, 1) -> Day03.part1
- | (3, 2) -> Day03.part2
- | _ -> failwith (Format.sprintf "Day %d, part %d, has not yet been implemented\n" day part)
+ | 1, 1 -> Day01.part1
+ | 1, 2 -> Day01.part2
+ | 2, 1 -> Day02.part1
+ | 2, 2 -> Day02.part2
+ | 3, 1 -> Day03.part1
+ | 3, 2 -> Day03.part2
+ | _ ->
+ failwith
+ (Format.sprintf "Day %d, part %d, has not yet been implemented\n" day
+ part)
let () =
let usage_msg = "aoc2025 <day> <part> <inputfile>" in
@@ -43,7 +52,7 @@ let () =
try
let s = Unix.gettimeofday () in
fn ic;
- Printf.printf "%.3f" ((Unix.gettimeofday ()) -. s)
+ Printf.printf "%.3f" (Unix.gettimeofday () -. s)
with e ->
close_in_noerr ic;
raise e
diff --git a/lib/day01.ml b/lib/day01.ml
index 9a983a6..097ad28 100644
--- a/lib/day01.ml
+++ b/lib/day01.ml
@@ -5,10 +5,10 @@
*)
let parse_rotation s =
- let num = lazy (int_of_string (String.sub s 1 ((String.length s) - 1))) in
+ let num = lazy (int_of_string (String.sub s 1 (String.length s - 1))) in
match s.[0] with
| 'R' -> Lazy.force num
- | 'L' -> -(Lazy.force num)
+ | 'L' -> -Lazy.force num
| s -> failwith (Printf.sprintf "Unknown rotation %c" s)
let parse_rotations ch =
@@ -25,8 +25,7 @@ let part1 ch =
(fun (a, p) r ->
let nxt = (p + r) mod m in
let inc = if nxt = 0 then 1 else 0 in
- (a + inc, nxt)
- )
+ (a + inc, nxt))
(0, s)
|> fst
|> Printf.printf "%d\n"
@@ -38,10 +37,9 @@ let part2 ch =
|> List.fold_left
(fun (a, p) r ->
let nxt = p + r in
- let inc = (abs nxt) / m + if p > 0 && nxt <= 0 then 1 else 0 in
+ let inc = (abs nxt / m) + if p > 0 && nxt <= 0 then 1 else 0 in
let nxt = ((nxt mod m) + m) mod m in
- (a + inc, nxt)
- )
+ (a + inc, nxt))
(0, s)
|> fst
|> Printf.printf "%d\n"
diff --git a/lib/day02.ml b/lib/day02.ml
index b751fbd..4bce292 100644
--- a/lib/day02.ml
+++ b/lib/day02.ml
@@ -6,7 +6,7 @@
let parse_range s =
match String.split_on_char '-' s with
- | [a; b] -> (int_of_string a, int_of_string b)
+ | [ a; b ] -> (int_of_string a, int_of_string b)
| _ -> failwith ("Invalid range " ^ s)
let parse_ranges ch =
@@ -16,11 +16,11 @@ let parse_ranges ch =
|> List.map parse_range
let is_invalid s cnt =
- cnt > 0 &&
- (String.length s) mod cnt = 0 &&
- String.to_seqi s
- |> Seq.drop cnt
- |> Seq.for_all (fun (i, c) -> c = s.[i - cnt])
+ cnt > 0
+ && String.length s mod cnt = 0
+ && String.to_seqi s
+ |> Seq.drop cnt
+ |> Seq.for_all (fun (i, c) -> c = s.[i - cnt])
let is_invalid_part1 a =
let s = string_of_int a in
@@ -31,23 +31,19 @@ let sum_invalid_ids invalid_fn (a, b) =
Seq.ints a
|> Seq.take (b - a + 1)
|> Seq.filter invalid_fn
- |> Seq.fold_left (+) 0
+ |> Seq.fold_left ( + ) 0
let solve ch invalid_fn =
parse_ranges ch
|> List.map (sum_invalid_ids invalid_fn)
- |> List.fold_left (+) 0
+ |> List.fold_left ( + ) 0
|> Printf.printf "%d\n"
-let part1 ch =
- solve ch is_invalid_part1
+let part1 ch = solve ch is_invalid_part1
let is_invalid_part2 a =
let s = string_of_int a in
let l = String.length s in
- Seq.ints 1
- |> Seq.take (l / 2)
- |> Seq.exists (is_invalid s)
+ Seq.ints 1 |> Seq.take (l / 2) |> Seq.exists (is_invalid s)
-let part2 ch =
- solve ch is_invalid_part2
+let part2 ch = solve ch is_invalid_part2
diff --git a/lib/day03.ml b/lib/day03.ml
index 4e535b2..a5fab4a 100644
--- a/lib/day03.ml
+++ b/lib/day03.ml
@@ -14,18 +14,16 @@ let parse_data ch =
- Keep track of the number [k = |bat| - n] of banks that can be discarded
Then process banks sequentially according to the following steps:
- - While the stack is not empty, [k > 0], and the current bank is bigger
- than the one at the top of the stack, pop a bank from the stack and
- reduce [k] by 1
+ - While the stack is not empty, [k > 0], and the current bank is bigger than
+ the one at the top of the stack, pop a bank from the stack and reduce [k]
+ by 1
- Insert the bank into the stack if the size of the stack is less then [n]
- Otherwise, don't insert the bank, and reduce [k] by 1
At the end the stack contains the relevant banks, in reverse order.
- Since each bank is inserted, and removed, at most once from the
- stack, and all operations are [O(1)] the complexity is
- [O(|bat|)].
- *)
+ Since each bank is inserted, and removed, at most once from the stack, and
+ all operations are [O(1)] the complexity is [O(|bat|)]. *)
let joltage n bat =
let rec fn s k m l =
match Seq.uncons l with
@@ -34,13 +32,10 @@ let joltage n bat =
let digit c = int_of_char c - zero in
List.rev_map digit s |> List.fold_left (fun a v -> (a * 10) + v) 0
| Some (h, t) -> (
- match s with
- | sh :: st when k > 0 && sh < h ->
- fn st (k - 1) (m - 1) l
- | s when m < n ->
- fn (h :: s) k (m + 1) t
- | s ->
- fn s (k - 1) m t )
+ match s with
+ | sh :: st when k > 0 && sh < h -> fn st (k - 1) (m - 1) l
+ | s when m < n -> fn (h :: s) k (m + 1) t
+ | s -> fn s (k - 1) m t)
in
let k = String.length bat - n in
fn [] k 0 (String.to_seq bat)
@@ -52,5 +47,4 @@ let solve ch n =
|> Printf.printf "%d\n"
let part1 ch = solve ch 2
-
let part2 ch = solve ch 12