Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 24th, 2011  |  syntax: OCaml  |  size: 0.93 KB  |  views: 432  |  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. let visited = Hashtbl.create 200000
  2.  
  3. let rec walk x y =
  4.     let addDigits number =
  5.         let rec sumInner n soFar =
  6.             match n with
  7.             | x when x<10  -> soFar+x
  8.             | x -> sumInner (n/10) (soFar + n mod 10) in
  9.         sumInner number 0 in
  10.     let rec innerWalk x y totalPoints =
  11.         match Hashtbl.mem visited (x,y) with
  12.         | true -> totalPoints
  13.         | _    -> begin
  14.             Printf.printf "%d,%d\n" x y ;
  15.             Hashtbl.add visited (x,y) 1 ;
  16.             let digitSum = (addDigits x) + (addDigits y) in
  17.             match digitSum with
  18.             | n when n>25 ->
  19.                 totalPoints
  20.             | n -> List.fold_left
  21.                     (fun total (dx,dy) -> total + walk (x+dx) (y+dy))
  22.                     (totalPoints+1)
  23.                     [(1,0);(-1,0);(0,1);(0,-1)]
  24.         end in
  25.     innerWalk x y 0
  26.  
  27. let _ =
  28.     Printf.printf "Points: %d\n" (walk 1000 1000)