# Simulate the ripple effect

Imagine throwing a rock into a pond. You get to see perfect circles of ripples spreading out over the pond, bouncing off of each other. Of course, nothing can be as serene as that in coding, but, we can try!

Given a point on a 2D array (x, y) and the dimensions of a 2D array/pond (w, h), and a "frame" count, f, return a array of 2D arrays length f that shows a ripple animation starting at (x, y). An ON value (choose between int, bool, or any val for that matter that has two values) should spread out one point in a diamond shape, as the examples show.

Example 1

Input: (0, 0, 3, 3, 7)
Output = a[]
a[0] =
1 0 0
0 0 0
0 0 0

a[1] =
0 1 0
1 0 0
0 0 0

a[2] =
0 0 1
0 1 0
1 0 0

a[3] =
0 0 0
0 0 1
0 1 0

a[4] =
0 0 0
0 0 0
0 0 1

a[5]+ =
0 0 0
0 0 0
0 0 0



Example 2

Input: (2, 2, 5, 5, 5)
Output: b
b[0] =
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0

b[1] =
0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 0 1 0 0
0 0 0 0 0

b[2] =
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1
0 1 0 1 0
0 0 1 0 0

b[3] =
0 1 0 1 0
1 0 0 0 1
0 0 0 0 0
1 0 0 0 1
0 1 0 1 0

b[4] =
1 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 0 0 0 1

b[5]+ =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0


Requirements

• Output should be an array of an array of an array, or a format that well represents the desired output shown above (comments appreciated on this part)
• Must be a method
• Must not use libraries that do everything for you
• values in the arrays of output must only use 2 unique values
• this means you can't just return the plain manhattan distance, you must make it a ripple
• Standard loopholes apply

Information

• Parameters are (int x, int y, int w, int h, int f)
• $$\0 \le x < w \le 10\$$
• $$\0 \le y < h \le 10\$$
• $$\0 < f \le 10\$$
• w and h are equal
• if your language does not support functions, just print to stdout

Test cases:

• The two examples
• (3, 2, 5, 5, 3)
• (0, 0, 1, 1, 1)
• (1, 2, 3, 3, 4)
• (4, 4, 10, 10, 10)

Please try to make it testable online, would be appreciated
This is code golf, so shortest code for each language wins!

• If you add bouncing waves off the sides and wave interference, this could be a very interesting question 👀 (not saying you should do those things) Apr 4 at 1:31
• I was wondering if I should do that, except I'm not exactly sure how that even works! Apr 4 at 2:29
• Return an array of 2D arrays and must be a method assume language features. Why those restrictions? Apr 4 at 9:25
• If w and h are equal, why take both as input? Also, does the output have to have the Index 1 etc. lines? Finally, you should provide sample outputs for the test cases, rather than just giving a bunch of inputs Apr 4 at 11:35
• @Chspsa easiest way to fix it is to remove "must be a method" (as we have standard rules for what submissions must be), and changing the first bullet point to something along the lines of "array of matrices, or the closest equivalent in your language" Apr 4 at 14:57

# J, 30 27 26 bytes

1 :'i.@u=/1#.(|@-"1]#:i.)'


Try it online!

Using (2 2) 5 f (5 5) as the example:

• ]#:i. Creates the w,h coordinate system (showing it boxed here for clarity):

┌───┬───┬───┬───┬───┐
│0 0│0 1│0 2│0 3│0 4│
├───┼───┼───┼───┼───┤
│1 0│1 1│1 2│1 3│1 4│
├───┼───┼───┼───┼───┤
│2 0│2 1│2 2│2 3│2 4│
├───┼───┼───┼───┼───┤
│3 0│3 1│3 2│3 3│3 4│
├───┼───┼───┼───┼───┤
│4 0│4 1│4 2│4 3│4 4│
└───┴───┴───┴───┴───┘

• |@-"1 subtract from x,y and take absolute value:

┌───┬───┬───┬───┬───┐
│2 2│2 1│2 0│2 1│2 2│
├───┼───┼───┼───┼───┤
│1 2│1 1│1 0│1 1│1 2│
├───┼───┼───┼───┼───┤
│0 2│0 1│0 0│0 1│0 2│
├───┼───┼───┼───┼───┤
│1 2│1 1│1 0│1 1│1 2│
├───┼───┼───┼───┼───┤
│2 2│2 1│2 0│2 1│2 2│
└───┴───┴───┴───┴───┘

• 1#. Sum each cell for the Manhattan distance:

4 3 2 3 4
3 2 1 2 3
2 1 0 1 2
3 2 1 2 3
4 3 2 3 4

