r/dailyprogrammer • u/Elite6809 1 1 • Nov 13 '14
[2014-11-14] Challenge #188 [Hard] Arrows and Arrows, part 1
(Hard): Arrows and Arrows, part 1
Wednesday's challenge was released later than I wanted it to be (my fault entirely), so I'll make it up to you by posting this one early. I fear some previous hard challenges have appeared unapproachable to some people due to their logical or mathematical complexity. I aim to make a Hard challenge today which is innately simple, but will still require a Hard degree of thought (assuming you come up with the algorithm yourself.)
Take this grid of characters:
v<^><>>v><>^<>vvv^^>
>^<>^<<v<>>^v^v><^<<
v^^>>>>>><v^^<^vvv>v
^^><v<^^<^<^^>>>v>v>
^<>vv^><>^<^^<<^^><v
^vv<<<><>>>>^<>^^^v^
^<^^<^>v<v^<>vv<^v<>
v<>^vv<^>vv>v><v^>^^
>v<v><^><<v>^^>>^<>^
^v<>^<>^>^^^vv^v>>^<
v>v^^<>><<<^^><^vvv^Let's imagine they all represent arrows, pointing to a cell next to them. For example, v points downward, and < points left. Let's also imagine the grid is infinite - ie. a > arrow at the right-hand side will 'wrap around' and point to the leftmost character on the same row, meaning the board has no limits. Now, we're going to follow the direction of the arrows. Look at the top-left cell. It's a v, so it points down to the cell below it, which is a >. That points to the cell to its right, which is a ^. This points up to the cell above it, which is a <. This points to the cell to its left... which is exactly where we started. See how this has formed a 'loop'? You could go round and round and round forever. Remember, the board wraps around, so this grid is also a loop:
>>>>>>>>>>>>And so is this, if you follow the arrows:
^^>
>^^
^>^This looping structure is called a cycle. The discrete mathematicians in this sub should have all collectively just said 'aha!', as they should know already be thinking of how to approach the challenge from that last sentence. If you're not a discrete mathematician, read on. Your challenge today is simply described: given a grid such as the one above, find the largest cycle in it.
One important point: the 'length' of the cycle is just the part of the cycle that repeats. For example, the cycle is not made longer by adding an 'intro' to it:
>>v
^<<
^
^
^
^The length of this cycle is 6 regardless of where you start from, as that is the length of the 'cycle'.
Formal Inputs and Outputs
Input Description
You will input 2 numbers first - these are the width and height of the grid you'll be working with. Then you will input a grid in the same format as described above.
Output Description
You are to output the length of the longest cycle on the grid, possibly along with some representation of where that cycle is on the board (eg. print the cycle in another color.)
Sample Inputs and Outputs
Sample Input
This input should test the ability of your program to find longer cycles over shorter cycles, and ignore arrows not in a cycle.
5 5
>>>>v
^v<<v
^vv^v
^>>v<
^<<<^Sample Output
Longest cycle: 16
Position:
>>>>v
^ v
^ v
^ v<
^<<< Sample Input
This should test the ability of your program to find cycles that wrap around.
45 20
^^v>>v^>>v<<<v>v<>>>>>>>>^vvv^^vvvv<v^^><^^v>
>><<>vv<><<<^><^<^v^^<vv>>^v<v^vv^^v<><^>><v<
vv<^v<v<v<vvv>v<v<vv<^<v<<<<<<<<^<><>^><^v>>>
<v<v^^<v<>v<>v<v<^v^>^<^<<v>^v><^v^>>^^^<><^v
^>>>^v^v^<>>vvv>v^^<^<<<><>v>>^v<^^<>v>>v<v>^
^^^<<^<^>>^v>>>>><>>^v<^^^<^^v^v<^<v^><<^<<<>
v<>v^vv^v<><^>v^vv>^^v^<>v^^^>^>vv<^<<v^<<>^v
<<<<<^<vv<^><>^^>>>^^^^<^<^v^><^v^v>^vvv>^v^^
<<v^<v<<^^v<>v>v^<<<<<>^^v<v^>>>v^><v^v<v^^^<
^^>>^<vv<vv<>v^<^<^^><><^vvvv<<v<^<<^>^>vv^<v
^^v^>>^>^<vv^^<>>^^v>v>>v>>v^vv<vv^>><>>v<<>>
^v<^v<v>^^<>>^>^>^^v>v<<<<<>><><^v<^^v><v>^<<
v>v<><^v<<^^<^>v>^><^><v^><v^^^>><^^<^vv^^^>^
v><>^><vv^v^^>><>^<^v<^><v>^v^<^<>>^<^vv<v>^v
><^<v>>v>^<<^>^<^^>v^^v<>>v><<>v<<^><<>^>^v<v
>vv>^>^v><^^<v^>^>v<^v><>vv>v<^><<<<v^<^vv<>v
<><<^^>>^<>vv><^^<vv<<^v^v^<^^^^vv<<>^<vvv^vv
>v<<v^><v<^^><^v^<<<>^<<vvvv^^^v<<v>vv>^>>^<>
^^^^<^<>^^vvv>v^<<>><^<<v>^<<v>>><>>><<^^>vv>
<^<^<>vvv^v><<<vvv<>>>>^<<<^vvv>^<<<^vv>v^><^Sample Output
Longest cycle: 44
Position:
>>>>>^
^<
^
>^
^
>^
^
>>>^
^
^<
^
^
^
>^
^
^
^ v<<
^<<< ^
^<<
^<<Notes
If you're a discrete mathematician or know of graph theory, you could try treating the grid as a directed graph and use a cycle finding algorithm on it. If not, try and come up with your own algorithm. I wrote a tool for you to generate random inputs. If you find (or make) a cool loop in an input, post it here!
Bonus
Notice how the path length will always be an even number if the arrows do not wrap around? Try to explain why. Food for thought.
7
u/Elite6809 1 1 Nov 13 '14
My solution, in C#: https://gist.github.com/Quackmatic/066d5ce1b82b1dada258 Half of the code is just input logic and the
Pointstructure.Outputs the solution like this: http://i.imgur.com/e4mWU8s.png
5
u/dohaqatar7 1 1 Nov 13 '14
Could you clarify weather a cycle has to return to it's initial location?
To clarify, is this
,starting at (2,6) , considered a longer cylce than the loop formed by starting at (0,0)?
4
u/Mangoov Nov 14 '14 edited Nov 14 '14
Hi guys, I've been a lurker for a while, I've done a few problems but here's my first submission! I'd love some criticism however harsh it may be! Solution in Python 3:
17
u/[deleted] Nov 13 '14 edited Nov 15 '14
This code is bad (and it's python):
shorter (fewer lines):
prints shortest:
5
u/Godd2 Nov 14 '14
The answer to your bonus is:
5
u/KillerCodeMonky Nov 14 '14 edited Nov 14 '14
JavaScript solution: http://codechaos.me/188H.html
This one function is the real worker. Everything else is pretty much just I/O:
1
u/pshatmsft 0 1 Nov 17 '14
PowerShell
Answer to bonus
Output of code
http://i.imgur.com/WJYrwTs.png
Code
1
u/[deleted] Nov 18 '14 edited Nov 19 '14
Python 3.4:
Output
1
u/deepcube Mar 12 '15 edited Mar 12 '15
I'm late to the game, but I'm looking through the top of all time and choosing fun problems. I used C because pointers make this easy! C99 compliant, no warnings or errors compiled with gcc -std=c99 -pedantic -Wall -Wextra. We read in the width and height then field on stdin exactly as the examples. The field is populated with Node structures, each of which points to the next Node based on the direction of the arrow on input. To find cycles we iterate through the Nodes and follow the pointers. Each Node has a number representing its position in a traversal, these are all 0 to start. If we hit a node with a nonzero position (or start on one ) we have either completed a cycle or come upon a previous traversal. To check which one, each Node has a field containing a pointer to the first Node in a traversal. If the first node for this traversal matches the first stored in that Node (the one we hit with a nonzero position) then we completed a cycle. The length of the cycle is the total length of the chain minus the position of the Node we just found in that chain.
EDIT: small update to use malloc instead of a VLA so it can handle bigger inputs. e.g:
code
1
u/adrian17 1 4 Mar 14 '15 edited Mar 14 '15
J solution! It solves both this challenge and proposed sequel to this challenge (which will probably not be used as it's too similar to Part 1). I'd love some comments so paging /u/Godspiral :D
Result for sample input:
3
u/adrian17 1 4 Nov 13 '14 edited Nov 13 '14
C++(11). Could be easily optimized by skipping all the locations I've visited in the past, but for these inputs it's fast enough as it is so I didn't bother.
(Output is similar to the the sample output so I won't copy it)
3
u/dohaqatar7 1 1 Nov 14 '14 edited Nov 14 '14
It's getting late, so I won't have time to polish this tonight, but I've managed to finish it before to late. I developed this solution on my own with no prior experience with this challenge. Take this into account when reading this, but feel free to give feedback on my approach.
edit: I cleaned it up a little and added some comments to the code, so hopefully it's a bit easier to understand.
I'd also like to mention that you are correct in saying that some previous hard challenges have seemed mathematically unapproachable. While hard challenges could be more difficult than this one, I feel that this challenge was able to sufficiently challenging (at least for me) while requiring minimal background knowledge.
Java
3
u/skeeto -9 8 Nov 14 '14 edited Nov 14 '14
C. I initially set up some data structures -- path, tile, and grid -- and associated functions to assist in computing the solution.
The algorithm is really simple. Keep a list of all the tiles visited and mark each tile with the count on which it was visited. If a tile is visited again, remove its count number of items from the beginning of tile listing. Do this for each possible starting position on the grid and keep track of the longest path. I think the time complexity is something like O(n*m), n being the number of tiles and m being the path length.
It outputs a colored path (ANSI escapes): http://i.imgur.com/QlWWV3T.png
3
u/altoz Nov 14 '14
Written in Go (golang). Comments welcome.
2
u/grim-grime Nov 14 '14 edited Nov 14 '14
Brute force approach with Python 3:
2
u/LuckyShadow Nov 14 '14 edited Nov 14 '14
Python 3
Not short but I like it. :)
https://gist.github.com/DaveAtGit/105cf1436a4d8b0dc3f0#file-arrows_and_arrows-py
The actual code, doing the cycle-finding is pretty short, but I added some bulky classes around, so it is a bit more readable. The output is similar to the one of the examples. So there is nothing special to view :P
Feedback is always welcome. :)
1
u/MuffinsLovesYou 0 1 Nov 14 '14
I wish I wasn't so tired, this seems like a really fun one.
1
u/Godspiral 3 3 Nov 14 '14 edited Nov 14 '14
in J,
NB. get input data from clipboard as boxed cells
NB. makes "linked table" cells pointed by each cell
NB. cycles from starting point until repeat
Not quite complete in finding the longest path, but I did the most interesting stuff.
1
u/dongas420 Nov 14 '14 edited Nov 14 '14
C. Horrible, but it works:
1
u/[deleted] Nov 14 '14 edited Dec 22 '18
deleted 0.5377 What is ^^^this?
1
u/[deleted] Nov 14 '14
Ruby, with the loop output in LOGO: https://gist.github.com/oGabrielMarques/a9d33de85d426d94799b
There are some errors. For instance, I couldn't find a way to determine when there is no loop, so I decided to limit the maximum loop size.
1
u/lukz 2 0 Nov 14 '14
vbscript, simple approach
Output:
1
u/fvandepitte 0 0 Nov 14 '14 edited Nov 14 '14
C#, could have done it a lot cleaner I guess, since I loop a lot over the same data, but it is pretty fast.
Bonus
1
u/[deleted] Nov 14 '14
C++11
It could be made faster by having multiple threaded searches. It already cuts off a search once it reaches a cell it's already scanned.
1
u/Qyaffer Nov 14 '14 edited Nov 15 '14
I really enjoyed this challenge. Though I don't have a fraction of the skill that some(most(if not all)) of you have, I am glad I got it working, even though it is not the smartest or simplest. I could definitely use some tips if anyone has some.
C++11
Output
So sorry for eating up so much page space.
EDIT : I ported my code over to C# as my first C# program ever. Pretty cool experience! https://gist.github.com/anonymous/5526703df68d11c602e3
1
u/CreatureKing Nov 14 '14
Python. Not a particularly clever solution but it gets the result very quickly.
1
u/ReginaldIII Nov 14 '14 edited Nov 15 '14
I'm a bit late to the party but here's my C++ implementation.
1
u/DorffMeister Nov 15 '14
My Groovy solution
1
u/[deleted] Nov 15 '14
In literate Java:
Solution on GitHub
1
u/IDe- Nov 15 '14 edited Nov 15 '14
Python, would appreciate improvement suggestions, especially in terms of shortening without losing readability. Aside from the obvious:
...improvement.
Bonus:
1
u/jnazario 2 0 Nov 15 '14
F#. i got the simple case working - no edge wraps. pretty proud to have gotten this done in under an hour after a long week and with the tv on as a distraction.
my strategy:
1
u/Davipb Nov 15 '14
Here's my C# solution. I chose to read the grid from an input.txt file, treating line breaks as new rows
1
u/MuffinsLovesYou 0 1 Nov 16 '14
Solution in Javascript. Markup is included so you can paste the text to a file and run it in a browser. http://pastebin.com/6VxDJv42
With a pic of the sample output: http://imgur.com/3euZ1YA
1
u/Fs0i Nov 16 '14
My solution, written in C# (tested only with mono under ubuntu)
How do I do it?
Bonus:
1
u/AtlasMeh-ed Nov 16 '14 edited Nov 16 '14
I implemented a solution in python. I used the following insight that I think most everyone else used:
This leads to a O(n) solution which is the fastest you could ever solve this problem in. Great challenge!
I also took a crack at the bonus. It's a verbose, easy to understand explanation.
And the python code:
1
u/goto3 Nov 16 '14 edited Nov 16 '14
Haskell
Trying to learn some Haskell. Probably many newbie mistakes.
One thing...
Code:
1
u/Itrillian Nov 16 '14
Solved using Clojure 1.6.0
It works but it could use some optimisation. I feel like I'm missing an easy way to eliminate trying out routes in the grid.
The program expects a file as input called "input.txt" like:
1
u/reaganveg Nov 17 '14
1
u/TheWorstPossibleName Nov 17 '14
I have never done one of these challenges before, but this looked fun, so I did it in Python. I'm a bit new to coding, so this probably isn't optimized at all, but it works.
1
u/dozybolox13 Nov 18 '14 edited Nov 18 '14
A bit late to the party. Here's a very long and verbose solution. Go easy I'm new to this :)
edit: Location is another class that I didn't paste here. 2nd edit: refactoring
Ruby:
1
u/dvassdvsd Nov 18 '14
1
u/gengisteve Nov 21 '14
My take, which for fun generates and solves a couple thousand times to find the longest path in the bunch
1
u/wilsoniya Nov 23 '14
An implementation in Rust: https://github.com/wilsoniya/dailyprog188-arrows
1
u/TieSoul 0 1 Dec 06 '14 edited Dec 06 '14
Ruby
Works with both examples given and, as far as I can tell, also with some random ones I generated.
it's decently fast: found the longest loop in a 100x100 input in around a minute.
1
u/lucashn Dec 18 '14
Python 3
1
u/agambrahma Dec 18 '14
Golang
However, it returns a different (longer!) answer for the second sample question above, which (IMO) does look legit. Anyone mind confirming?
i.e. basically identical except for the extra lead bit starting from about halfway through the last line.
1
u/Quel Jan 03 '15 edited Jan 03 '15
I am way late to the topic, but saw this on the front page and took a crack at it. Solved it in R, though I haven't taken the last step to pretty up my output to the format in the challenge.
Consists of 4 main functions:
Here's what the main function and output look like for the 5x5 example. It also works for the 45x20 example, I'm just using this to save space. First column is the row, second is the column, and third is the value (1/2/3/4 up/down/left/right).
And here are all of the functions:
1
u/tangentstorm Mar 20 '15
/u/adrian17 mentioned me in a comment here, so I figured I'd try my own J implementation before I looked at the others. Here is the compact version:
The full version, with comments and examples for every word, is here:
https://github.com/tangentstorm/tangentlabs/blob/master/j/tracks.ijs