From 39b07486912dff62b5c8311f2420abd4df01d87f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9gory=20Burri?= Date: Tue, 4 Dec 2018 07:51:59 +0100 Subject: [PATCH] Use a map for days functions. --- Program.fs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Program.fs b/Program.fs index 1f93628..71135ba 100644 --- a/Program.fs +++ b/Program.fs @@ -7,21 +7,25 @@ let day01 () = let input = File.ReadAllText "Data/day01.input" |> Day01.parseInput input -let days : (unit -> string) array = - [| - day01 - |] +let days : Map string> = + [ + 1, day01 + ] |> Map.ofList let doDay (n : int) = if n < 1 then ArgumentException "day number must be greater or equal to 1" |> raise - elif n > days.Length then - NotImplementedException (sprintf "no implementation for day %i" n) |> raise + else - let sw = Diagnostics.Stopwatch () - sw.Start () - let result = days.[n - 1] () - printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds + match Map.tryFind n days with + | Some day -> + let sw = Diagnostics.Stopwatch () + sw.Start () + let result = day () + printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds + + | None -> + NotImplementedException (sprintf "no implementation for day %i" n) |> raise [] let main argv = @@ -30,8 +34,8 @@ let main argv = if argv.Length > 0 then doDay (int argv.[0]) else - for d = 1 to days.Length do - doDay d + for day in days do + doDay day.Key Console.Read () |> ignore 0 -- 2.45.2