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

One way to solve the Problem 411 in Clojure – Clj-Syd May 2013

One way to solve the Problem 411 in Clojure – Clj-Syd May 2013

During the first clj-syd hack night we all discussed possible approaches on solving the ProjectEuler problem 411 and we started solving it.

After few weeks I finally solved it and this is how I did it.

Filippo Vitale

May 02, 2013
Tweet

More Decks by Filippo Vitale

Other Decks in Programming

Transcript

  1. clj-syd pep-411 "It was nice to meet you at the

    first clj-syd hack night"
  2. clj-syd pep-411 "It was nice to meet you at the

    first clj-syd hack night" aka "One way to solve the Problem 411 in Clojure"
  3. (hack-night (nth clj-syd 10))

  4. Problem 411

  5. Problem 411 outline Generate 2D Grid of Stations Calculate longest

    uphill path
  6. Problem 411 longest uphill path longest path = 5

  7. Problem 411 longest uphill path n x n

  8. Problem 411 longest uphill path n=22

  9. Problem 411 station generation (x, y) = (2i mod n,

    3i mod n) i = [0..2n]
  10. Problem 411 station generation n = 22 44 stations (2n)

    11 distinct stations
  11. Problem 411 30 instances k = [1..30] n = k5

  12. Problem 411 30 instances k = [1..30] n = k5

    Σ solve(n)
  13. Problem 411 30 instances k = [1..30] n = k5

    Σ solve(n)
  14. Problem 411 n = k5

  15. Dynamic Programming Approach

  16. Naïve Algorithm generate stations basei mod n (Modular Exponentiation)

  17. Naïve Algorithm generate stations (x, y) = (2i mod n,

    3i mod n) i = [0..2n]
  18. Naïve Algorithm generate stations

  19. Naïve Algorithm generate stations

  20. Naïve Algorithm dynamic programming

  21. Naïve Algorithm dynamic programming max

  22. Naïve Algorithm dynamic programming 1+max *subproblem

  23. Naïve Algorithm dynamic programming 1+max *subproblem

  24. Naïve Algorithm dynamic programming 0

  25. Naïve Algorithm correct (but slow)

  26. Naïve Algorithm correct (but slow)

  27. Naïve Algorithm correct (but slow)

  28. Improvements: Station Generation

  29. Station Generation avoid repetitions

  30. Station Generation avoid repetitions

  31. Station Generation avoid repetitions

  32. Station Generation avoid repetitions

  33. Station Generation improve modpow

  34. Station Generation improve modpow

  35. Improvements: Station Generation

  36. Improvements: Station Generation Data Structure

  37. QuadTree

  38. QuadTree

  39. Point QuadTree

  40. None
  41. *QuadTree

  42. SortedMap of SortedSets DS

  43. SortedMap of SortedSet group by Y coord

  44. SortedMap of SortedSet group by Y coord y=1

  45. SortedMap of SortedSet group by Y coord y=3

  46. SortedMap of SortedSet group by Y coord y=15

  47. SortedMap of SortedSet group by Y coord

  48. SortedMap of SortedSet group by Y coord

  49. SortedMap of SortedSet group by Y coord

  50. SortedMap of SortedSet group by Y coord

  51. SortedMap of SortedSet group by Y coord

  52. SortedMap of SortedSet group by Y coord

  53. SortedMap of SortedSets DS Now what ?!?!?

  54. SortedMap of SortedSet now what?!?! same problem but 1D only...

    any leverage?
  55. SortedMap of SortedSet Patience Sorting

  56. SortedMap of SortedSet Patience Sorting

  57. SortedMap of SortedSet Patience Sorting

  58. SortedMap of SortedSet now what?!?! .....

  59. SortedMap of SortedSet now what?!?!

  60. SortedMap of SortedSet now what?!?! WTF ??!?!?!

  61. SortedMap of SortedSet Patience Sorting

  62. SortedMap of SortedSet Patience Sorting

  63. SortedMap of SortedSet Patience Sorting

  64. SortedMap of SortedSet Patience Sorting

  65. SortedMap of SortedSet Patience Sorting

  66. SortedMap of SortedSet Patience Sorting

  67. SortedMap of SortedSet Patience Sorting

  68. SortedMap of SortedSet Patience Sorting

  69. SortedMap of SortedSet Patience Sorting [1 3 5 8 10]

    [1 4 5 8 10] [1 4 6 8 10] ...
  70. SortedMap of SortedSets DS Patience Algorithm ?

  71. SortedMap of SortedSet Patience

  72. SortedMap of SortedSet Patience 3

  73. SortedMap of SortedSet Patience 4

  74. SortedMap of SortedSet Patience Sorting

  75. SortedMap of SortedSet Patience 4 [1 10 12] --> [1

    2 12] --> [1 2 12 20]
  76. SortedMap of SortedSet Patience 4 [1 10 12] --> [1

    2 12] --> [1 2 12 20]
  77. SortedMap of SortedSet Patience 4 [1 2 12 20] -->

    [1 2 8 20] --> [1 2 8 14]
  78. SortedMap of SortedSet Patience 4 [1 2 8 14] -->

    [1 2 4 14] --> [ ? ? ? ]
  79. SortedMap of SortedSet Patience 5 [1 2 8 14] -->

    [1 2 4 14] --> [1 2 4 14 18]
  80. SortedMap of SortedSet Patience 5 [1 2 8 14] -->

    [1 2 4 14] --> [1 2 4 14 18]
  81. SortedMap of SortedSet Patience 5 [1 2 4 14 18]

    --> [1 2 4 6 18] --> [1 2 4 6 16]
  82. Patience Algorithm Python to the Rescue

  83. Patience Algorithm Python to the Rescue

  84. SortedMap of SortedSet Py to the rescue

  85. SortedMap of SortedSet Py to the rescue

  86. SortedMap of SortedSet Py to the rescue

  87. SortedMap of SortedSet Py to the rescue

  88. SortedMap of SortedSet Py to the rescue

  89. SortedMap of SortedSet Py to the rescue http://hg.python.org/cpython/file/2.7/Lib/bisect.py

  90. SortedMap of SortedSet Py to the rescue

  91. SortedMap of SortedSet Py to the rescue

  92. SortedMap of SortedSet Py to the rescue

  93. SortedMap of SortedSet Py to the rescue

  94. SortedMap of SortedSet Py to the rescue

  95. SortedMap of SortedSet Py to the rescue

  96. SortedMap of SortedSet Py to the rescue

  97. LIS in 1D is easy... What about our 2D DS?

  98. SortedMap of SortedSet LIS on our DS [1 2 4

    6 16]
  99. SortedMap of SortedSet LIS on our DS [1 2 4

    6 16]
  100. SortedMap of SortedSet LIS on our DS [1 2 4

    6 16]
  101. SortedMap of SortedSet LIS on our DS [1 2 4

    6 16]
  102. SortedMap of SortedSet LIS on our DS [1 2 4

    6 16]
  103. DS as a SortedSet

  104. DS as a SortedSet Full Scale? k=30 --> n=305 -->

    24,300,000
  105. DS as a SortedSet Full Scale? k=30 --> n=305 -->

    24,300,000 Everything --> "mod n"
  106. DS as a SortedSet Full Scale? k=30 --> n=305 -->

    24,300,000
  107. DS as a SortedSet XY combined

  108. DS as a SortedSet XY combined

  109. DS as a SortedSet XY combined

  110. DS as a SortedSet Stations-Y

  111. Here be Dragons

  112. Here be Dragons

  113. Optimisations innermost loops "If a tree falls in a forest

    and no one is around to hear it, does it make a sound?"
  114. Optimisations innermost loops

  115. map - pmap - r/map

  116. *map you multicore, uh? map pmap r/map

  117. *map you multicore, uh?

  118. *map you multicore, uh?

  119. what's next?

  120. What's next? chunked-seq http://goo.gl/Peym4

  121. What's next? chunked-seq

  122. What's next? 1) measure 2) optimise

  123. What's next? 1) measure 2) optimise

  124. Questions? Twitter @filippovitale BitBucket: filippovitale GitHub: filippovitale

  125. Thank you! Twitter @filippovitale BitBucket / GitHub: filippovitale

  126. f in