• i.@u=/ Equality "table" (3d table) with 0, 1, 2, 3, 4:

0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0

0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 0 1 0 0
0 0 0 0 0

0 0 1 0 0
0 1 0 1 0
1 0 0 0 1
0 1 0 1 0
0 0 1 0 0

0 1 0 1 0
1 0 0 0 1
0 0 0 0 0
1 0 0 0 1
0 1 0 1 0

1 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 0 0 0 1

• Wow! That is really cool! Thank you for sharing this program. Apr 4 at 2:28
• Can your solution leave a flat 'pond' as in the OP's examples or does it always leave the last set of 1s? Apr 4 at 20:03
• It can leave a flat pond. Just adjust the iteration number (directly left of f in the examples) high enough. Apr 4 at 20:05

# Julia 1.0, 55 52 bytes

-3 bytes thanks to @MarcMush

x and y are 1-indexed. Outputs an Array{BitArray{2},1}.

x*y*w*h*f=0:~-f.|>d->@.abs((1:w)-x)+abs((1:h)-y)'==d


Try it online!

It simply generates a 2D matrix consisting of the manhattan distances from the point (x,y).

• this is really cool. thanks for posting this solution! Apr 4 at 14:05
• -3 bytes by overloading *: x*y*w*h*f=... Try it online! Apr 7 at 8:19
• @MarcMush Nice, I'll remember that one! Apr 9 at 3:04

# Wolfram Language (Mathematica), 53 bytes

