In case you missed the announcement: The Alteryx One Fall Release is here! Learn more about the new features and capabilities here
ACT NOW: The Alteryx team will be retiring support for Community account recovery and Community email-change requests after December 31, 2025. Set up your security questions now so you can recover your account anytime, just log out and back in to get started. Learn more here
Start Free Trial

General Discussions

Discuss any topics that are not product-specific here.

Euleryx Project 15 – Lattice Paths

Pilsner
13 - Pulsar

Euleryx Problem 15 – Lattice Paths

Pilsner_3-1762469830229.png

 



My workflow:

Spoiler

 

Pilsner_4-1762469844033.png

 




Answer: 137846528820



Last Week's Favourite Solution:

There are join winners for last weeks favourite solution. Both @gawa  and @PangHC  identified different methods to reduce the number of records to compute the sequence for. This in turn reduced the required computation and therefore run time.  Please find their solutions on page one of last week's post or click here.

 

Definitions:

  • Lattice: The mathematical definition of a lattice can be confusing – “a partially ordered set in which every subset, consisting of two elements has a smallest upper bound and a greatest lower bound.”

    But what this effectively means is a set of points in a regular / repetitive pattern.

  • Factorial: The product of all numbers less than and equal to the chosen number. I.e The factorial of 5 would be

5 x 4 x 3 x 2 x 1 = 120

 

This can also be written as 5! = 120

Mathematical Theory:

This week the focus is going to be on something called the Binomial (Choose) function. We will work together to create this formula, whilst examining each element in the context of this weeks problem.

 

WhatAreYouTalkingAboutCupheadGIF.gif

 

First of all, why is the relevant? Well let’s consider a 2 by 2 grid. In order to get from the top of the grid to the bottom of the grid, I will need to make a total of 4 moves (two moves right, and two moves down). But importantly, the order of these moves does not matter. Lets consider the 4 potential moves (D = down, R = right, _ = currently unassigned):

 

_ _ _ _

 

Now lets decided upon the moves to the right. For one of the “R”s it could go in 4 potential positions.

1) R _ _ _                 2) _ R _ _                           3) _ _ R _                           4) _ _ _ R

But we still have another right hand move to make. For each of the examples above, there are 3 remaining spaces therefore 3 potential spots the final R could go in.

  1. R R _ _                R _ R _                R _ _ R
  2. R R _ _                _ R R _                _ R _ R
  3. R _ R _                _ R R _                _ _ R R
  4. R _ _ R                _ R _ R                _ _ R R

 

As we know there are 3 options, for each of the 4 initial moves, we can calculate the total options and equalling .

3 x 4 = 12

 

Now all the right hand moves have been placed, all the gaps can simply be filled with the down moves.

  1. R R D D                             R D R D                             R D D R
  2. R R D D                             D R R D                             D R D R
  3. R D R D                             D R R D                             D D R R
  4. R D D R                             D R D R                             D D R R

 

What about a 3 by 3 grid? In this case we would have to make a total of 6 moves, 3 down and 3 to the right. If we follow the same logical thinking as before, we can conclude that:

 

  • Firstly, we have 6 spaces that the first “R” could go it
  • For each of those 6 options, there a 5 potential spaces for the second “R”
  • For the 3rd and final “R” there are 4 potential spaces per combination above.


That makes a total of:

6 x 5 x 4 = 120 possible options/ routes through the grid

 

This formula can actually be generalised. If the grid is k x k, we know we need to make 2k ( which we will call n) moves to get through the grid. Combining this with the above logic, we know that the number of paths through the grid is equal to:

n x (n – 1) x (n – 2) x …… x (k +1)

 

ThisOneIsALittleBitTrickyAdamGIF.gif

But this isn’t very nice to write out each time. To make things simpler lets multiply the above equation by 1. Remember, anything divided by itself = 1.

Lets take 

Pilsner_5-1762470170554.png

 

If we multiply the original equaiton by this we get:

Pilsner_6-1762470197453.png

 

 

Interestingly, this can then be written as:

Pilsner_7-1762470227977.png

 



You may have spotted that the numerator of that fraction is now simply the product of every single number less than or equal to n. By definition, this is the factorial of n, so the equation can be written as:

Pilsner_8-1762470298475.png

 

 

We are nearly there. This formula will yield the values we looked at earlier (12 options for the 2x2 grid and 120 options for the 3x3 grid) but there is something we missed. If we look back at the 2 x 2 example, you will notice that there are quite a few sequences that appear more than once:

 

  1. R R D D                             R D R D                             R D D R
  2. R R D D                             D R R D                             D R D R
  3. R D R D                             D R R D                             D D R R
  4. R D D R                             D R D R                             D D R R

