summaryrefslogtreecommitdiffstats
path: root/bin/main.ml
blob: 292f581b737d048d07a90d79007e76bed03c4d0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(*
 * SPDX-FileCopyrightText: Copyright 2025 Alexandre Jesus <https://adbjesus.com>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *)

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)

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)

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
  | _ -> failwith (Format.sprintf "Day %d, part %d, has not yet been implemented\n" day part)

let () =
  let usage_msg = "aoc2025 <day> <part> <inputfile>" in
  let args = ref [] in
  let anon_fun arg = args := arg :: !args in
  let speclist = [] in
  Arg.parse speclist anon_fun usage_msg;
  let args = Array.of_list !args in
  if Array.length args != 3 then
    failwith "Wrong number of arguments, expected 3: <day> <part> <inputfile>"
  else
    let fn = day_part_fn args.(2) args.(1) in
    let ic = open_in args.(0) in
    try
      let s = Unix.gettimeofday () in
      fn ic;
      Printf.printf "%.3f" ((Unix.gettimeofday ()) -. s)
    with e ->
      close_in_noerr ic;
      raise e