Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

APL, 53 49 43 42 40 39 36

I managed to replicate J's ;. in APL, and used Gareth's approachGareth's approach, saving 13 chars.

{×5⌷⍵:5⌷⍵⋄+/⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1

Sample run:

      {×5⌷⍵:5⌷⍵⋄+/⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1
9  9 6  1 7  5 6
7 55 5 39 9 54 9
9  8 2  1 8  1 9
2 43 8 41 6 42 5
7  3 4  4 8  3 2
2 29 1 26 2 35 8
6  4 2  3 2  3 7

Explanation:

  • ∘.∨⍨9⍴0 1 generates a bit mask.
  • ×∘?∘9¨ multiplies each bit by a random value from 1 to 9 inclusive, generating a masked grid of random numbers.
  • 3,⌿3,/ uses what can only be described as hackery to return all 3 by 3 overlapping boxes in the masked array. These are also flattened in the process.
  • {×5⌷⍵:5⌷⍵⋄+/⍵}¨ iterates over the array, assigning each element to . For each iteration, it takes the fifth (middle, remembering that APL indexing is 1-based), and returns its sign. In this case this is equivalent to testing if the number is greater than 0. If this returns 1 (for true), then return that element. Otherwise, return the sum of the elements in the flattened 3 by 3 box. It uses the :⋄ ternary operator, which is the equivalent of ?: in many languages.

APL, 53 49 43 42 40 39 36

I managed to replicate J's ;. in APL, and used Gareth's approach, saving 13 chars.

{×5⌷⍵:5⌷⍵⋄+/⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1

Sample run:

      {×5⌷⍵:5⌷⍵⋄+/⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1
9  9 6  1 7  5 6
7 55 5 39 9 54 9
9  8 2  1 8  1 9
2 43 8 41 6 42 5
7  3 4  4 8  3 2
2 29 1 26 2 35 8
6  4 2  3 2  3 7

Explanation:

  • ∘.∨⍨9⍴0 1 generates a bit mask.
  • ×∘?∘9¨ multiplies each bit by a random value from 1 to 9 inclusive, generating a masked grid of random numbers.
  • 3,⌿3,/ uses what can only be described as hackery to return all 3 by 3 overlapping boxes in the masked array. These are also flattened in the process.
  • {×5⌷⍵:5⌷⍵⋄+/⍵}¨ iterates over the array, assigning each element to . For each iteration, it takes the fifth (middle, remembering that APL indexing is 1-based), and returns its sign. In this case this is equivalent to testing if the number is greater than 0. If this returns 1 (for true), then return that element. Otherwise, return the sum of the elements in the flattened 3 by 3 box. It uses the :⋄ ternary operator, which is the equivalent of ?: in many languages.

APL, 53 49 43 42 40 39 36

I managed to replicate J's ;. in APL, and used Gareth's approach, saving 13 chars.

{×5⌷⍵:5⌷⍵⋄+/⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1

Sample run:

      {×5⌷⍵:5⌷⍵⋄+/⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1
9  9 6  1 7  5 6
7 55 5 39 9 54 9
9  8 2  1 8  1 9
2 43 8 41 6 42 5
7  3 4  4 8  3 2
2 29 1 26 2 35 8
6  4 2  3 2  3 7

Explanation:

  • ∘.∨⍨9⍴0 1 generates a bit mask.
  • ×∘?∘9¨ multiplies each bit by a random value from 1 to 9 inclusive, generating a masked grid of random numbers.
  • 3,⌿3,/ uses what can only be described as hackery to return all 3 by 3 overlapping boxes in the masked array. These are also flattened in the process.
  • {×5⌷⍵:5⌷⍵⋄+/⍵}¨ iterates over the array, assigning each element to . For each iteration, it takes the fifth (middle, remembering that APL indexing is 1-based), and returns its sign. In this case this is equivalent to testing if the number is greater than 0. If this returns 1 (for true), then return that element. Otherwise, return the sum of the elements in the flattened 3 by 3 box. It uses the :⋄ ternary operator, which is the equivalent of ?: in many languages.
added 76 characters in body
Source Link
Volatility
  • 3.4k
  • 2
  • 20
  • 35

APL, 53 49 43 42 40 39 3936

I managed to replicate J's ;. in APL, and used Gareth's approach, saving 1013 chars.

{5⌷⍵+(+×5⌷⍵:5⌷⍵⋄+/⍵)×0=5⌷⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1

Sample run:

      {5⌷⍵+(+×5⌷⍵:5⌷⍵⋄+/⍵)×0=5⌷⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1
49  89 46  41 67  35 16
97 4955 75 4539 69 4654 89
39  8 62  41 8  51 9
72 4143 48 3641 76 4942 85
8  4 17  23 4  14 7
8 37 63 2
2 29 81 3726 2 35 8
76  4 2 1 3 2 5 3 7 3

Explanation:

  • ∘.∨⍨9⍴0 1 generates a bit mask.
  • ×∘?∘9¨ multiplies each bit by a random value from 1 to 9 inclusive, generating a masked grid of random numbers.
  • 3,⌿3,/ uses what can only be described as hackery to return all 3 by 3 overlapping boxes in the masked array. These are also flattened in the process.
  • {5⌷⍵+(+×5⌷⍵:5⌷⍵⋄+/⍵)×0=5⌷⍵ iterates over the array, assigning each element to . For each iteration, it does the following:
  • 0=5⌷⍵ takes the fifth (middle, remembering that APL indexing is 1-based), and compares it withreturns its sign. In this case this is equivalent to testing if the number is greater than 0. ItIf this returns 1 if(for true), and 0 if notthen return that element.
  • (+/⍵)× multiplies the result with Otherwise, return the sum of the elements in the flattened 3 by 3 box.
  •   It uses the 5⌷⍵+:⋄ then re-adds the middle element toternary operator, which is the resultequivalent of ?: in many languages.

APL, 53 49 43 42 40 39

I managed to replicate J's ;. in APL, and used Gareth's approach, saving 10 chars.

{5⌷⍵+(+/⍵)×0=5⌷⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1

Sample run:

      {5⌷⍵+(+/⍵)×0=5⌷⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1
4  8 4  4 6  3 1
9 49 7 45 6 46 8
3  8 6  4 8  5 9
7 41 4 36 7 49 8
8  4 1  2 4  1 7
8 37 6 29 8 37 2
7  2 1  2 5  7 3

Explanation:

  • ∘.∨⍨9⍴0 1 generates a bit mask.
  • ×∘?∘9¨ multiplies each bit by a random value from 1 to 9 inclusive, generating a masked grid of random numbers.
  • 3,⌿3,/ uses what can only be described as hackery to return all 3 by 3 overlapping boxes in the masked array. These are also flattened in the process.
  • {5⌷⍵+(+/⍵)×0=5⌷⍵ iterates over the array, assigning each element to . For each iteration, it does the following:
  • 0=5⌷⍵ takes the fifth (middle, remembering that APL indexing is 1-based), and compares it with 0. It returns 1 if true, and 0 if not.
  • (+/⍵)× multiplies the result with the sum of the elements in the box.
  •  5⌷⍵+ then re-adds the middle element to the result.

APL, 53 49 43 42 40 39 36

I managed to replicate J's ;. in APL, and used Gareth's approach, saving 13 chars.

{×5⌷⍵:5⌷⍵⋄+/⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1

Sample run:

      {×5⌷⍵:5⌷⍵⋄+/⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1
9  9 6  1 7  5 6
7 55 5 39 9 54 9
9  8 2  1 8  1 9
2 43 8 41 6 42 5
7  3 4  4 8  3 2
2 29 1 26 2 35 8
6  4 2  3 2  3 7

Explanation:

  • ∘.∨⍨9⍴0 1 generates a bit mask.
  • ×∘?∘9¨ multiplies each bit by a random value from 1 to 9 inclusive, generating a masked grid of random numbers.
  • 3,⌿3,/ uses what can only be described as hackery to return all 3 by 3 overlapping boxes in the masked array. These are also flattened in the process.
  • {×5⌷⍵:5⌷⍵⋄+/⍵}¨ iterates over the array, assigning each element to . For each iteration, it takes the fifth (middle, remembering that APL indexing is 1-based), and returns its sign. In this case this is equivalent to testing if the number is greater than 0. If this returns 1 (for true), then return that element. Otherwise, return the sum of the elements in the flattened 3 by 3 box. It uses the :⋄ ternary operator, which is the equivalent of ?: in many languages.
added 1 characters in body
Source Link
Volatility
  • 3.4k
  • 2
  • 20
  • 35

APL, 53 49 43 42 40 4039

I managed to replicate J's ;. in APL, and used Gareth's approach, saving 910 chars.

{5⌷⍵+(+/⍵)×0=5⌷⍵}¨3,⌿3,/{⍵××∘?9}¨∘∘9¨∘.∨⍨9⍴0 1

Sample run:

      {5⌷⍵+(+/⍵)×0=5⌷⍵}¨3,⌿3,/{⍵××∘?9}¨∘∘9¨∘.∨⍨9⍴0 1
5 4 1 38 4 6 4 6 2 93 1
9 4249 67 3845 46 2846 58
7  3 8 8 6 1 4 18 2
5 385 29
7 2741 14 2936 7 49 8
78  4 21  32 4  51 87
18 3237 16 4229 68 4537 32
47  42 91  92 85  87 3

Explanation:

  • ∘.∨⍨9⍴0 1 generates a bit mask.
  • {⍵××∘?9}¨∘9¨ multiplies each bit by a random value from 1 to 9 inclusive, generating a masked grid of random numbers.
  • 3,⌿3,/ uses what can only be described as hackery to return all 3 by 3 overlapping boxes in the masked array. These are also flattened in the process.
  • {5⌷⍵+(+/⍵)×0=5⌷⍵}¨ iterates over the array, assigning each element to . For each iteration, it does the following:
  • 0=5⌷⍵ takes the fifth (middle, remembering that APL indexing is 1-based), and compares it with 0. It returns 1 if true, and 0 if not.
  • (+/⍵)× multiplies the result with the sum of the elements in the box.
  • 5⌷⍵+ then re-adds the middle element to the result.

APL, 53 49 43 42 40

I managed to replicate J's ;. in APL, and used Gareth's approach, saving 9 chars.

{5⌷⍵+(+/⍵)×0=5⌷⍵}¨3,⌿3,/{⍵×?9}¨∘.∨⍨9⍴0 1

Sample run:

      {5⌷⍵+(+/⍵)×0=5⌷⍵}¨3,⌿3,/{⍵×?9}¨∘.∨⍨9⍴0 1
5  1 3  6 4  2 9
9 42 6 38 4 28 5
7  3 8  6 1  1 2
5 38 2 27 1 29 7
7  4 2  3 4  5 8
1 32 1 42 6 45 3
4  4 9  9 8  8 3

Explanation:

  • ∘.∨⍨9⍴0 1 generates a bit mask.
  • {⍵×?9}¨ multiplies each bit by a random value from 1 to 9 inclusive, generating a masked grid of random numbers.
  • 3,⌿3,/ uses what can only be described as hackery to return all 3 by 3 overlapping boxes in the masked array. These are also flattened in the process.
  • {5⌷⍵+(+/⍵)×0=5⌷⍵}¨ iterates over the array, assigning each element to . For each iteration, it does the following:
  • 0=5⌷⍵ takes the fifth (middle, remembering that APL indexing is 1-based), and compares it with 0. It returns 1 if true, and 0 if not.
  • (+/⍵)× multiplies the result with the sum of the elements in the box.
  • 5⌷⍵+ then re-adds the middle element to the result.

APL, 53 49 43 42 40 39

I managed to replicate J's ;. in APL, and used Gareth's approach, saving 10 chars.

{5⌷⍵+(+/⍵)×0=5⌷⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1

Sample run:

      {5⌷⍵+(+/⍵)×0=5⌷⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1
4  8 4  4 6  3 1
9 49 7 45 6 46 8
3  8 6  4 8  5 9
7 41 4 36 7 49 8
8  4 1  2 4  1 7
8 37 6 29 8 37 2
7  2 1  2 5  7 3

Explanation:

  • ∘.∨⍨9⍴0 1 generates a bit mask.
  • ×∘?∘9¨ multiplies each bit by a random value from 1 to 9 inclusive, generating a masked grid of random numbers.
  • 3,⌿3,/ uses what can only be described as hackery to return all 3 by 3 overlapping boxes in the masked array. These are also flattened in the process.
  • {5⌷⍵+(+/⍵)×0=5⌷⍵}¨ iterates over the array, assigning each element to . For each iteration, it does the following:
  • 0=5⌷⍵ takes the fifth (middle, remembering that APL indexing is 1-based), and compares it with 0. It returns 1 if true, and 0 if not.
  • (+/⍵)× multiplies the result with the sum of the elements in the box.
  • 5⌷⍵+ then re-adds the middle element to the result.
deleted 21 characters in body
Source Link
Volatility
  • 3.4k
  • 2
  • 20
  • 35
Loading
edited body
Source Link
Volatility
  • 3.4k
  • 2
  • 20
  • 35
Loading
deleted 641 characters in body
Source Link
Volatility
  • 3.4k
  • 2
  • 20
  • 35
Loading
deleted 180 characters in body
Source Link
Volatility
  • 3.4k
  • 2
  • 20
  • 35
Loading
added 120 characters in body
Source Link
Volatility
  • 3.4k
  • 2
  • 20
  • 35
Loading
deleted 129 characters in body
Source Link
Volatility
  • 3.4k
  • 2
  • 20
  • 35
Loading
added 1602 characters in body
Source Link
Volatility
  • 3.4k
  • 2
  • 20
  • 35
Loading
Source Link
Volatility
  • 3.4k
  • 2
  • 20
  • 35
Loading