Table[Abs[#1-i]+Abs[#2-j]==k,{k,0,#5},{i,#3},{j,#4}]&


Try it online!

Defines a pure function that takes the inputs x, y, w, h, f (in that order). Constructs a h-by-wmatrix using Table, where each matrix element is either True or False depending on whether the Manhattan distance from (x, y) equals k, an iteration variable corresponding to the kth element in the output array. x and y are 1-indexed. In the TIO I have wrapped the output in Boole so that the output has 0 for Falseand 1 for True.

# R, 62 bytes

\(x,y,d,f,[=outer)abs(1:d-x-1)[abs(1:d-y-1),"+"][1:f-1,"=="]

Attempt This Online!

Returns a 3-d logical array--+ is prepended to the function call to make the output more readable.

Computes the manhattan distances of each cell with the starting cell, then computes the equality with each frame number.

• Great! I was 1 minute slower but 12 bytes worse! Well done! Apr 4 at 15:24
• @DominicvanEssen I guess you'll have to find a nice Husk solution now! Apr 4 at 15:26
• Does the Q insist on zero-based coordinates & indexing (I didn't find it)? If not, it seems wasteful (to me...) to use-up 6 bytes just to convert from native R 1-based numbering... Apr 4 at 15:32
• @DominicvanEssen it does quite plainly state the bounds for the inputs, so I went with those. Apr 4 at 20:31
• Hm, is there anything here that doesn't work within those bounds, though... (probably yes, but I don't immediately see it...)? Apr 4 at 21:04

# PARI/GP, 63 bytes

A port of dingledooper's Julia answer. x and y are 1-indexed. Outputs a vector of matrices.

(x,y,w,h,f)->[matrix(w,h,i,j,abs(i-x)+abs(j-y)==d)|d<-[0..f-1]]

Attempt This Online!

# Factor + math.matrices math.unicode pair-rocket, 80 bytes

[ <iota> [ '[ 2array _ => _ v- vabs Σ _ = ] <matrix-by-indices> ] 4 nwith map ]


Takes input as h w y x f and gives output as a sequence of matrices.

I'm using a modern build of Factor for this, so have a picture:

There are two reasons for that:

• make-matrix-with-indices is now called <matrix-by-indices>.
• Values can be fried directly into literals now, so I can say _ => _ instead of _ _ 2array.

See here for a version that runs on TIO at a cost of 7 bytes.

• Can your solution leave a flat 'pond' as in the OP's examples or does it always leave the last set of 't's? Apr 4 at 20:04
• @gotube It works the same way as OP's examples. Try it online! Apr 4 at 21:06

# 05AB1E, 12 (or 17) bytes

F²Lã€αO²ôNQ,


Three inputs in the order: $$\f\$$, $$\d\$$, $$\[x,y]\$$, where $$\d\$$ is both $$\w\$$ and $$\h\$$ since it's guaranteed that $$\w==h\$$ anyway, and $$\[x,y]\$$ are 1-based indices. Outputs each matrix on a separated line to STDOUT.

Try it online.

For a more strict I/O as described in the challenge description, it would be 17 bytes instead:

L<²Lâε³>αO}²нôδQ


Try it online.

Explanation:

F            # Loop N in the range [0, first (implicit) input f):
²L          #  Push a list in the range [1, second input d]
ã         #  Create all coordinate-pairs by taking the cartesian product of
#  itself
€        #  Map over each coordinate-pair:
α       #   Calculate the absolute difference with the (implicit) third
#   input [x,y]
O      #  Sum each inner pair to get the Manhattan distance
²ô    #  Split this list into parts of size d to turn it into a matrix
NQ  #  Check for each value if it equals N
, #  Pop and output this matrix with trailing newline

L            # Push a list in the range [1, first (implicit) input f]
<           # Decrease it by 1 to range [0, f)
²          # Push the second input-pair [w,h]
L         # Transform both to a list in the range [[1,w],[1,h]]
# Pop and push both lists separated to the stack
â       # Create pairs of both lists with the cartesian power
ε      # Map over each pair:
³     #  Push the third input [x,y]
>    #  Increase it from a 0-based to 1-based index
α   #  Take the absolute difference with the current pair
O  #  Sum to get the Manhattan distance
}²н    # After the map: push the first value of the second input: w
ô   # Split the list into parts of that size
δ  # Apply on the two lists double-vectorized:
Q #  Check which values in the matrix are equal to the value of the
#  [0,f) list
# (after which the list of matrices is output implicitly as result)


# Ruby, 81 bytes

->x,y,w,h,f{(0...f).map{|c|(-y...h-y).map{|m|(-x...w-x).map{|n|m.abs+n.abs==c}}}}


Try it online!

# Java 10, 140 134 bytes

(x,y,w,h,f)->{var r=new int[f][h][w];for(;f-->0;)for(int d=h*w;d-->0;)r[f][d%h][d/h]=Math.abs(d%h-x)+Math.abs(d/h-y)==f?1:0;return r;}


Could technically be 132 bytes since w==h, but this also works for non-square matrices.

Try it online.

Explanation:

(x,y,w,h,f)->{           // Method with the 5 parameters & int[][][] return-type
var r=new int[f][h][w];//  Result array of f amount of h by w sized
//  matrices, filled with 0s by default
for(;f-->0;)           //  Loop f in the range (f,0]:
for(int d=h*w;d-->0;)//   Inner loop over all cells of a matrix:
r[f]               //    Modify the matrix at index f
[d%h][d/h]=    //    at the current cell, to:
Math.abs(d%h-x)  //     Calculate the absolute difference of the current
//     cell's x-coordinate and x
+Math.abs(d/h-y) //     Add the same for y
==f?             //     If this Manhattan distance is equal to f:
1            //      Fill the current cell with a 1
:0;           //     Else: keep the 0
return r;}             //  Return the result

• man, java is my favorite language, it is super cool how you golfed this language! Thank you for posting this solution Apr 4 at 14:07
• java? close to competitive with python? its more likely than you think! Apr 9 at 3:48

# Python 3.8 (pre-release), 90 bytes

lambda a,b,c,d,e,R=range:[[[i==abs(k-a)+abs(j-b)for k in R(c)]for j in R(d)]for i in R(e)]


Try it online!

Returns a 2d list of booleans indicating whether or not the Manhattan distance from the original point is equal to the iteration number, for each iteration number from 0 to e (frame count).

# Charcoal, 25 bytes

ＮθＮηＮζＮεＥＮＥε⭆ζ⁼ι⁺↔⁻ηλ↔⁻θν


Try it online! Link is to verbose version of code. Explanation:

Ｎθ                          First input as a number
Ｎη                        Second input as a number
Ｎζ                      Third input as a number
Ｎε                    Fourth input as a number
Ｎ                  Fifth input as a number
Ｅ                   Map over implicit range
ε                Fourth input
Ｅ                 Map over implicit range
ζ              Third input
⭆               Map over implicit range and join
ι            Current frame
⁼             Equal to
η        Second input
↔⁻         Absolute difference with
λ       Current row
⁺           Plus
θ    First input
↔⁻     Absolute difference with
ν   Current column


# Jelly, 10 bytes

Uses Jonah's method of precalculating the Manhattan distance matrix and then filtering for equality with the step number.

;ạ⁵SƲþ/‘=Ɱ


A full-program that accepts [w, h] f [x, y] and prints a list of frames which are each lists of lists of integers 1 (ripple) and 0 (calm). The coordinates, w h x y are all 1-indexed (top-left = [1,1]).

Try it online! (Footer formats the result for ease on the eyes.)

### How?

;ạ⁵SƲþ/‘=Ɱ - Main Link: [w, h]; f
/    - reduce [w, h] by:
þ     -   table between i in [1..w] and j in [1..h] of: