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!
+? \$\endgroup\$