$30 off During Our Annual Pro Sale. View Details »

A Robot path problem – SydJS June 2012

A Robot path problem – SydJS June 2012

A Robot path problem helped me realise I still love JavaScript
Project Euler 208 – https://projecteuler.net/problem=208

Filippo Vitale

June 20, 2012
Tweet

More Decks by Filippo Vitale

Other Decks in Programming

Transcript

  1. A Robot path problem helped me realise I still love

    JavaScript
  2. Water-Cooler Talk

  3. Project Euler

  4. One-minute Rule "My program takes days to get to the

    answer!" Each problem has been designed according to a "one-minute rule", which means that although it may take several hours to design a successful algorithm with more difficult problems, an efficient implementation will allow a solution to be obtained on a modestly powered computer in less than one minute.
  5. Which programming language? http://shootout.alioth.debian.org/u64/which-programming-languages-are-fastest.php

  6. http://xkcd.com/568/

  7. None
  8. Project Euler - Problem 208

  9. Project Euler - Problem 208 A robot moves in a

    series of one-fifth circular arcs (72°)
  10. Project Euler - Problem 208 A robot moves in a

    series of one-fifth circular arcs (72°) with a free choice of ▪ a clockwise or ▪ an anti-clockwise arc for each step but no turning on the spot.
  11. Project Euler - Problem 208 One of 70932 possible closed

    paths of 25 arcs
  12. Project Euler - Problem 208 Given that the robot starts

    facing North: How many journeys of 70 arcs in length can it take that return it, after the final arc, to its starting position? (Any arc may be traversed multiple times) http://projecteuler.net/problem=208
  13. None
  14. Move left, move right 5 possible moves:

  15. Move left, move right How many closed path? 5 possible

    moves:
  16. Move left, move right 5 possible moves:

  17. N levels http://www.wolframalpha.com/input/?i=binary+tree+5

  18. 70 levels Number of grains of sand on earth! http://www.wolframalpha.com/input/?i=2%5E71

  19. None
  20. The plane

  21. The plane

  22. Sample elements

  23. Default coordinates

  24. Plane centered

  25. Positive up and right

  26. CSS code http://jsbin.com/evinum/edit#html,live http://jsfiddle.net/filippovitale/3z7Ln/embedded/

  27. None
  28. Arc left, Arc right

  29. Arc left, Arc right

  30. Arc left, Arc right

  31. Arc left, Arc right Basically we add:

  32. Arc left, Arc right Basically we add: http://jsbin.com/abaxoy/edit#preview

  33. None
  34. Move left, move right Model: • Move = { L,

    R } • StateI = ( x, y, heading ) StateI
  35. Move left, move right Model: • Move = { L,

    R } • StateI = ( x, y, heading ) • StateII = ( lx, ly, rx, ry ) StateI StateII
  36. Move left, move right

  37. Move left, move right

  38. Move left, move right

  39. Move left, move right

  40. Move left, move right

  41. Move left, move right

  42. Move left, move right How can we rotate a point

    on a plane? http://www.wolframalpha.com/input/?i=%7B100%2C+0%7D+72%C2%B0+rotation
  43. Move left, move right How can we rotate a point

    on a plane? http://www.wolframalpha.com/input/?i=%7B100%2C+0%7D+72%C2%B0+rotation http://www.wolframalpha.com/input/?i=with+center+%7B-50%2C0%7D+rotate+72+deg+%7B50%2C0%7D
  44. Move left, move right http://www.wolframalpha.com/input/?i=%7B100%2C+0%7D+72%C2%B0+rotation

  45. Move left, move right

  46. Move left, move right

  47. Move left, move right

  48. Move left, move right http://jsbin.com/anijuc/edit#preview ~0.000001

  49. None
  50. Brute force (naïve method)

  51. Brute force (naïve method) http://jsbin.com/ejebip/edit#preview

  52. None
  53. ABCDE Model A B C D E 0 1 2

    3 4
  54. ABCDE Model A B C D E 0 1 2

    3 4 heading 0 1 2 3 4
  55. ABCDE Model A B C D E 0 1 2

    3 4 heading counter 0 1 2 3 4 http://jsbin.com/oyifem/edit#preview
  56. None
  57. A way to solve this problem

  58. A way to solve this problem

  59. A way to solve this problem

  60. A way to solve this problem

  61. A way to solve this problem

  62. A way to solve this problem MaxDistance!

  63. A way to solve this problem

  64. A way to solve this problem ClosedPath!

  65. A way to solve this problem

  66. A way to solve this problem

  67. A way to solve this problem

  68. A way to solve this problem http://jsfiddle.net/filippovitale/hS9KS/embedded/result/ http://underscorejs.org/ + CoffeConsole

  69. None
  70. Conclusion ~2 seconds ??? seconds

  71. Conclusion ~2 seconds ~20 seconds

  72. Conclusion ~2 seconds ~20 seconds ~89 seconds

  73. Questions? twitter / bitbucket / github @filippovitale

  74. A Robot path problem helped me realise I still love

    JavaScript