Code Golf: Connecting the dots

Posted by ChristopheD on Stack Overflow See other posts from Stack Overflow or by ChristopheD
Published on 2010-03-26T23:37:24Z Indexed on 2010/03/26 23:43 UTC
Read the original article Hit count: 140

Description:

The input are multiple lines (terminated by a newline) which describe a 'field'. There are 'numbers' scattered across this field:

  • the numbers always start at 1
  • they follow the ordering of the natural numbers: every 'next number' is incremented with 1
  • every number is surrounded by (at least) one whitespace on it's left and right

Task:

Draw lines between these numbers in their natural order (1 -> 2 -> 3 -> ...N) with the following characteristics:

  1. replace a number with a '+' character
  2. for horizontal lines: use '-'
  3. for vertical lines: use '|'
  4. going left and down or right and up: /
  5. going left and up or right and down: \

Important note: When drawing lines of type 4 and 5 you can assume that : (given points to connect with coordinates x1, y1 and x2, y2) distance(x1,x2) == distance(y1,y2).

Have a look at the examples to see where you should 'attach' the lines. It is important to follow the order in which the dots are connected (newer lines can be drawn over older lines).

Sample input 1

                                  9 


                             10   8  7 
                                     6              

                                         5            
                             11                        

           13            12          3   4        

       14 15                                               
          16                                         
          1                          2              

Sample output 1


                                /+                         
                               / |                         
                              /  |                         
                            +/   +--+                      
                            |       +\                     
                            |         \                    
                            |          \+                  
                           /+           |                  
                          /             |                  
         /+-------------+/          +---+                  
        /                           |                      
      +--+                          |                      
         +                          |                      
         +--------------------------+                      

Sample input 2


                                4 



       2                  3     5        6 

   1 8                                       7 

Sample output 2

                              /+               
                             / |               
                            /  |               
                           /   |               
     /+------------------+/    +--------+\     
    /                                     \    
  +/ +--------------------------------------+  


Winner: shortest solution (by code count). Input can be read via command line.

© Stack Overflow or respective owner

Related posts about code-golf

  • Code Golf: Collatz Conjecture

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Inspired by http://xkcd.com/710/ here is a code golf for it. The Challenge Given a positive integer greater than 0, print out the hailstone sequence for that number. The Hailstone Sequence See Wikipedia for more detail.. If the number is even, divide it by two. If the number is odd, triple… >>> More

  • Code Golf - p day

    as seen on Stack Overflow - Search for 'Stack Overflow'
    The Challenge The shortest code by character count to display a representation of a circle of radius R using the *character, followed by an approximation of p. Input is a single number, R. Since most computers seem to have almost 2:1 ratio you should only output lines where y is odd. The approximation… >>> More

  • Code Golf - PI day

    as seen on Stack Overflow - Search for 'Stack Overflow'
    The Challenge The shortest code by character count to display a representation of a circle of radius R using the *character. Followed by an approximation of pi Input is a single number, R Since most computers seem to have almost 2:1 ratio you should only output lines where y is odd. The approximation… >>> More

  • Code Golf: Triforce

    as seen on Stack Overflow - Search for 'Stack Overflow'
    This is inspired by/taken from this thread: http://www.allegro.cc/forums/thread/603383 The Problem Assume the user gives you a numeric input ranging from 1 to 7. Input should be taken from the console, arguments are less desirable. When the input is 1, print the following: *********** *********… >>> More

  • Code Golf: Tic Tac Toe

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Post your shortest code, by character count, to check if a player has won, and if so, which. Assume you have an integer array in a variable b (board), which holds the Tic Tac Toe board, and the moves of the players where: 0 = nothing set 1 = player 1 (X) 2 = player 2 (O) So, given the array b… >>> More

Related posts about rosetta-stone