Skip to content

Instantly share code, notes, and snippets.

@plecong
Created December 10, 2015 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plecong/0d9845558504a2eb6b3f to your computer and use it in GitHub Desktop.
Save plecong/0d9845558504a2eb6b3f to your computer and use it in GitHub Desktop.
open System
open System.Text
module List =
let rec skip n xs =
match (n, xs) with
| 0, _ -> xs
| _, [] -> []
| n, _::xs -> skip (n-1) xs
let rec countChar (c : char) (acc : int) (xs : char list) =
match (c, xs) with
| _, [] -> acc
| a, b::c when a = b -> countChar a (acc + 1) c
| _ -> acc
let rec seeAndSayLoop (acc : (int * char) list) (input : char list) : (int * char) list =
if input.IsEmpty then
acc
else
let f = input.Head
let c = countChar f 0 input
seeAndSayLoop ((c, f) :: acc) (input |> List.skip c)
let seeAndSay (input : char list) =
seeAndSayLoop List.empty input
|> List.rev
|> List.fold (fun (b : StringBuilder) (i, c) -> b.Append(i).Append(c)) (new StringBuilder())
|> (fun s -> s.ToString().ToCharArray() |> Array.toList)
let foldingSeeAndSay (input : string) (c : int) =
let n = input.ToCharArray() |> Array.toList
[1..c]
|> Seq.fold (fun a i -> seeAndSay a) n
|> (fun x -> x.Length)
let part1 = foldingSeeAndSay "3113322113" 40
let part2 = foldingSeeAndSay "3113322113" 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment