Advertisement
Guest User

Untitled

a guest
Sep 24th, 2011
1,284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.96 KB | None | 0 0
  1. open System.Collections.Generic
  2.  
  3. let visited = new Dictionary<_,_>(HashIdentity.Structural)
  4.  
  5. let rec walk x y =
  6.     let addDigits num =
  7.         let rec sumInner n soFar =
  8.             match n with
  9.             | x when x<10  -> soFar+x
  10.             | x -> sumInner (n/10) (soFar + n % 10)
  11.         sumInner num 0
  12.     let rec innerWalk x y totalPoints =
  13.         let mycell = (x,y)
  14.         match visited.TryGetValue(mycell) with
  15.         | true,_ -> totalPoints
  16.         | _    ->
  17.             (* printfn "%d,%d" x y *)
  18.             visited.[mycell] <- true
  19.             let digitSum = (addDigits x) + (addDigits y)
  20.             match digitSum with
  21.             | n when n>25 ->
  22.                 totalPoints
  23.             | n -> List.fold
  24.                     (fun total (dx,dy) -> total + walk (x+dx) (y+dy))
  25.                     (totalPoints+1)
  26.                     [(1,0);(-1,0);(0,1);(0,-1)]
  27.     innerWalk x y 0
  28.  
  29. let _ =
  30.     printfn "Points: %d\n" (walk 1000 1000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement