From: Grégory Burri Date: Tue, 4 Dec 2018 06:51:59 +0000 (+0100) Subject: Use a map for days functions. X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=39b07486912dff62b5c8311f2420abd4df01d87f;p=advent_of_code_2018.git Use a map for days functions. --- 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