That is because we have assumed that the order of the rights matters. In reality, all the right hand moves (and downs) are equal, to mitigate this we need to adjust out equation.

 

 

For the 2 x 2 case there are 2 right hand moves, if we did care about the order, and tried to arrange 2 values, the number of possible options would be 2 x 1 = 2! = 2. As we do not care about the order when considering the path through the grid, we need to divide our previous answer, by this value to get 12/2 = 6 .

 

 

As for the 3 x 3 grid. If we did care about the order of 3 values, there would be 3 x 2 x 1 = 3! = 6 Possible ways to rearrange them. But as we do not care about the order in our problem, the answer is 120/6 = 20

 

 

The final step is to generalise this. You will notice that we have simply divided our answer by the factorial value of our dimension. k. This can be expressed as k! or as (n-k)! (as n = 2k). Adding this into our formula from before we get the following:

Pilsner_9-1762470353205.png

 

And that’s it. We now have a formula to calculate the number of paths through our grids! If you want to find out more, I would encourage you to look into this formula. Its actually a very well know formula often referred to as the binomial function or choose function.


Method:

 

1) Create an input in which the grid length is defined.

Pilsner_1-1762469595982.png

 



2) Implement the formula from earlier. As the grid is a 20 x 20, we need to move a total of 40 spaces to get out. Therefore n = 40 (which is 2 times the dimension)

If we choose the order of all 20 horizontal moves (k = 20) the 20 vertical moves will have to take the remaining spaces.

Hence the formula reads:

Pilsner_2-1762469616318.png

 



Pilsner_0-1762469577741.png

 


3) Submit your answer to the Project Euler Website!

DoneSoDoneGIF.gif



Summary:

The formula itself is not immediately obvious however once you are aware of it, this problem becomes a simple substitution exercise, solvable in just one tool!

Want to find out more, follow this link to our introduction post - Euleryx: Let The Games Begin.

10 REPLIES 10
Hub119
12 - Quasar
12 - Quasar

Having built many different maze path finder macros for AoC I started by going down the BFS approach to build a path finder macro... then immediately realized that was going to be infeasible and would blow up my record counts... so did some quick research and went with the nice quick math formula for the easy solve.

Spoiler
PE-15.png
patrick_digan
17 - Castor
17 - Castor
Spoiler
I decided to copy/paste instead of building a macro. It ran in about a second, so I was happy. 
image.png
jrlindem
12 - Quasar

I'm done feeling guilty about not fully comprehending the math concepts.  Thank you @Pilsner for being a better math teacher than I ever had in grade school (no sarcasm intended).

 

Now, with the math theory in hand, adapting to Alteryx.

 

I was going to be "cute" and break this down into the individual pieces in the formula tool (ex: Numerator and Denominator, separately); but I got bit by the large number problem where Alteryx didn't want to show those obscenely large numbers on their own, so I resorted to putting all in one formula like the original example.

 

Here's my solution, nevertheless:

 

Spoiler
jrlindem_0-1762550928119.png

 

jrlindem
12 - Quasar

Also, @patrick_digan ...  your solution is bonkers.  You crack me up!  Well done 😆

AkimasaKajitani
17 - Castor
17 - Castor

Just use formula!

 

Spoiler
AkimasaKajitani_0-1762562842704.png

 

DaisukeTsuchiya
14 - Magnetar
14 - Magnetar

This is definitely overkill for this problem, but I'm using WF for arbitrary-precision arithmetic.

Spoiler
Many of the Project Euler problems, especially from around #50, involve calculations that exceed the limits of INT64.
I actually built this workflow in the weekend to tackle such issues to solve the combinatorics problem in #53, where some intermediate values grew to over 150 digits long. So, I figured I might as well use it here too.

スクリーンショット 2025-11-10 091453.jpgスクリーンショット 2025-11-10 091517.jpgスクリーンショット 2025-11-10 091531.jpgスクリーンショット 2025-11-10 091546.jpg

 

PangHC
13 - Pulsar

no idea how the math work...

using groupby and count. where first method that I learn from AOC. the lantern fish. 

PangHC_0-1762745500921.png

Spoiler
macro workflow
PangHC_1-1762745637329.png

 

 

Qiu
21 - Polaris
21 - Polaris

Obiviously I am using a different formula.

Spoiler
Euleryx Project 15.jpg
WirkKarl
8 - Asteroid

Reducing the number of records before computing sequences is such a neat optimization — really shows how a small tweak can cut run time dramatically.

Excited to dive into this week’s Binomial function challenge and see how everyone applies it in Alteryx workflows. Always a great way to sharpen both math and tool skills! 💪

Labels
Top Solution Authors