Skip to content

Instantly share code, notes, and snippets.

@henrik-ch
Created May 29, 2012 18:41
Show Gist options
  • Save henrik-ch/2829955 to your computer and use it in GitHub Desktop.
Save henrik-ch/2829955 to your computer and use it in GitHub Desktop.
solution to the heathrow route planning problem from learnyousomeerlang.com
type Move =
| AFwd
| BFwd
| XCross
// list of amove, bmove, xcross moves tuples (costs)
let moveCosts = [(50, 10, 30); (5, 90, 20); (40, 2, 25); (10, 8,0)]
let bottomGraphVal = ((0, ([]: Move list)), (0, ([] : Move list)))
let calcNewSetAndMoves ((aVal, (aRoute)), (bVal, (bRoute))) (aFwd, bFwd, xCross) =
let valToAStraightFromA = aVal + aFwd
let valToACrossFromB = bVal + bFwd + xCross
let newAVal = min valToAStraightFromA valToACrossFromB
let newAMoves =
if valToAStraightFromA < valToACrossFromB then AFwd :: aRoute
else XCross :: BFwd :: bRoute
//-----------------------------------
let valToBStraightFromB = bVal + bFwd
let valToBCrossFromA = aVal + aFwd + xCross
let newBVal = min valToBStraightFromB valToBCrossFromA
let newBMoves =
if valToBStraightFromB < valToBCrossFromA then BFwd :: bRoute
else XCross :: AFwd :: aRoute
((newAVal, newAMoves), (newBVal, newBMoves))
let myTestRes = calcNewSetAndMoves bottomGraphVal (50, 10, 30)
let foldAccFn =
fun (curState) (curEntry ) ->
calcNewSetAndMoves curState curEntry
let main =
let retVal = List.fold foldAccFn bottomGraphVal moveCosts
let ((aVal,aList),(bVal,bList)) = retVal
(aVal, List.rev aList, bVal, List.rev bList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment