r/dailyprogrammer • u/jnazario 2 0 • Mar 30 '16
[2016-03-30] Challenge #260 [Intermediate] Diagonal collision
Description
You have one rectangle composed of X*Y squares, with X being the width and Y being the height. You want to know how many squares you are going to collide if you were to draw a diagonal, meaning a line between the bottom-left edge and the top-right edge.
Input Description
2 unsigned integers X and Y
Output Description
Number of squares that collide with the diagonal.
Sample Inputs
Sample Input 1 : 5 2 Sample Input 2 : 3 9
Sample Outputs
For this first case, the squares marked as X would collide with the diagonal :
..XXX
XXX..
meaning the Sample Output 1 is 6
Sample Output 2 : 9
Challenge Input
Input 0 : 3 9 Input 1 : 21 2 Input 2 : 168 189 Input 3 : 100 101 Input 4 : 123456789 987654321
Bonus
For small numbers, you can output on the standard output which squares would collide, like so :
..XXX
XXX..
Credit
This challenge was suggested by /u/Cobrand. Have a good challenge idea? Consider submitting it to /r/dailyprogrammer_ideas.
4
u/bearific Mar 30 '16 edited Mar 30 '16
Edit: I accidentally implemented this as a path walking algorithm that can not move diagonally.
An easy and fast approximation would be:
X + Y + gcd(X, Y) - 2
Python 2.7, Challenge + bonus, a variation of Bresenham's Algorithm
Output:
Gist of 168x189 and 100x101
4
u/BobFloss Mar 30 '16
This is killing me here. There must be a mathematical function to calculate the number of intersections given a rectangle of X*Y squares. Somebody please reply if you find out the solution in math terms; I'll appreciate it.
3
u/u1tralord Mar 31 '16
Professor told me I should learn Go this morning. Any input is welcome. The only resource I've used to learn so far is LearnXInYMinutes, so my conventions may be really out of wack. This is my first piece of code so far:
Code:
Output:
2
u/netbpa Mar 30 '16
Perl6 alternate algorithm, no bonus
2
u/popRiotSemi Mar 30 '16
C++, brute force
Few seconds for challenge input. I accidentally implemented the bonus as my first solution but took it out to process the challenge inputs.
2
u/AttackOfTheThumbs Mar 31 '16
If I understand this correctly (arbitrary rectangle filled with 1x1 squares), then this is just a mathematical problem rather than a programming one. Yeah, all programming is maths bla bla.
I feel like it fits better into Project Euler.
1
u/Godspiral 3 3 Mar 30 '16 edited Mar 30 '16
in J, don't know what I'm doing
last bonus one would need to look at nearly 1B cells to decide intersections.
drawing
assuming this is right answer for even columns
1
u/jnd-au 0 1 Mar 30 '16 edited Mar 31 '16
Scala
quick-and-dirty (uses Double instead of Rationals).[Edit: Pfft. Commented out my naive estimator, now just using the GCD version which seems to be accurate after testing.]Output:
1
u/Daanvdk 1 0 Mar 30 '16
Java
1
u/wcastello Mar 30 '16
In Python 3, my first try was:
Second try is exactly what /u/jmsdvl did so I won't post it again.
1
u/pburns1587 Mar 30 '16 edited Mar 30 '16
Started with a for loop across each horizontal box, but thought this may be the right algorithm after some tests. Makes sense to me at least. No bonus yet.
Any comments or suggestions are very welcome, I'm just learning Python (and programming in general, really).
python 3.5
Output:
1
u/flpcb Mar 30 '16
Second ever F# program. It feels like I could make this much better but I'm not good enough to know how. :/
1
u/glenbolake 2 0 Mar 30 '16
Python 3, with bonus
Output:
1
u/jnd-au 0 1 Mar 31 '16 edited Mar 31 '16
Hmm, a few differing GCD-based solutions have been proposed, but it looks like the original is correct:
Edit: more examples.
1
u/automata-door 1 0 Mar 31 '16 edited Mar 31 '16
Common Lisp (with bonus) (without gcd formula)
It finds points on diagonals on grid (slope and increasing x stepwise), and just takes coordinates of cells to left and right.
I/O :
XXX--
--XXX
6
X--
X--
X--
-X-
-X-
-X-
--X
--X
--X
9
XXXXXXXXXXX----------
----------XXXXXXXXXXX
22
(reddit won't let me post the sketch max length: 10000, see http://pastebin.com/s5tmeFhP)
200
I disabled sketching for higher inputs
I guess I'll have to fix that last one. Though I guess it wasn't meant for non-gcd formula solutions
1
u/porphyro Mar 31 '16 edited Mar 31 '16
Cjam, 29 bytes
Try it online!
1
u/FlammableMarshmallow Mar 31 '16
Haskell
Pretty short solution if I say so myself
1
u/fibonacci__ 1 0 Apr 01 '16 edited Apr 01 '16
Python
Output
1
u/IAmTheCreeper Apr 02 '16
Java
Like many other people in the thread, simply using the formula
Code:
1
u/[deleted] Apr 02 '16
Factor, does not do challenge input:
1
u/Gobbedyret 1 0 Apr 06 '16
Python 3.5
I didn't figure out the GCD trick, so my solution relies on simple iteration.
Also, it suffers from floating point rounding errors, which is annoying, but hard to do something about.
1
u/CTMemorial Apr 07 '16
C
Super late submission, but I may as well post it since it took me the better part of a day to do this. I also learned that I need to figure out how to deal with floating point error - I couldn't get the really large one to work, but it went well for everything else.
0
u/[deleted] Mar 30 '16
[deleted] — view deleted comment
0
u/[deleted] Mar 30 '16
[deleted] — view deleted comment