Last active
December 6, 2022 05:56
-
-
Save object/75fa6b55b9ac00f72886a1c98d5e7a74 to your computer and use it in GitHub Desktop.
AdventOfCode2022 December 6
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
| open System | |
| open System.IO | |
| let input = | |
| File.ReadAllLines(__SOURCE_DIRECTORY__ + "input06.txt") | |
| |> Seq.head | |
| |> Seq.toArray | |
| // First solution uses "windowed" function. It is not memory efficient because it allocates large number of arrays | |
| // (the number of allocated arrays is approximately the count of characters in input string) | |
| let getResult distinctLength = | |
| input | |
| |> Array.windowed distinctLength | |
| |> Array.findIndex (fun range -> range |> Array.distinct |> Array.length = distinctLength) | |
| |> (+) distinctLength | |
| // Part One | |
| getResult 4 | |
| // Part Two | |
| getResult 14 | |
| // Second solution uses recursive function and does not suffer from excessive memory allocation | |
| let rec getResult chars ndx distinctLength = | |
| if chars |> Array.truncate distinctLength |> Array.distinct |> Array.length = distinctLength | |
| then ndx + distinctLength | |
| else getResult (chars |> Array.skip 1) (ndx+1) distinctLength | |
| // Part One | |
| getResult input 0 4 | |
| // Part Two | |
| getResult input 0 14 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment