one way to solve the problem 411 in clojure

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

Upload: filippo-vitale

Post on 17-Aug-2015

142 views

Category:

Presentations & Public Speaking


0 download

TRANSCRIPT

Page 1: One way to solve the problem 411 in clojure

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

Page 2: One way to solve the problem 411 in clojure

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"

Page 3: One way to solve the problem 411 in clojure

(hack-night (nth clj-syd 10))

Page 4: One way to solve the problem 411 in clojure

Problem 411

Page 5: One way to solve the problem 411 in clojure

Problem 411 outline

Generate2D Grid

of Stations

Calculatelongest

uphill path

Page 6: One way to solve the problem 411 in clojure

Problem 411 longest uphill path

longestpath = 5

Page 7: One way to solve the problem 411 in clojure

Problem 411 longest uphill path

n x n

Page 8: One way to solve the problem 411 in clojure

Problem 411 longest uphill path

n=22

Page 9: One way to solve the problem 411 in clojure

Problem 411 station generation

(x, y) = (2i mod n, 3i mod n)i = [0..2n]

Page 10: One way to solve the problem 411 in clojure

Problem 411 station generation

n = 2244 stations (2n)11 distinct stations

Page 11: One way to solve the problem 411 in clojure

Problem 411 30 instances

k = [1..30] n = k5

Page 12: One way to solve the problem 411 in clojure

Problem 411 30 instances

k = [1..30] n = k5

Σ solve(n)

Page 13: One way to solve the problem 411 in clojure

Problem 411 30 instances

k = [1..30] n = k5

Σ solve(n)

Page 14: One way to solve the problem 411 in clojure

Problem 411 n = k5

Page 15: One way to solve the problem 411 in clojure

Dynamic Programming Approach

Page 16: One way to solve the problem 411 in clojure

Naïve Algorithm generate stations

basei mod n(Modular Exponentiation)

Page 17: One way to solve the problem 411 in clojure

Naïve Algorithm generate stations

(x, y) = (2i mod n, 3i mod n)i = [0..2n]

Page 18: One way to solve the problem 411 in clojure

Naïve Algorithm generate stations

Page 19: One way to solve the problem 411 in clojure

Naïve Algorithm generate stations

Page 20: One way to solve the problem 411 in clojure

Naïve Algorithm dynamic programming

Page 21: One way to solve the problem 411 in clojure

Naïve Algorithm dynamic programming

max

Page 22: One way to solve the problem 411 in clojure

Naïve Algorithm dynamic programming

1+max*subproblem

Page 23: One way to solve the problem 411 in clojure

Naïve Algorithm dynamic programming

1+max*subproblem

Page 24: One way to solve the problem 411 in clojure

Naïve Algorithm dynamic programming

0

Page 25: One way to solve the problem 411 in clojure

Naïve Algorithm correct (but slow)

Page 26: One way to solve the problem 411 in clojure

Naïve Algorithm correct (but slow)

Page 27: One way to solve the problem 411 in clojure

Naïve Algorithm correct (but slow)

Page 28: One way to solve the problem 411 in clojure

Improvements:Station Generation

Page 29: One way to solve the problem 411 in clojure

Station Generation avoid repetitions

Page 30: One way to solve the problem 411 in clojure

Station Generation avoid repetitions

Page 31: One way to solve the problem 411 in clojure

Station Generation avoid repetitions

Page 32: One way to solve the problem 411 in clojure

Station Generation avoid repetitions

Page 33: One way to solve the problem 411 in clojure

Station Generation improve modpow

Page 34: One way to solve the problem 411 in clojure

Station Generation improve modpow

Page 35: One way to solve the problem 411 in clojure

Improvements:Station Generation

Page 36: One way to solve the problem 411 in clojure

Improvements:Station Generation

Data Structure

Page 37: One way to solve the problem 411 in clojure

QuadTree

Page 38: One way to solve the problem 411 in clojure

QuadTree

Page 39: One way to solve the problem 411 in clojure

Point QuadTree

Page 40: One way to solve the problem 411 in clojure
Page 41: One way to solve the problem 411 in clojure

*QuadTree

Page 42: One way to solve the problem 411 in clojure

SortedMap ofSortedSets DS

Page 43: One way to solve the problem 411 in clojure

SortedMap of SortedSet group by Y coord

Page 44: One way to solve the problem 411 in clojure

SortedMap of SortedSet group by Y coord

y=1

Page 45: One way to solve the problem 411 in clojure

SortedMap of SortedSet group by Y coord

y=3

Page 46: One way to solve the problem 411 in clojure

SortedMap of SortedSet group by Y coord

y=15

Page 47: One way to solve the problem 411 in clojure

SortedMap of SortedSet group by Y coord

Page 48: One way to solve the problem 411 in clojure

SortedMap of SortedSet group by Y coord

Page 49: One way to solve the problem 411 in clojure

SortedMap of SortedSet group by Y coord

Page 50: One way to solve the problem 411 in clojure

SortedMap of SortedSet group by Y coord

Page 51: One way to solve the problem 411 in clojure

SortedMap of SortedSet group by Y coord

Page 52: One way to solve the problem 411 in clojure

SortedMap of SortedSet group by Y coord

Page 53: One way to solve the problem 411 in clojure

SortedMap ofSortedSets DS

Now what ?!?!?

Page 54: One way to solve the problem 411 in clojure

SortedMap of SortedSet now what?!?!

same problem but 1D only... any leverage?

Page 55: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience Sorting

Page 56: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience Sorting

Page 57: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience Sorting

Page 58: One way to solve the problem 411 in clojure

SortedMap of SortedSet now what?!?!

.....

Page 59: One way to solve the problem 411 in clojure

SortedMap of SortedSet now what?!?!

Page 60: One way to solve the problem 411 in clojure

SortedMap of SortedSet now what?!?!

WTF ??!?!?!

Page 61: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience Sorting

Page 62: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience Sorting

Page 63: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience Sorting

Page 64: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience Sorting

Page 65: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience Sorting

Page 66: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience Sorting

Page 67: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience Sorting

Page 68: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience Sorting

Page 69: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience Sorting

[1 3 5 8 10][1 4 5 8 10][1 4 6 8 10]

...

Page 70: One way to solve the problem 411 in clojure

SortedMap ofSortedSets DS

Patience Algorithm ?

Page 71: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience

Page 72: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience

3

Page 73: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience

4

Page 74: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience Sorting

Page 75: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience

4

[1 10 12] --> [1 2 12] --> [1 2 12 20]

Page 76: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience

4

[1 10 12] --> [1 2 12] --> [1 2 12 20]

Page 77: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience

4

[1 2 12 20] --> [1 2 8 20] --> [1 2 8 14]

Page 78: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience

4

[1 2 8 14] --> [1 2 4 14] --> [ ? ? ? ]

Page 79: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience

5

[1 2 8 14] --> [1 2 4 14] --> [1 2 4 14 18]

Page 80: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience

5

[1 2 8 14] --> [1 2 4 14] --> [1 2 4 14 18]

Page 81: One way to solve the problem 411 in clojure

SortedMap of SortedSet Patience

5

[1 2 4 14 18] --> [1 2 4 6 18] --> [1 2 4 6 16]

Page 82: One way to solve the problem 411 in clojure

Patience AlgorithmPython to the Rescue

Page 83: One way to solve the problem 411 in clojure

Patience AlgorithmPython to the Rescue

Page 84: One way to solve the problem 411 in clojure

SortedMap of SortedSet Py to the rescue

Page 85: One way to solve the problem 411 in clojure

SortedMap of SortedSet Py to the rescue

Page 86: One way to solve the problem 411 in clojure

SortedMap of SortedSet Py to the rescue

Page 87: One way to solve the problem 411 in clojure

SortedMap of SortedSet Py to the rescue

Page 88: One way to solve the problem 411 in clojure

SortedMap of SortedSet Py to the rescue

Page 89: One way to solve the problem 411 in clojure

SortedMap of SortedSet Py to the rescue

http://hg.python.org/cpython/file/2.7/Lib/bisect.py

Page 90: One way to solve the problem 411 in clojure

SortedMap of SortedSet Py to the rescue

Page 91: One way to solve the problem 411 in clojure

SortedMap of SortedSet Py to the rescue

Page 92: One way to solve the problem 411 in clojure

SortedMap of SortedSet Py to the rescue

Page 93: One way to solve the problem 411 in clojure

SortedMap of SortedSet Py to the rescue

Page 94: One way to solve the problem 411 in clojure

SortedMap of SortedSet Py to the rescue

Page 95: One way to solve the problem 411 in clojure

SortedMap of SortedSet Py to the rescue

Page 96: One way to solve the problem 411 in clojure

SortedMap of SortedSet Py to the rescue

Page 97: One way to solve the problem 411 in clojure

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

Page 98: One way to solve the problem 411 in clojure

SortedMap of SortedSet LIS on our DS

[1 2 4 6 16]

Page 99: One way to solve the problem 411 in clojure

SortedMap of SortedSet LIS on our DS

[1 2 4 6 16]

Page 100: One way to solve the problem 411 in clojure

SortedMap of SortedSet LIS on our DS

[1 2 4 6 16]

Page 101: One way to solve the problem 411 in clojure

SortedMap of SortedSet LIS on our DS

[1 2 4 6 16]

Page 102: One way to solve the problem 411 in clojure

SortedMap of SortedSet LIS on our DS

[1 2 4 6 16]

Page 103: One way to solve the problem 411 in clojure

DS as a SortedSet

Page 104: One way to solve the problem 411 in clojure

DS as a SortedSet Full Scale?

k=30 --> n=305 --> 24,300,000

Page 105: One way to solve the problem 411 in clojure

DS as a SortedSet Full Scale?

k=30 --> n=305 --> 24,300,000

Everything --> "mod n"

Page 106: One way to solve the problem 411 in clojure

DS as a SortedSet Full Scale?

k=30 --> n=305 --> 24,300,000

Page 107: One way to solve the problem 411 in clojure

DS as a SortedSet XY combined

Page 108: One way to solve the problem 411 in clojure

DS as a SortedSet XY combined

Page 109: One way to solve the problem 411 in clojure

DS as a SortedSet XY combined

Page 110: One way to solve the problem 411 in clojure

DS as a SortedSet Stations-Y

Page 111: One way to solve the problem 411 in clojure

Here be Dragons

Page 112: One way to solve the problem 411 in clojure

Here be Dragons

Page 113: One way to solve the problem 411 in clojure

Optimisations innermost loops

"If a tree falls in a forest and no one is around to hear it, does it make a sound?"

Page 114: One way to solve the problem 411 in clojure

Optimisations innermost loops

Page 115: One way to solve the problem 411 in clojure

map - pmap - r/map

Page 116: One way to solve the problem 411 in clojure

*map you multicore, uh?

map

pmap

r/map

Page 117: One way to solve the problem 411 in clojure

*map you multicore, uh?

Page 118: One way to solve the problem 411 in clojure

*map you multicore, uh?

Page 119: One way to solve the problem 411 in clojure

what's next?

Page 120: One way to solve the problem 411 in clojure

What's next? chunked-seq

http://goo.gl/Peym4

Page 121: One way to solve the problem 411 in clojure

What's next? chunked-seq

Page 122: One way to solve the problem 411 in clojure

What's next? 1) measure 2) optimise

Page 123: One way to solve the problem 411 in clojure

What's next? 1) measure 2) optimise

Page 124: One way to solve the problem 411 in clojure

Questions?

Twitter @filippovitale BitBucket: filippovitale

GitHub: filippovitale

Page 125: One way to solve the problem 411 in clojure

Thank you!

Twitter @filippovitale BitBucket / GitHub: filippovitale

Page 126: One way to solve the problem 411 in clojure

f in