28
3
I have a serious problem. I have some text files where I keep my very important numbers -- all of the important ones! And twos, and threes..
These numbers were so important that I couldn't entrust them to those newfangled decimal or binary number systems. I kept each number encoded in unary, as so:
+--+
| |
+---+ +----+ |
| | | |
+---+ +-------+
~/two.txt
Simple and reliable: two ASCII loops for the number 2. Unfortunately, these things tend to get tangled up over time and now I have a hard time figuring out how many loops are in each file. Here are some examples that I worked out by hand:
One:
+---+
| |
+--+ |
| |
+--+ |
| |
| |
| |
+--+ +--+
| |
+---------+
Three:
+---------+
| +-----+ |
| | +-+ | |
| | | | | |
| | +-+ | |
| +-----+ |
+---------+
Four:
+--------------+
| +--+ +--+ |
| | | | | |
+-|-----|-----|----+
| | | | | | | |
| +--+ +--+ +--+ |
+------------------+
+------------+
| |
+-----+ +-----+ |
| | | |
+-----|-----------+ | |
| | +--+ | | | |
+-+ +--|--|--+ +---------+
| | +-+ | | |
+------+ | | | |
+-------+ | |
|| | |
|+-----+
| |
+---+
Five:
+--------+ +--------+ +--------+
| | | | | |
| +--|-----+ +--|-----+ |
| | | | | | | | | |
+-----|--+ +-----|--+ +--------+
| | | |
+--------+ +--------+
Can you help me count my loops?
Here are the rules:
- Since I store everything in ASCII-encoded unary, space efficiency is very important to me. Therefore, this is code golf. The smallest program in bytes wins.
- Loops are drawn with the characters +, -, |. Every corner in the loop is drawn unambiguously: exactly one of the characters above and below the + will be |, and exactly one to the right or left will be -. Two + marks are never adjacent.
- Strands may pass over and under each other. When strands cross, you'll be able to see the "under" strand immediately on both sides of the "over" strand.
- Your program should take a string representation of the loop (either from stdin or as a function parameter) and produce a number (either to stdout or as a return value).
- Line lengths may not be uniform in the loop drawing and there may be trailing spaces on each line.
- You may assume that there is at least one loop in the input.
I'm counting on you!
Can one of the sides of an 'under strand' be a
+
? – feersum – 2015-03-24T01:55:57.530@feersum: No, the two edges attached to the + will always be visible. – Matt Noonan – 2015-03-24T01:58:06.410
1@Martin: I'm afraid not. My storage space is really at a premium, so I can't spare all those extra spaces. – Matt Noonan – 2015-03-24T12:33:55.030
I think you should add this (pastebin) as a test case unless I'm missing something and it's not valid input. There are 6 loops; I only tested it online with the SnakeEx solution and it outputs 12.
– blutorange – 2015-03-31T07:43:10.693Each corner has got a | and -. Or does that mean, out of the 4 adjacent characters, 2 need to be - and |, and the other 2 spaces? Wouldn't that invalidate the current example for
four
? – blutorange – 2015-03-31T08:03:19.880Colored version of the loops as it might be hard to see: http://imgur.com/sf87vsn
– blutorange – 2015-03-31T08:12:06.3201
Perhaps you should explicitly forbid or allow loops that cross themselves (eg http://pastebin.com/5RLZuULG) Currently, they are detected by the ruby solution, but not by the SnakeEx solution.
– blutorange – 2015-03-31T11:49:43.720@blutorange Yes, self crossings absolutely are allowed. I can't believe I forgot to add an example like that! Your colored example is also valid. – Matt Noonan – 2015-03-31T14:30:05.200
Possible sequel of knot or not? – Matthew Roh – 2017-02-15T06:48:52.433