Skip to content

Instantly share code, notes, and snippets.

@object
Created December 23, 2020 06:45
Show Gist options
  • Select an option

  • Save object/5720fa89f71e2b4244b1cd977d78938f to your computer and use it in GitHub Desktop.

Select an option

Save object/5720fa89f71e2b4244b1cd977d78938f to your computer and use it in GitHub Desktop.
AdventOfCode 2020, December 23
module TwentyThree =
open System
let testInput = "389125467"
let input = "942387615"
let cups = (Seq.map ((fun x -> x.ToString()) >> Int32.Parse) input) |> Seq.toList
let rec findDestination (cups : int list) pickup destination =
if not (pickup |> List.contains destination) then
destination
else
findDestination cups pickup (if destination = 1 then cups.Length else destination-1)
let makeMove cups =
let current = cups |> List.head
let pickup = cups |> List.skip 1 |> List.take 3
let destination = findDestination cups pickup (if current = 1 then cups.Length else current-1)
let destIndex = cups |> List.findIndex (fun x -> x = destination)
let res =
List.concat [
cups |> List.skip 4 |> List.truncate (destIndex-4)
destination :: (List.append pickup (cups |> List.skip (destIndex+1))) @ [current]
]
res
let rec play cups n =
if n = 0 then cups
else play (makeMove cups) (n - 1)
play cups 100
|> fun cups ->
let ndx = cups |> List.findIndex (fun x -> x = 1)
[cups |> List.skip (ndx+1); cups |> List.take ndx] |> List.concat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment