10
\$\begingroup\$

Long time lurker first time poster here.

Write a program which takes 3 inputs: X, Y and Z.

  • X = across (columns)
  • Y = down (rows)
  • Z = Location Marker

The program should then print a visual grid X across and Y down. This grid will can be made of any character except "+". Each 'location' is given an index number, counting up from 1 at coordinate 1, 1 across and then down until the end.

X and Y will always be at least 3, and Z will never be bigger than X * Y.

Z will represent the location which is printed as a "+", on the location as well as 1 character left, right, up and down. For example:

 +
+++
 +

Finally, if the + characters would intercept the edges (top most, left most, right most and/or down most edge), then the + should bounce back along the same axis and overflow the other side.

Examples: Input = 5, 5, 13

-----
--+--
-+++-
--+--
-----

Input = 10, 10, 10

-------+++
---------+
---------+
----------
----------
----------
----------
----------
----------
----------

Input = 10, 10, 21

----------
+---------
+++-------
+---------
----------
----------
----------
----------
----------
----------

Edit : non square example 16,3,32

---------------+
-------------+++
---------------+

I think I've covered everything. There should be no limit to the input, but if your program requires, cap it off at 64 * 64.

Bonus point (can I do that?): Input Z should not be > X * Y, but if it is larger than Y * Z, then output the center + to the middle of the grid. EDIT: Input Z cannot be greater than X * Y

Edit 2:. Made some changes to X and Y to hopefully be clearer

This is code golf, shortest code wins.

\$\endgroup\$
  • \$\begingroup\$ Welcome to Programming Puzzles and Code Golf! This is a nice challenge, but I recommend posting future challenges to the Sandbox where they can get feedback before being posted to the main site. \$\endgroup\$ – betseg Dec 2 '16 at 5:09
  • \$\begingroup\$ What is the "Bonus point" about? Does implementing that exact feature give an advantage to your byte-count? If it does, you should explicitly note how big of a bonus it is. (As a side note, bonuses in code-golf are generally discouraged) \$\endgroup\$ – James Dec 2 '16 at 5:13
  • \$\begingroup\$ @betseg - oops. Sorry, hopefully I can gain some interest having missed that important step. \$\endgroup\$ – Jake Harry Dec 2 '16 at 5:19
  • \$\begingroup\$ @DrMcMoylex - Noted, thanks, have removed the bonus now. \$\endgroup\$ – Jake Harry Dec 2 '16 at 5:19
  • 2
    \$\begingroup\$ You shouldn't be accepting an answer in the first day of posting, I am fairly confident a MATL/Jelly/05AB1E golfer will see this and solve it in much less bytes than Python. I think most people tend to wait at least a week. \$\endgroup\$ – Kade Dec 2 '16 at 12:35
1
\$\begingroup\$

Python 2, 172 171 bytes

def f(x,y,z):A=[['-']*x for _ in' '*y];z-=1;X,Y=z%x,z/x;a=[2,-1];A[Y][X]=A[Y+a[Y>0]][X]=A[Y-a[Y<y-1]][X]=A[Y][X+a[X>0]]=A[Y][X-a[X<x-1]]='+';print'\n'.join(map(''.join,A))

Edit: Saved 1 bytes by converting to function.

Previous (more readable):

x,y,z=inputtt
A=[['-']*x for _ in' '*y]
z-=1
X,Y=z%x,z/x
a=[2,-1]
A[Y][X]=A[Y+a[Y>0]][X]=A[Y-a[Y<y-1]][X]=A[Y][X+a[X>0]]=A[Y][X-a[X<x-1]]='+'
print'\n'.join(map(''.join,A))
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ nice job, think I broke it though with 10,100,300. Doesn't seem to behave at the far right boundary? \$\endgroup\$ – Jake Harry Dec 2 '16 at 12:22
  • \$\begingroup\$ @JakeHarry Works for me: ideone.com/G2fwV1 \$\endgroup\$ – TFeld Dec 2 '16 at 12:26
  • \$\begingroup\$ Ahh, I'm a stooge, the ide I used wasn't fixed width so it was technically right, just not to the eyes. \$\endgroup\$ – Jake Harry Dec 2 '16 at 12:30
1
\$\begingroup\$

JavaScript (ES6), 165 bytes

(x,y,z,a=[...Array(y)].map(_=>Array(x).fill`-`))=>a.map(a=>a.join``,a[b=--z/x|0][c=z%x]=a[b?b-1:2][c]=a[b][c?c-1:2]=a[y+~b?b+1:y-3][c]=a[b][++c<x?c:x-3]=`+`).join`\n`
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Befunge, 175 bytes

>&:10p60p&:00p&1-:10g%:20p\10g/:30p::1+00g-!-\!+2-50p::1+1v
vg02g01*`\4\`0:-g05\!-g03:g00p01-1<g06+p00-1<p04-2+!\-!-g0<
>-!*\10g40g-:0`\4\`**+!2*"+"+10g:#^_$5500g:#^_$$$>:#,_@

Try it online!

The first line (and a short continuation onto the second line) is where the parameters are read in and a few constants are calculated - the coordinates of the location (lx, ly), as well as adjusted coordinates which account for the bouncing off the edges:

ax = lx - (lx+1==w) + (lx==0) - 2 
ay = ly - (ly+1==h) + (ly==0) - 2

The second and third lines contain the main loops over the height and width of the grid, the path of execution going from right to left initially before turning around onto the third line going left to right. For each coordinate in the grid (gx, gy) we calculate the following condition:

(gx==lx && gy>ay && gy<ay+4) || (gy==ly && gx>ax && gx<ax+4)

If that condition is true, we push a "+" onto the stack, if false we push a "-". To avoid branching here, we're really just pushing 43 + 2 * !condition (43 being the ASCII value of plus and 45 being minus).

Once the loops have finished, the final bit of code is just a standard output routine that prints out everything on the stack.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 170

Still golfable

(w,h,z,t=--z%w,u=z/w|0,r='-'.repeat(w),S=(f,j)=>(r+f+r).substr(w-j,w))=>[...Array(h)].map((q=u-!!u-!(u+1-h),y)=>y-u?y>=q&y<q+3?S('+',t):r:S('+++',t-!!t-!(t+1-w))).join`
`

Less golfed

(w, h, z
, t=--z%w
, u=z/w|0
, r='-'.repeat(w)
, S=(f,j)=>(r+f+r).substr(w-j,w)
) => [...Array(h)].map(
    (q = u-!!u-!(u+1-h), 
     y) => y-u?y>=q&y<q+3?S('+',t):r:S('+++',t-!!t-!(t+1-w))
).join`\n`

Test

F=
(w,h,z,t=--z%w,u=z/w|0,r='-'.repeat(w),S=(f,j)=>(r+f+r).substr(w-j,w))=>[...Array(h)].map((q=u-!!u-!(u+1-h),y)=>y-u?y>=q&y<q+3?S('+',t):r:S('+++',t-!!t-!(t+1-w))).join`
`

function update() {
  var [x,y,z] = I.value.match(/\d+/g)
  O.textContent = F(+x,+y,+z)
}

update()
<input value='5 6 10' oninput='update()' id=I>
<pre id=O>

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.