Created
May 1, 2012 21:27
-
-
Save lefthandedgoat/2571546 to your computer and use it in GitHub Desktop.
F# Map Kata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let mutable tests = [] | |
let test f = tests <- List.append tests [f] | |
let run _ = List.map (fun f -> (f ())) tests | |
let runp _ = List.toArray tests |> Array.Parallel.map (fun f -> (f ())) | |
let (==) value1 value2 = System.Console.WriteLine("{0} expected: {1} got: {2}", (value1 = value2), value2, value1) | |
let describe description = System.Console.WriteLine(description.ToString()) | |
type Cell = { x : int; y : int; moves : int } | |
let rec go (canGo : Cell list) (toTest : Cell list) (obstacles : Cell list)= | |
let mutable newCanGo = canGo | |
let mutable newToTest = toTest.Tail | |
let alreadyChecked cell cg o tt = | |
List.exists (fun c -> c.x = cell.x && c.y = cell.y) cg | |
|| List.exists (fun c -> c.x = cell.x && c.y = cell.y) o | |
|| List.exists (fun c -> c.x = cell.x && c.y = cell.y) tt | |
let aCell = toTest.Head | |
if aCell.moves > 0 then | |
let up = { x = aCell.x; y = aCell.y + 1; moves = aCell.moves - 1} | |
let down = { x = aCell.x; y = aCell.y - 1; moves = aCell.moves - 1} | |
let left = { x = aCell.x - 1; y = aCell.y; moves = aCell.moves - 1} | |
let right = { x = aCell.x + 1; y = aCell.y; moves = aCell.moves - 1} | |
if (alreadyChecked up newCanGo obstacles newToTest) = false then | |
newCanGo <- newCanGo @ [up] | |
newToTest <- newToTest @ [up] | |
if (alreadyChecked down newCanGo obstacles newToTest) = false then | |
newCanGo <- newCanGo @ [down] | |
newToTest <- newToTest @ [down] | |
if (alreadyChecked left newCanGo obstacles newToTest) = false then | |
newCanGo <- newCanGo @ [left] | |
newToTest <- newToTest @ [left] | |
if (alreadyChecked right newCanGo obstacles newToTest) = false then | |
newCanGo <- newCanGo @ [right] | |
newToTest <- newToTest @ [right] | |
if newToTest = [] then | |
newCanGo | |
else | |
go newCanGo newToTest obstacles | |
let sort cells = | |
let sortFunction (cell1 : Cell) (cell2 : Cell) = | |
if cell1.x > cell2.x || (cell1.x = cell2.x && cell1.y > cell2.y) then 1 else -1 | |
List.sortWith sortFunction cells | |
test (fun _ -> | |
describe "use at 2,2 with 0 moves" | |
let obstacles = [] | |
let expected = [{ x = 2; y = 2; moves = 0}] | |
let start = [{ x = 2; y = 2; moves = 0}] | |
let result = go start start obstacles | |
result == expected | |
) | |
test (fun _ -> | |
describe "use at 2,2 with 1 move" | |
let obstacles = [] | |
let expected = sort [{ x = 2; y = 2; moves = 1}; { x = 1; y = 2; moves = 0}; { x = 2; y = 1; moves = 0}; { x = 2; y = 3; moves = 0}; { x = 3; y = 2; moves = 0}] | |
let start = [{ x = 2; y = 2; moves = 1}] | |
let result = sort (go start start obstacles) | |
result == expected | |
) | |
test (fun _ -> | |
describe "use at 2,2 with 2 move" | |
let obstacles = [] | |
let expected = sort [{ x = 2; y = 2; moves = 2}; | |
{ x = 1; y = 2; moves = 1}; | |
{ x = 2; y = 1; moves = 1}; | |
{ x = 2; y = 3; moves = 1}; | |
{ x = 3; y = 2; moves = 1}; | |
{ x = 2; y = 0; moves = 0}; | |
{ x = 1; y = 1; moves = 0}; | |
{ x = 0; y = 2; moves = 0}; | |
{ x = 1; y = 3; moves = 0}; | |
{ x = 2; y = 4; moves = 0}; | |
{ x = 3; y = 3; moves = 0}; | |
{ x = 4; y = 2; moves = 0}; | |
{ x = 3; y = 1; moves = 0}; | |
] | |
let start = [{ x = 2; y = 2; moves = 2}] | |
let result = sort (go start start obstacles) | |
result == expected | |
) | |
test (fun _ -> | |
describe "use at 2,2 with 2 move and an obstacle" | |
let obstacles = [{ x = 1; y = 2; moves = 0}] | |
let expected = sort [{ x = 2; y = 2; moves = 2}; | |
//{ x = 1; y = 2; moves = 1}; Cant because of obstacle | |
{ x = 2; y = 1; moves = 1}; | |
{ x = 2; y = 3; moves = 1}; | |
{ x = 3; y = 2; moves = 1}; | |
{ x = 2; y = 0; moves = 0}; | |
{ x = 1; y = 1; moves = 0}; | |
//{ x = 0; y = 2; moves = 0}; Cant because of obstacle | |
{ x = 1; y = 3; moves = 0}; | |
{ x = 2; y = 4; moves = 0}; | |
{ x = 3; y = 3; moves = 0}; | |
{ x = 4; y = 2; moves = 0}; | |
{ x = 3; y = 1; moves = 0}; | |
] | |
let start = [{ x = 2; y = 2; moves = 2}] | |
let result = sort (go start start obstacles) | |
result == expected | |
) | |
test (fun _ -> | |
let stopWatch = new System.Diagnostics.Stopwatch() | |
stopWatch.Start() | |
describe "performance test" | |
let obstacles = [{ x = 1; y = 2; moves = 0}] | |
let start = [{ x = 2; y = 2; moves = 100}] | |
let result = sort (go start start obstacles) | |
System.Console.WriteLine(result.Length) | |
stopWatch.Stop() | |
System.Console.WriteLine() | |
System.Console.WriteLine("{0} seconds to execute", stopWatch.Elapsed.Seconds) | |
() | |
) | |
run() | |
System.Console.ReadKey() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment