r/dailyprogrammer • u/Elite6809 1 1 • Dec 05 '14
[2014-12-5] Challenge #191 [Hard] Tricky Stick Stacking
(Hard): Tricky Stick Stacking
Similar to the previous hard challenge with the arrows, this challenge will similarly require a hard degree of thought to solve (providing, of course, you develop the algorithm yourself,) while being relatively easy to understand.
Imagine you have a 2D plane, into which you can place sticks, like so. All of the sticks are perfectly straight, and placed into this plane from the top (positive Y) down. The sticks will never overlap or cross over one another. Your task today is to simply determine in which order the sticks must be pulled out of the plane without hitting any other sticks.
There are a few rules for this:
- A stick is pulled out of the plane directly upward (ie. along the positive Y axis), like so. This means that you can't pull a stick out sideward or at an angle (for example, to avoid another stick.)
- A stick cannot be pulled out if there's another stick directly above it.
In some possible possible scenarios, there is only one possible order to pull the sticks out of the plane. This scenario only has one possible order: 1, 2, 4, 3. This scenario however has two possible orders, as the last two remaining sticks are not interfering with one another's removal, so you can remove them in any order.
Formal Inputs and Outputs
Input Description
Each stick is described by a number and the co-ordinates of its 2 ends, like so:
n:x1,y1,x2,y2Where the stick number n is between the points (x1, y1) and (x2, y2). You will first input a number S which is the number of sticks in the scenario. You will then take a further S lines of input in the above format. n must be an integer but the co-ordinates can be any real number.
Output Description
You are to output one possible order of removal of the sticks (where each stick is identified by its number n. There may be more than one.
Sample Inputs and Outputs
Sample Input
4
1:0,3,4,5
2:2,3,8,1
3:4,0,5,1
4:1,3,4.2,1Sample Output
1, 2, 4, 3Sample Input
5
1:3,3,8,1
2:11,2,15,2
3:6,3,12,4
4:10,5,10,10
5:9,11,18,12Sample Output
This scenario has 2 possible outputs:
5, 4, 3, 1, 2or:
5, 4, 3, 2, 1Sample Input
6
1:1,6,12,6
2:1,7,1,15
3:11,1,13,10
4:14,10,15,6
5:15,2,15,5
6:12,1,14,11Sample Output
2, 1, 3, 6, 4, 5Sample Input
5
1:2,2,2,8
2:1,1,11,2
3:10,1,15,3
4:5,5,13,8
5:6,4,9,3Sample Output
(all 3 are valid)
1, 4, 5, 2, 3
4, 1, 5, 2, 3
4, 5, 1, 2, 3Sample Input
6
1:6,2,14,7
2:12,10,15,9
3:12,3,12,6
4:3,1,17,2
5:4,3,11,2
6:3,10,12,12Sample Output
(both are valid)
6, 2, 1, 3, 5, 4
6, 2, 1, 5, 3, 4Sample Input
5
1:2,1,15,15
2:15,5,15,12
3:10,8,13,2
4:13,4,15,4
5:8,9,12,13Sample Output
5, 1, 2, 4, 3
2
u/Elite6809 1 1 Dec 05 '14
My solution in F#. I'm trying to wean myself into FP slowly.
F# is the weird child of OCaml and C# so it looks kind of ugly when you have the telltales signs of .NET mixed in with functional code, but it does the job. Hopefully I'll try Haskell next.
2
u/galaktos Dec 05 '14
I think you swapped the numbers in the two possibilities image… AFAICT that should be 54321 / 54312.
That aside, this sounds a bit like a rendering problem, and I wonder if you could solve it in OpenGL (immediate mode), using the graphic card’s algorithms for rasterizing primitives, reading the solution from the depth buffer.
9
u/lucaswerkmeister Dec 05 '14
Prolog, because I need to train that anyways for uni. Here’s the one rule:
And here’s how you set it up for sample input 1:
And then this query gets you all possible configurations:
The output is in
N1,N2,N3,N4.I regret nothing.
4
u/dooglehead Dec 06 '14
C
4
u/TieSoul 0 1 Dec 06 '14 edited Dec 06 '14
Ruby + Gosu
First it shows an animation of the sticks getting pulled out (with fancy numbered labels :3), then prints the order in console. If multiple sticks can be removed at once, it will show them being removed simultaneously in the animation and display only one of the possible orders in the console.
(textual) output:
sample input 1:
sample input 2:
sample input 3:
1
u/adrian17 1 4 Dec 06 '14
Could you please provide additional tests for these cases? http://puu.sh/dj7fP/7b47ee73a0.png
It's a tricky case as some solutions always take into account only ends of one of the the sticks, which may give wrong results. Currently two solutions were tricked by this: link, link. I gave example inputs in the comments.
2
u/adrian17 1 4 Dec 05 '14 edited Dec 05 '14
Python 3.
2
u/aertho Dec 06 '14 edited Dec 06 '14
Python: overrides the logical operators for a stick object class
2
u/-Robbie Dec 06 '14 edited Dec 06 '14
Haskell
2
1:0, 0, 5, 5
2:1, 3, 2, 4
I also initially failed this example while passing the examples in the problem description.
2
u/[deleted] Dec 11 '14
My code failed this test because stick 1 intersects with both stick 3 and stick 5:
This test case should be removed.
1
u/TheDerkus Dec 05 '14
I have a solution in C++, works with the test cases provided:
1
u/jnazario 2 0 Dec 05 '14
i think this works. F#.
1
u/[deleted] Dec 06 '14 edited Dec 22 '18
deleted 0.4009 What is ^^^this?
1
u/jetRink Dec 06 '14 edited Dec 06 '14
Clojure
Works for all test inputs. It does assume that "the sticks will never overlap or cross over" means that the end of one stick will never lie on another line. (I hope I interpreted that correctly.)
1
u/Acidom Dec 06 '14
PYTHON, Semi-hackish but it passes all the test cases.
1
u/toodim Dec 06 '14
Python 3. A bit of a hack because I added a minuscule tilt to vertical line segments to give them slopes.
1
u/[deleted] Dec 12 '14
In Clojure:
1
u/curtmack Dec 15 '14 edited Dec 15 '14
There seems to be an error in this test case:
Sticks 1 and 3 intersect, which you said wouldn't happen.
Anyway, it's late, but here's my answer in Clojure. It validates all of the test cases (except the flawed one mentioned above), including the trick situations /u/adrian17 found. It also correctly handles the case where the points of a stick are specified in right-to-left order, which I noticed a few other solutions will trip on.
1
u/zwrty Dec 17 '14 edited Dec 17 '14
C, WARNING: Crappy code
Usage:
1
u/agambrahma Dec 21 '14
golang
Runs as follows: