Skip to content

Commit

Permalink
2024: Use HashSet for speeding up
Browse files Browse the repository at this point in the history
From 30 to 6 seconds... still slow but already a lot better.
  • Loading branch information
jp7677 committed Dec 11, 2024
1 parent 5958099 commit 2b6e825
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions 2024/src/Day06.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Aoc2024

open System.Collections.Generic
open Xunit
open FsUnit.Xunit
open Util
Expand Down Expand Up @@ -122,7 +123,7 @@ module Day06 =

distinctSteps |> should equal 5212

[<Fact(Skip = "Slow")>]
[<Fact>]
let ``part 02`` () =
let max, obstructions, guard = getMap

Expand All @@ -136,16 +137,16 @@ module Day06 =
if obstructions.Contains(newObstruction) || newObstruction = guard.coord then
None
else
let alternativeObstructions = obstructions |> Set.add newObstruction
let alternativeObstructions = obstructions.Add newObstruction
let alternativeSteps = getSteps max alternativeObstructions guard

let mutable alternativeGuardStepsSet: Set<Guard> = Set.empty
let alternativeGuardStepsSet = HashSet<Guard>()

let findRepeat =
alternativeSteps
|> Seq.tryFind (fun alternativeStep ->
let foundRepeatedStep = alternativeGuardStepsSet.Contains(alternativeStep)
alternativeGuardStepsSet <- alternativeGuardStepsSet |> Set.add alternativeStep
alternativeGuardStepsSet.Add alternativeStep |> ignore
foundRepeatedStep)

match findRepeat with
Expand Down

0 comments on commit 2b6e825

Please sign in to comment.