Calculating waves
Pyth, 46 18 bytes
ucR2sV+hGGt+GeG.*Q
This code expects input in the form iterations, [wave1, wave2, wave3 ...]
, as seen at the first link below.
Demonstration. Test Harness.
The program works by applying the code within the reduce (u
) function to the input list, as many times as the number of iterations.
I will demonstrate the wave propagation function on the list [0, 1, 2, 3, 4, 5]
, which is in G
:
+hGG
prepends G
's first element to G
, forming [0, 0, 1, 2, 3, 4, 5]
.
t+GeG
appends G
's last element to G
and removes its first element, forming [1, 2, 3, 4, 5, 5]
.
sV
first forms pairs from the lists, giving [[0, 1], [0, 2], [1, 3], [2, 4], [3, 5], [4, 5]]
with the final element of the first list truncated away. Then, the pairs are summed via the s
function, giving [1, 2, 4, 6, 8, 9]
.
cR2
uses floating point division to divide all of the numbers by 2, giving the desired result, [0.5, 1.0, 2.0, 3.0, 4.0, 4.5]
.
Snowman 1.0.0, 219 chars
{vg" "aS:10sB;aM0aa,AAg**-:|al|'NdE'0nRal@(%}{->:1*?{0AaG;:?{;bI:dUNiNwR'NdEwRaC;aM(~:?{(()1wR]0wRaC*))#'wRaC|*#|(()#aLNdEdUNdEwR]wR]aCwR*))#aC;:0wRdUaCwR*?{#aC;#bI:*#0aA'|aa|'!*+'(()#1aA*))#|,aa|'*`nA2nD;aM|*0*;bR|tSsP
With linebreaks for "readability":
{vg" "aS:10sB;aM0aa,AAg**-:|al|'NdE'0nRal@(%}{->:1*?{0AaG;:?{;bI:dUNiNwR'NdEwRaC;
aM(~:?{(()1wR]0wRaC*))#'wRaC|*#|(()#aLNdEdUNdEwR]wR]aCwR*))#aC;:0wRdUaCwR*?{#aC;#
bI:*#0aA'|aa|'!*+'(()#1aA*))#|,aa|'*`nA2nD;aM|*0*;bR|tSsP
Ungolfed / unminified version:
{vg" "aS:10sB;aM // input space-separated list of numbers
0aa,AAg // get first element and array of all-but-first elements
** // discard original input and the 0
// execute the following (input[0]) times
-:
|al|'NdE'0nR // get range from (0..input.length-1]
al@(%}{->:1*?{0AaG;:?{;bI // chop off first element if any
:dUNiNwR'NdEwRaC;aM // map n -> [n-1 n+1]
// if the input consisted of a single element, append [0 0]
// otherwise prepend [0 1] and append [len-2 len-1]
(~:?{(()1wR]0wRaC*))#'wRaC|*#|(()#aLNdEdUNdEwR]wR]aCwR*))#aC;
:0wRdUaCwR*?{#aC;#bI
// map indeces to avg(input[i1], input[i2])
:*#0aA'|aa|'!*+'(()#1aA*))#|,aa|'*`nA2nD;aM
// replace old input, reset permavar
|*0*
;bR
|tSsP // output result
Sample I/O format:
llama@llama:...Code/snowman/ppcg53799waves$ snowman waves.snowman
4 32 32 32 16 64 16 32 32 32
[33 27 40 22 44 22 40 27 33]
Pyth - 25 24 bytes
Uses enumerate, reduce to do iterations.
u.ecs@L++hGGeG,khhk2GvzQ
Try it online here.