r/dailyprogrammer • u/Elite6809 1 1 • Dec 30 '15
[2015-12-30] Challenge #247 [Intermediate] Moving (diagonally) Up in Life
(Intermediate): Moving (diagonally) Up in Life
Imagine you live on a grid of characters, like the one below. For this example, we'll use a 2*2 grid for simplicity.
. X
X .
You start at the X at the bottom-left, and you want to get to the X at the top-right. However, you can only move up, to the right, and diagonally right and up in one go. This means there are three possible paths to get from one X to the other X (with the path represented by -, + and |):
+-X . X . X
| / |
X . X . X-+
What if you're on a 3*3 grid, such as this one?
. . X
. . .
X . .
Let's enumerate all the possible paths:
+---X . +-X . +-X . +-X . . X . +-X . . X
| / | | / | |
| . . + . . +-+ . . + . . / . . | . +---+
| | | / / | |
X . . X . . X . . X . . X . . X-+ . X . .
. . X . . X . . X . . X . . X . . X
/ | | | | /
. + . . +-+ . . + . . | . +-+ +-+ .
| | / | / |
X-+ . X-+ . X-+ . X---+ X . . X . .
That makes a total of 13 paths through a 3*3 grid.
However, what if you wanted to pass through 3 Xs on the grid? Something like this?
. . X
. X .
X . .
Because we can only move up and right, if we're going to pass through the middle X then there is no possible way to reach the top-left and bottom-right space on the grid:
. X
. X .
X .
Hence, this situation is like two 2*2 grids joined together end-to-end. This means there are 32=9 possible paths through the grid, as there are 3 ways to traverse the 2*2 grid. (Try it yourself!)
Finally, some situations are impossible. Here, you cannot reach all 4 Xs on the grid - either the top-left or bottom-right X must be missed:
X . X
. . .
X . X
This is because we cannot go left or down, only up or right - so this situation is an invalid one.
Your challenge today is, given a grid with a certain number of Xs on it, determine first whether the situation is valid (ie. all Xs can be reached), and if it's valid, the number of possible paths traversing all the Xs.
Formal Inputs and Outputs
Input Specification
You'll be given a tuple M, N on one line, followed by N further lines (of length M) containing a grid of spaces and Xs, like this:
5, 4
....X
..X..
.....
X....
Note that the top-right X need not be at the very top-right of the grid, same for the bottom-left X. Also, unlike the example grids shown above, there are no spaces between the cells.
Output Description
Output the number of valid path combinations in the input, or an error message if the input is invalid. For the above input, the output is:
65
Sample Inputs and Outputs
Example 1
Input
3, 3
..X
.X.
X..
Output
9
Example 2
Input
10, 10
.........X
..........
....X.....
..........
..........
....X.....
..........
.X........
..........
X.........
Output
7625
£xample 3
Input
5, 5
....X
.X...
.....
...X.
X....
Output
<invalid input>
Example 4
Input
7, 7
...X..X
.......
.......
.X.X...
.......
.......
XX.....
Output
1
Example 5
Input
29, 19
.............................
........................X....
.............................
.............................
.............................
.........X...................
.............................
.............................
.............................
.............................
.............................
.....X.......................
....X........................
.............................
.............................
.............................
XX...........................
.............................
.............................
Output
19475329563
Example 6
Input
29, 19
.............................
........................X....
.............................
.............................
.............................
.........X...................
.............................
.............................
.............................
.............................
.............................
....XX.......................
....X........................
.............................
.............................
.............................
XX...........................
.............................
.............................
Output
6491776521
Finally
Got any cool challenge ideas? Submit them to /r/DailyProgrammer_Ideas!
5
u/Elite6809 1 1 Dec 30 '15
My F# solution, O(n) in the number of rows.
3
u/abyssalheaven 0 1 Dec 30 '15 edited Dec 30 '15
You've basically defined a Delannoy number. Your definition of a grid of n x m dots, is a n-1 x m-1 grid of squares for Delannoy.
EDIT: didn't read the part about other points on the grid, but you can just break it into other Delannoy squares if it's valid.
2
u/[deleted] Dec 30 '15 edited Dec 30 '15
Edit: The link to two sheets of my trying to do the right math: http://imgur.com/ZY8wKrq
I tried to figure out the math by myself - 3 hours later and 5 1/2 page full with beautifull dots and numbers, I learned a good bit combinatorics and discovered the Delannoy numbers, which is like the solution to this challenge. Now that I solved it, I am too tired to actually programm the solution, but on the other hand incredible proud on myself :)
Small lazy outline of the would-have-been programm:
5
u/ponkanpinoy Dec 30 '15 edited Dec 30 '15
Common Lisp. We'll first start by figuring out if a list of points forms a valid path. This is true if and only if the x and y values of the points are monotonically increasing.
Next, let's count the number of paths from
(0, 0)to(x, y). This is very similar to the Lattice Path problem, except that as well as being able to go north and east, you can also go northeast. The standard Lattice Path problem has a O(1) solution, however since we also allow diagonal movement it doesn't quite work here. Instead we'll use a dynamic programming solution. If either of the dimensions is 0 (that is, you can only go in a straight line), there is only one path. Otherwise, the number of paths is equal to the sum ofLATTICE-PATH(x-1, y),LATTICE-PATH(x, y-1), andLATTICE-PATH(x-1, y-1). This runs in O(x*y) time and space.EDIT: there is a solution that runs in O(MAX(x, y)). This is left as an exercise for the reader ;)
EDIT2 TIL about Delannoy numbers.
The number of paths given waypoints is simply the product of each segment. Before doing that we check to see that the path is indeed valid.
Turning the input grid into a set of points:
Challenge inputs and outputs:
Inspired by /u/a_Happy_Tiny_Bunny's optimization challenges:
1
u/ReckoningReckoner Dec 31 '15
Python 3. A solution that doesn't use delannoy numbers for the non-math majors :P
Solves the last input instantaneously.
1
u/bmc__ Dec 31 '15
Thank for all of your input, far more than I could ever ask for. I will definitely check out some practical problems for common lisp or scheme since I already know some scheme and its light weight. Do you know of any good lisp editors or how to start on Linux?
3
u/[deleted] Dec 30 '15 edited Dec 30 '15
Python.
3
u/Godspiral 3 3 Dec 30 '15 edited Dec 30 '15
In J, with full path list generator,
13 5
7625 NB. ex#2
gets the big 2 in under half a second.
a valid check function,
called on
inreturns 0 if invalid, or num paths if valid0
3
u/a_Happy_Tiny_Bunny Dec 30 '15 edited Dec 31 '15
I propose these inputs as optimization challenges
Easy
Medium
You will need 64-bit integers for this one.
Hard
You will need arbitrary precision integer for this one.
Just manually input the coordinates (0, 0) and (999, 999), corresponding to a 1000x1000 grid with the Xs at the lower-left and upper-right corners, into your function that calculates the number of steps. Or generate an actual input file if you'd like, I just can't post such a large input because of the character limitation.
My solution for these challenges:
Haskell
Answer for medium:
Answer for hard:
EDIT: Alternative implementation using good-old lists; still fast enough but 20-100% slower (for small inputs to about 2000long side of square grid). The Hard input runs in about 1.45s, while the table version runs in about 0.9s. However, the Table version hangs my laptop because of its memory hungriness at about 2100-2200, while the list version finishes running size 4000 (while barely swapping) in about 85s.
These were all measured on my 6 year-old laptop with 4GB of RAM, and the process can take about 2,200 MB before swapping.
3
u/Sirflankalot 0 1 Dec 31 '15 edited Jan 03 '16
My C++ solution. This is the first ever working c++ program that I've ever written, so any assistance would be amazing. Gets solution to all challenges in under 0.002 seconds.
2
u/a_Happy_Tiny_Bunny Dec 30 '15 edited Dec 30 '15
Haskell
Very straightforward code. Should be understandable by Haskell beginners.
EDIT: Added input validation. Now basic understanding of
Maybeis required to fully understand the code.I might code a faster implementation that can solve the biggest grids later today.There was a bug that produced huge coordinates.2
u/New_Kind_of_Boredom Dec 30 '15 edited Dec 31 '15
Python 3, cheated and used the Delannoy number as suggested by /u/Godspiral here. Work smarter not harder right?
Solves the 1000x1000 grid in 1.51s on my crappy Celeron 847 @ 1.10GHz, 2GB RAM netbook.
Example input:
This 1000x1000 grid. (warning, almost 1mb)
Example output:
Somewhat monstrous code:
*edited a couple times to remove some redundancies and make it more cooler
1
u/____OOOO____ Dec 30 '15
Python solution. Solves all 6 examples in ~6.5 seconds.
1
u/linkazoid Dec 30 '15
Python
1
u/netbpa Dec 30 '15
Perl 6 solution allowing input via stdin or file
1
u/TheSageMage Jan 01 '16
My solution in Java: https://gist.github.com/NicholasAStuart/8b0e7ba89d0436a4fe7a
All-in-all it's as most others have said, once you recognise this as Delannoy's sequence, it becomes trivial compute this.
1
u/[deleted] Jan 01 '16
Dynamic programming with Python 2.
1
u/fibonacci__ 1 0 Jan 01 '16
Python
1
u/JustABlankAccount Jan 03 '16
Little late, here's my solution in C#
1
u/Gobbedyret 1 0 Jan 03 '16
Solution in Python 3.
It's almost instant, but gets an overflow error if there are more than about 10300 paths between any two X'es.
The code takes advantage of the following observations:
Code below:
1
u/easydoits Jan 03 '16
Here a C++ solution with a raw int array as a look up table. Instead of using a function to find Delanoy number, I built a 2d array using the fact that any (x, y) value in the table is the sum of (x - 1,y) + (x - 1,y - 1), (x ,y - 1). I realize I could optimize it a bit by looking for the largest block of max(x,y) paths and only build the look up table using that.
1
u/iandioch Jan 06 '16
My (late) submission in Python:
1
u/adrian17 1 4 Dec 30 '15
Python.