Bouncey-edged XY box with a marker at Z

10

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.

Jake Harry

Posted 2016-12-02T04:43:23.277

Reputation: 101

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.

– betseg – 2016-12-02T05:09:56.093

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)

– James – 2016-12-02T05:13:25.263

@betseg - oops. Sorry, hopefully I can gain some interest having missed that important step. – Jake Harry – 2016-12-02T05:19:01.680

@DrMcMoylex - Noted, thanks, have removed the bonus now. – Jake Harry – 2016-12-02T05:19:20.153

You should explain how Z represents the location (1-based row-major it seems)

– Luis Mendo – 2016-12-02T08:44:18.857

X accross and Y down @luis – Jake Harry – 2016-12-02T09:00:20.260

2You 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. – Kade – 2016-12-02T12:35:01.640

@kade. Thanks the rules made it sound like the answer you choose doesn't have to be the best one? – Jake Harry – 2016-12-02T22:07:43.037

@James yes I'm an all round stooge. First across, then down. And yes, it should be 10 rows and 10 columns – Jake Harry – 2016-12-04T02:18:33.470

@james I made some changes to the description. Is that clearer? – Jake Harry – 2016-12-04T02:22:50.517

@james That's the spirit! Sorry mate, my fault – Jake Harry – 2016-12-04T02:30:09.087

I am sad that there has been so few golf language attempts at this!! – Jake Harry – 2016-12-15T23:36:35.960

Answers

1

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))

TFeld

Posted 2016-12-02T04:43:23.277

Reputation: 19 246

nice job, think I broke it though with 10,100,300. Doesn't seem to behave at the far right boundary? – Jake Harry – 2016-12-02T12:22:27.793

@JakeHarry Works for me: http://ideone.com/G2fwV1

– TFeld – 2016-12-02T12:26:51.247

Ahh, I'm a stooge, the ide I used wasn't fixed width so it was technically right, just not to the eyes. – Jake Harry – 2016-12-02T12:30:34.757

1

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`

Neil

Posted 2016-12-02T04:43:23.277

Reputation: 95 035

1

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.

James Holderness

Posted 2016-12-02T04:43:23.277

Reputation: 8 298

0

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>

edc65

Posted 2016-12-02T04:43:23.277

Reputation: 31 086