From c629e2e3694ab207df9de2b4198171325a3b61e5 Mon Sep 17 00:00:00 2001 From: Ummon Date: Wed, 6 Dec 2017 09:41:48 +0100 Subject: [PATCH] Reduce execution time from 1s to 0.1s for day 6 --- AdventOfCode2017.sln | 8 +++++++- AdventOfCode2017/Day6.fs | 17 ++++++++++++++++- AdventOfCode2017/Program.fs | 4 +++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/AdventOfCode2017.sln b/AdventOfCode2017.sln index b07e76f..bc9b5dc 100644 --- a/AdventOfCode2017.sln +++ b/AdventOfCode2017.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.27004.2010 +VisualStudioVersion = 15.0.27130.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "AdventOfCode2017", "AdventOfCode2017\AdventOfCode2017.fsproj", "{D3555943-8102-43D1-B3CB-570A4E4EC513}" EndProject @@ -14,6 +14,9 @@ EndProject Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Tests", "Tests\Tests.fsproj", "{238BFFBE-E2D4-4DC4-804C-6E43205E4701}" EndProject Global + GlobalSection(Performance) = preSolution + HasPerformanceSessions = true + EndGlobalSection GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU @@ -34,4 +37,7 @@ Global GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {1DAC18E9-E52B-45B9-AB96-686277B1EB81} EndGlobalSection + GlobalSection(Performance) = preSolution + HasPerformanceSessions = true + EndGlobalSection EndGlobal diff --git a/AdventOfCode2017/Day6.fs b/AdventOfCode2017/Day6.fs index 85fac4c..867d408 100644 --- a/AdventOfCode2017/Day6.fs +++ b/AdventOfCode2017/Day6.fs @@ -1,12 +1,27 @@ module AdventOfCode2017.Day6 open System +open System.Linq type Blocks = int[] let parseInput (str : string) : int[] = str.Split ([| '\r'; '\t'; ' ' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int +// Custom equality: more efficient than '='. +let inline (|=|) (a1 : 'a[]) (a2 : 'a[]) : bool = + if a1.Length <> a2.Length then + false + else + let rec result i = + if i = a1.Length then + true + elif a1.[i] <> a2.[i] then + false + else + result (i + 1) + result 0 + let nbRedistribution (blocks : Blocks) = let rec next (previous : Blocks list) = let blocks = List.head previous |> Array.copy @@ -16,7 +31,7 @@ let nbRedistribution (blocks : Blocks) = let i' = offset % blocks.Length blocks.[i'] <- blocks.[i'] + 1 - match previous |> List.tryFindIndex ((=) blocks) with + match previous |> List.tryFindIndex ((|=|) blocks) with | Some i -> previous, i + 1 | None -> next (blocks :: previous) diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index b5116bb..e64ecf4 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -29,6 +29,8 @@ let day6 () = sprintf "part1 = %A, part2 = %A" part1 part2 let doDay (n : int) = + let sw = Diagnostics.Stopwatch () + sw.Start () let result = match n with | 1 -> day1 () @@ -38,7 +40,7 @@ let doDay (n : int) = | 5 -> day5 () | 6 -> day6 () | _ -> raise <| NotImplementedException () - printfn "Result of day %i: %s" n result + printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds [] let main argv = -- 2.45.2