Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cmbrown1598
Last active September 24, 2019 18:30
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 cmbrown1598/8844eeed27a2e0f92d94b5661842bac9 to your computer and use it in GitHub Desktop.
Save cmbrown1598/8844eeed27a2e0f92d94b5661842bac9 to your computer and use it in GitHub Desktop.
#r "Facades/netstandard"
#r "netstandard"
let vals = [1;2;3;4;5;6;7;8;9]
let operations = [(+);(-);(fun x y -> (x * 10) + (y))]
let render ops expectedResult =
let s op =
if op 1 1 = 2 then Some " + "
else if op 1 1 = 0 then Some " - "
else if op 1 1 = 11 then Some ""
else None
let strs = ops |> List.map s
if List.length strs = ((List.length vals) - 1) && List.forall Option.isSome strs then
(vals |> List.mapi (fun i v -> match List.tryItem i strs with | Some a -> (string v) + Option.get a | None -> string v) |> List.reduce (+)) + " = " + (string expectedResult)
else
"Error."
let test ops expectedResult =
let fn = List.zip ops (List.tail vals)
let mutable m = List.head vals
for o, v in fn
do
m <- o m v
m = expectedResult
// there are only 6561 possible results here... this is pretty painless to eval all options of.
let answer expectedResult =
let filter m = test m expectedResult
let r t = render t expectedResult
let allAnswers = [
for a in 0..2 do
for b in 0..2 do
for c in 0..2 do
for d in 0..2 do
for e in 0..2 do
for f in 0..2 do
for g in 0..2 do
for h in 0..2 do
yield
[operations.[a];operations.[b];operations.[c];operations.[d];operations.[e];operations.[f];operations.[g];operations.[h]]
]
allAnswers |>
List.filter filter |> List.fold (fun s t -> sprintf "%s\r\n%s" s (r t)) ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment