## APL, <s>53</s> 49/51 a←?7 7⍴9⋄{(⍵⌷a)-⍨←+/,⌷∘a¨+∘⍵¨∘.,⍨2-⍳3}¨∘.,⍨2×⍳3⋄⍕¨a Sample run: a←?7 7⍴9⋄{(⍵⌷a)-⍨←+/,⌷∘a¨+∘⍵¨∘.,⍨2-⍳3}¨∘.,⍨2×⍳3⋄⍕¨a 5 1 8 4 4 3 7 6 42 7 44 7 38 9 6 4 5 5 4 3 1 4 40 3 32 2 23 5 7 8 3 7 3 4 1 5 46 8 41 1 34 6 4 2 9 2 8 9 2 A shorter 49 char program can be obtained by removing the `⍕¨` at the end, but leaves the numbers right-aligned (by default). --- Explanation: - `a←?7 7⍴9` creates a 7 by 7 array filled with random numbers between 1 and 9 inclusive, and assigns it to `a`. - `2×⍳3` takes the array `1 2 3` and multiplies each term by `2`, resulting in `2 4 6`. - `∘.,⍨` generates the product of an array with itself. This results in `(2 2)(2 4)...`, which are the indices of the elements in the array which are to be replaced by the sums. - `{(⍵⌷a)-⍨←+/,⌷∘a¨+∘⍵¨∘.,⍨2-⍳3}¨` iterates over the array, assigning each index pair to `⍵`. For each iteration, it does the following: - `2-⍳3` takes the array `1 2 3` then subtracts each element from 2, resulting in `1 0 ¯1`. - `∘.,⍨` once again generates the product of the array with itself. - `+∘⍵¨` takes each pair in the resulting array and adds both terms to the corresponding ones in `⍵`. This results in the surrounding terms (plus itself) of an index that is going to be replaced by a sum. - `⌷∘a¨` takes each pair from the resulting array and uses them to index `a`, the original array. This generates the surrounding terms of an item, as well as itself. - `+/,` flattens the resulting array and sums the array. This results in the sum of an element in the list and its surrounding terms. - `⍵⌷a` uses `⍵` to index `a`, and returns the item in the array which is to be replaced. - `(⍵⌷a)-⍨←` takes that item, subtracts it from the sum, and replaces the item. The item is then replaced by the appropriate sum. - `⍕¨a` prints the modified list with the numbers left-aligned.