Skip to content

Instantly share code, notes, and snippets.

@object
Last active December 6, 2022 05:56
Show Gist options
  • Select an option

  • Save object/75fa6b55b9ac00f72886a1c98d5e7a74 to your computer and use it in GitHub Desktop.

Select an option

Save object/75fa6b55b9ac00f72886a1c98d5e7a74 to your computer and use it in GitHub Desktop.
AdventOfCode2022 December 6
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