Last active
December 17, 2020 11:08
-
-
Save Karamell/337f8fb32500adfa2308891bd7753df0 to your computer and use it in GitHub Desktop.
Advent of code 2020 day 17 part 2 in F#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #time "on" | |
| open System | |
| let lines = System.IO.File.ReadAllLines ( __SOURCE_DIRECTORY__ + "/input.txt") | |
| module Cube = | |
| let neighbours (x,y,z,w) = | |
| let n = [-1 .. 1] | |
| n |> List.collect(fun x' -> | |
| n |> List.collect(fun y' -> | |
| n |> List.collect(fun z' -> | |
| n |> List.collect(fun w' -> | |
| [(x + x', y + y', z + z', w + w')])))) | |
| |> List.except [(x,y,z,w)] | |
| let activeN a c = | |
| neighbours c |> List.filter(fun p -> Set.contains p a) |> List.length | |
| let starting = | |
| lines |> Seq.mapi(fun y l -> l |> Seq.mapi(fun x a -> if a = '#' then Some (x, y, 0, 0) else None )) | |
| |> Seq.concat |> Seq.choose id |> Seq.toList | |
| let rec run a cycle n = | |
| let wc = System.Diagnostics.Stopwatch.StartNew() | |
| let aList = a |> Set.toList | |
| let consider = aList |> List.collect Cube.neighbours |> List.distinct | |
| printfn "found considered (%d)" (List.length consider) | |
| let a' = | |
| consider | |
| |> List.choose(fun p -> | |
| let l = Cube.activeN a p | |
| if a |> Set.contains p then | |
| if (l = 3 || l = 2) then Some p else None | |
| else | |
| if l = 3 then Some p else None) | |
| let cycle' = cycle + 1 | |
| printfn "Cycle %d done (%d) %ds" cycle' (Set.count a) (wc.ElapsedMilliseconds / 1000L) | |
| if cycle' < n then | |
| run (Set a') cycle' n | |
| else a' | |
| let c6 = run (Set starting) 0 6 | |
| c6 |> List.length |> printfn "part 2 %d" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment