Advertisement
Guest User

Untitled

a guest
Sep 24th, 2011
1,660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.93 KB | None | 0 0
  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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement