Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 24th, 2011  |  syntax: F#  |  size: 0.96 KB  |  views: 369  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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)
clone this paste RAW Paste Data