Skip to content

Instantly share code, notes, and snippets.

@bjartwolf
Last active August 29, 2015 14:20
Show Gist options
  • Save bjartwolf/0d702e5638025b0b6bcd to your computer and use it in GitHub Desktop.
Save bjartwolf/0d702e5638025b0b6bcd to your computer and use it in GitHub Desktop.
initial
type symbol = Number of int | Plus | Minus
let rec calc (s: symbol list) : int =
match s with
| [] -> 0
| [Number x] -> x
| Number x :: Number y :: rest -> calc(Number ((int)(sprintf "%d%d" x y)) :: rest)
| Number x :: rest -> x + calc(rest)
| Plus :: rest -> + calc(rest)
| Minus :: rest -> - calc(rest)
let rec genPossibleSolutions (start: int) (stop: int) = seq {
if start = stop then yield [Number stop]
else for n in start .. start do
for i in genPossibleSolutions (start + 1) stop do
yield Number n :: i
yield Number n :: Minus :: i
yield Number n :: Plus:: i
}
genPossibleSolutions 1 9 |> Seq.filter (fun x -> 100 = calc x) |> Seq.toList
|> List.length |> printfn "Is %d long"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment