Exploding NumbersCrazy but Rational BasesLet's design a digit mosaicRemove surrounding zeroes of a 2d...
Buying a "Used" Router
Using 14 ga on part of a 20A circuit?
How can guns be countered by melee combat without raw-ability or exceptional explanations?
Is it ethical to apply for a job on someone's behalf?
What is the source for this Leonardo Da Vinci quote?
Is it possible to detect 100% of SQLi with a simple regex?
Why is Bernie Sanders maximum accepted donation on actblue $5600?
Why Third 'Reich'? Why is 'reich' not translated when 'third' is? What is the English synonym of reich?
Was Opportunity's last message to Earth "My battery is low and it's getting dark"?
Why does finding small effects in large studies indicate publication bias?
How sharp are RAW photos before processing?
Ramanujan's radical and how we define an infinite nested radical
Determining whether a system is consistent or inconsistent from the linear dependence of its columns
Does an intelligent undead have a soul in 5e D&D?
I hate taking lectures, can I still survive in academia?
How can I make my enemies feel real and make combat more engaging?
A semicolon (';') is not needed after a function declaration. C++
Is my plan for an air admittance valve ok?
Can you wish for more wishes from an Efreeti bound to service via an Efreeti Bottle?
Have any astronauts or cosmonauts died in space?
Boiling meatballs, how long?
Misplaced noalign when centering tabularx cell
How bad is a Computer Science course that doesn't teach Design Patterns?
Truncating the output of AES-128
Exploding Numbers
Crazy but Rational BasesLet's design a digit mosaicRemove surrounding zeroes of a 2d arrayChop off the matrix to get the desired sumOutput the anti-clockwise inward spiral of a 2D arrayTrim that distracting background off!The Hungry MouseMatrix rotation sortBorderless tableCell phone Charge
$begingroup$
sandbox (deleted)
Lets define a matrix of 9s as:
$$ N = begin{bmatrix} 9&9&9\9&9&9\9&9&9 end{bmatrix} $$
Lets define an exploding number as a number at position $(x,y)$ that can be decomposed into equal integers between all its adjacent neighbors (including itself) and the absolute value of each portion is greater than 0.
From the previous matrix, lets explode the number at position $(1,1)$ (0 indexed)
$$ N = begin{bmatrix} 9&9&9\9&color{red}9&9\9&9&9 end{bmatrix} $$
$$ N = begin{bmatrix} 9+color{red}1&9+color{red}1&9+color{red}1\9+color{red}1&color{blue}0+color{red}1&9+color{red}1\9+color{red}1&9+color{red}1&9+color{red}1 end{bmatrix} $$
$$ N = begin{bmatrix} 10&10&10\10&color{red}1&10\10&10&10 end{bmatrix} $$
Sometimes, decomposing result into a rational number greater than 1. This is something we need to avoid when exploding numbers. In this cases the remainder will be assigned to the exploded number.
To demonstrate it, lets continue working with our previous matrix. This time we will explode the number at position $(0,0)$
$$ N = begin{bmatrix} color{red}{10}&10&10\10&1&10\10&10&10 end{bmatrix} $$
Here we have 3 neightbors and the number itself. Here the equation is something like $10/4$ which give us 2 for each and 2 as remainder.
$$ N = begin{bmatrix} color{blue}2+color{red}{2}&color{red}{10+2}&10\color{red}{10+2}&color{red}{1+2}&10\10&10&10 end{bmatrix} $$
$$ N = begin{bmatrix} color{red}{4}&12&10\12&3&10\10&10&10 end{bmatrix} $$
As well, sometimes a number wont be big enough to be decomposed in equal parts (where the abs is greater than 0) between his neighbors (|rational number| < 1). In this cases we need to "borrow" from the exploded number in order to maintain the "greater than 0" condition. Lets continue with our previous example and explode the number at position $(1,1)$.
$$ N = begin{bmatrix} 4&12&10\12&color{red}3&10\10&10&10 end{bmatrix} $$
$$ N = begin{bmatrix} 4+color{red}1&12+color{red}1&10+color{red}1\12+color{red}1&color{blue}0+color{red}{1}-color{green}6&10+color{red}1\10+color{red}1&10+color{red}1&10+color{red}1 end{bmatrix} $$
$$ N = begin{bmatrix} 5&13&11\13&color{red}{-5}&11\11&11&11 end{bmatrix} $$
The challenge is, given a list of $(x,y)$ positions and an finite non-empty array of natural numbers, return the exploded form after each number from the positions list has been exploded.
Test cases
Input: initial matrix: [[3, 3, 3], [3, 3, 3], [3, 3, 3]], numbers: [[0,0],[0,1],[0,2]]
Output: [[1, 0, 1], [5, 6, 5], [3, 3, 3]]
Input: Initial matrix: [[9, 8, 7], [8, 9, 7], [8, 7, 9]], numbers: [[0,0],[1,1],[2,2]]
Output: [[4, 11, 8],[11, 5, 10],[9, 10, 4]]
Input: Initial matrix: [[0, 0], [0, 0]], numbers: [[0,0],[0,0],[0,0]]
Output: [[-9, 3],[3, 3]]
Input: Initial Matrix: [[10, 20, 30],[30, 20, 10],[40, 50, 60]], numbers: [[0,2],[2,0],[1,1],[1,0]]
Output: [[21, 38, 13], [9, 12, 21], [21, 71, 64]]
code-golf matrix
$endgroup$
add a comment |
$begingroup$
sandbox (deleted)
Lets define a matrix of 9s as:
$$ N = begin{bmatrix} 9&9&9\9&9&9\9&9&9 end{bmatrix} $$
Lets define an exploding number as a number at position $(x,y)$ that can be decomposed into equal integers between all its adjacent neighbors (including itself) and the absolute value of each portion is greater than 0.
From the previous matrix, lets explode the number at position $(1,1)$ (0 indexed)
$$ N = begin{bmatrix} 9&9&9\9&color{red}9&9\9&9&9 end{bmatrix} $$
$$ N = begin{bmatrix} 9+color{red}1&9+color{red}1&9+color{red}1\9+color{red}1&color{blue}0+color{red}1&9+color{red}1\9+color{red}1&9+color{red}1&9+color{red}1 end{bmatrix} $$
$$ N = begin{bmatrix} 10&10&10\10&color{red}1&10\10&10&10 end{bmatrix} $$
Sometimes, decomposing result into a rational number greater than 1. This is something we need to avoid when exploding numbers. In this cases the remainder will be assigned to the exploded number.
To demonstrate it, lets continue working with our previous matrix. This time we will explode the number at position $(0,0)$
$$ N = begin{bmatrix} color{red}{10}&10&10\10&1&10\10&10&10 end{bmatrix} $$
Here we have 3 neightbors and the number itself. Here the equation is something like $10/4$ which give us 2 for each and 2 as remainder.
$$ N = begin{bmatrix} color{blue}2+color{red}{2}&color{red}{10+2}&10\color{red}{10+2}&color{red}{1+2}&10\10&10&10 end{bmatrix} $$
$$ N = begin{bmatrix} color{red}{4}&12&10\12&3&10\10&10&10 end{bmatrix} $$
As well, sometimes a number wont be big enough to be decomposed in equal parts (where the abs is greater than 0) between his neighbors (|rational number| < 1). In this cases we need to "borrow" from the exploded number in order to maintain the "greater than 0" condition. Lets continue with our previous example and explode the number at position $(1,1)$.
$$ N = begin{bmatrix} 4&12&10\12&color{red}3&10\10&10&10 end{bmatrix} $$
$$ N = begin{bmatrix} 4+color{red}1&12+color{red}1&10+color{red}1\12+color{red}1&color{blue}0+color{red}{1}-color{green}6&10+color{red}1\10+color{red}1&10+color{red}1&10+color{red}1 end{bmatrix} $$
$$ N = begin{bmatrix} 5&13&11\13&color{red}{-5}&11\11&11&11 end{bmatrix} $$
The challenge is, given a list of $(x,y)$ positions and an finite non-empty array of natural numbers, return the exploded form after each number from the positions list has been exploded.
Test cases
Input: initial matrix: [[3, 3, 3], [3, 3, 3], [3, 3, 3]], numbers: [[0,0],[0,1],[0,2]]
Output: [[1, 0, 1], [5, 6, 5], [3, 3, 3]]
Input: Initial matrix: [[9, 8, 7], [8, 9, 7], [8, 7, 9]], numbers: [[0,0],[1,1],[2,2]]
Output: [[4, 11, 8],[11, 5, 10],[9, 10, 4]]
Input: Initial matrix: [[0, 0], [0, 0]], numbers: [[0,0],[0,0],[0,0]]
Output: [[-9, 3],[3, 3]]
Input: Initial Matrix: [[10, 20, 30],[30, 20, 10],[40, 50, 60]], numbers: [[0,2],[2,0],[1,1],[1,0]]
Output: [[21, 38, 13], [9, 12, 21], [21, 71, 64]]
code-golf matrix
$endgroup$
$begingroup$
new to code golf; what format is the sample allowed to take these matrices in? Any format that exists in the language? String form exactly as written?
$endgroup$
– rtpax
6 hours ago
2
$begingroup$
@rtpax Please take a look at the Input/Output methods allowed in this site.
$endgroup$
– Luis felipe De jesus Munoz
6 hours ago
$begingroup$
Basically you can use any format that your language can represent as a multidimensional array (list of lists, etc...)
$endgroup$
– Luis felipe De jesus Munoz
6 hours ago
$begingroup$
I suggest adding a test case for non-square matricies.
$endgroup$
– Οurous
4 hours ago
$begingroup$
@Ourous uh oh, I had been writing my program assuming they were guaranteed to be square, back to the drawing board I guess
$endgroup$
– rtpax
4 hours ago
add a comment |
$begingroup$
sandbox (deleted)
Lets define a matrix of 9s as:
$$ N = begin{bmatrix} 9&9&9\9&9&9\9&9&9 end{bmatrix} $$
Lets define an exploding number as a number at position $(x,y)$ that can be decomposed into equal integers between all its adjacent neighbors (including itself) and the absolute value of each portion is greater than 0.
From the previous matrix, lets explode the number at position $(1,1)$ (0 indexed)
$$ N = begin{bmatrix} 9&9&9\9&color{red}9&9\9&9&9 end{bmatrix} $$
$$ N = begin{bmatrix} 9+color{red}1&9+color{red}1&9+color{red}1\9+color{red}1&color{blue}0+color{red}1&9+color{red}1\9+color{red}1&9+color{red}1&9+color{red}1 end{bmatrix} $$
$$ N = begin{bmatrix} 10&10&10\10&color{red}1&10\10&10&10 end{bmatrix} $$
Sometimes, decomposing result into a rational number greater than 1. This is something we need to avoid when exploding numbers. In this cases the remainder will be assigned to the exploded number.
To demonstrate it, lets continue working with our previous matrix. This time we will explode the number at position $(0,0)$
$$ N = begin{bmatrix} color{red}{10}&10&10\10&1&10\10&10&10 end{bmatrix} $$
Here we have 3 neightbors and the number itself. Here the equation is something like $10/4$ which give us 2 for each and 2 as remainder.
$$ N = begin{bmatrix} color{blue}2+color{red}{2}&color{red}{10+2}&10\color{red}{10+2}&color{red}{1+2}&10\10&10&10 end{bmatrix} $$
$$ N = begin{bmatrix} color{red}{4}&12&10\12&3&10\10&10&10 end{bmatrix} $$
As well, sometimes a number wont be big enough to be decomposed in equal parts (where the abs is greater than 0) between his neighbors (|rational number| < 1). In this cases we need to "borrow" from the exploded number in order to maintain the "greater than 0" condition. Lets continue with our previous example and explode the number at position $(1,1)$.
$$ N = begin{bmatrix} 4&12&10\12&color{red}3&10\10&10&10 end{bmatrix} $$
$$ N = begin{bmatrix} 4+color{red}1&12+color{red}1&10+color{red}1\12+color{red}1&color{blue}0+color{red}{1}-color{green}6&10+color{red}1\10+color{red}1&10+color{red}1&10+color{red}1 end{bmatrix} $$
$$ N = begin{bmatrix} 5&13&11\13&color{red}{-5}&11\11&11&11 end{bmatrix} $$
The challenge is, given a list of $(x,y)$ positions and an finite non-empty array of natural numbers, return the exploded form after each number from the positions list has been exploded.
Test cases
Input: initial matrix: [[3, 3, 3], [3, 3, 3], [3, 3, 3]], numbers: [[0,0],[0,1],[0,2]]
Output: [[1, 0, 1], [5, 6, 5], [3, 3, 3]]
Input: Initial matrix: [[9, 8, 7], [8, 9, 7], [8, 7, 9]], numbers: [[0,0],[1,1],[2,2]]
Output: [[4, 11, 8],[11, 5, 10],[9, 10, 4]]
Input: Initial matrix: [[0, 0], [0, 0]], numbers: [[0,0],[0,0],[0,0]]
Output: [[-9, 3],[3, 3]]
Input: Initial Matrix: [[10, 20, 30],[30, 20, 10],[40, 50, 60]], numbers: [[0,2],[2,0],[1,1],[1,0]]
Output: [[21, 38, 13], [9, 12, 21], [21, 71, 64]]
code-golf matrix
$endgroup$
sandbox (deleted)
Lets define a matrix of 9s as:
$$ N = begin{bmatrix} 9&9&9\9&9&9\9&9&9 end{bmatrix} $$
Lets define an exploding number as a number at position $(x,y)$ that can be decomposed into equal integers between all its adjacent neighbors (including itself) and the absolute value of each portion is greater than 0.
From the previous matrix, lets explode the number at position $(1,1)$ (0 indexed)
$$ N = begin{bmatrix} 9&9&9\9&color{red}9&9\9&9&9 end{bmatrix} $$
$$ N = begin{bmatrix} 9+color{red}1&9+color{red}1&9+color{red}1\9+color{red}1&color{blue}0+color{red}1&9+color{red}1\9+color{red}1&9+color{red}1&9+color{red}1 end{bmatrix} $$
$$ N = begin{bmatrix} 10&10&10\10&color{red}1&10\10&10&10 end{bmatrix} $$
Sometimes, decomposing result into a rational number greater than 1. This is something we need to avoid when exploding numbers. In this cases the remainder will be assigned to the exploded number.
To demonstrate it, lets continue working with our previous matrix. This time we will explode the number at position $(0,0)$
$$ N = begin{bmatrix} color{red}{10}&10&10\10&1&10\10&10&10 end{bmatrix} $$
Here we have 3 neightbors and the number itself. Here the equation is something like $10/4$ which give us 2 for each and 2 as remainder.
$$ N = begin{bmatrix} color{blue}2+color{red}{2}&color{red}{10+2}&10\color{red}{10+2}&color{red}{1+2}&10\10&10&10 end{bmatrix} $$
$$ N = begin{bmatrix} color{red}{4}&12&10\12&3&10\10&10&10 end{bmatrix} $$
As well, sometimes a number wont be big enough to be decomposed in equal parts (where the abs is greater than 0) between his neighbors (|rational number| < 1). In this cases we need to "borrow" from the exploded number in order to maintain the "greater than 0" condition. Lets continue with our previous example and explode the number at position $(1,1)$.
$$ N = begin{bmatrix} 4&12&10\12&color{red}3&10\10&10&10 end{bmatrix} $$
$$ N = begin{bmatrix} 4+color{red}1&12+color{red}1&10+color{red}1\12+color{red}1&color{blue}0+color{red}{1}-color{green}6&10+color{red}1\10+color{red}1&10+color{red}1&10+color{red}1 end{bmatrix} $$
$$ N = begin{bmatrix} 5&13&11\13&color{red}{-5}&11\11&11&11 end{bmatrix} $$
The challenge is, given a list of $(x,y)$ positions and an finite non-empty array of natural numbers, return the exploded form after each number from the positions list has been exploded.
Test cases
Input: initial matrix: [[3, 3, 3], [3, 3, 3], [3, 3, 3]], numbers: [[0,0],[0,1],[0,2]]
Output: [[1, 0, 1], [5, 6, 5], [3, 3, 3]]
Input: Initial matrix: [[9, 8, 7], [8, 9, 7], [8, 7, 9]], numbers: [[0,0],[1,1],[2,2]]
Output: [[4, 11, 8],[11, 5, 10],[9, 10, 4]]
Input: Initial matrix: [[0, 0], [0, 0]], numbers: [[0,0],[0,0],[0,0]]
Output: [[-9, 3],[3, 3]]
Input: Initial Matrix: [[10, 20, 30],[30, 20, 10],[40, 50, 60]], numbers: [[0,2],[2,0],[1,1],[1,0]]
Output: [[21, 38, 13], [9, 12, 21], [21, 71, 64]]
code-golf matrix
code-golf matrix
asked 8 hours ago
Luis felipe De jesus MunozLuis felipe De jesus Munoz
4,79721465
4,79721465
$begingroup$
new to code golf; what format is the sample allowed to take these matrices in? Any format that exists in the language? String form exactly as written?
$endgroup$
– rtpax
6 hours ago
2
$begingroup$
@rtpax Please take a look at the Input/Output methods allowed in this site.
$endgroup$
– Luis felipe De jesus Munoz
6 hours ago
$begingroup$
Basically you can use any format that your language can represent as a multidimensional array (list of lists, etc...)
$endgroup$
– Luis felipe De jesus Munoz
6 hours ago
$begingroup$
I suggest adding a test case for non-square matricies.
$endgroup$
– Οurous
4 hours ago
$begingroup$
@Ourous uh oh, I had been writing my program assuming they were guaranteed to be square, back to the drawing board I guess
$endgroup$
– rtpax
4 hours ago
add a comment |
$begingroup$
new to code golf; what format is the sample allowed to take these matrices in? Any format that exists in the language? String form exactly as written?
$endgroup$
– rtpax
6 hours ago
2
$begingroup$
@rtpax Please take a look at the Input/Output methods allowed in this site.
$endgroup$
– Luis felipe De jesus Munoz
6 hours ago
$begingroup$
Basically you can use any format that your language can represent as a multidimensional array (list of lists, etc...)
$endgroup$
– Luis felipe De jesus Munoz
6 hours ago
$begingroup$
I suggest adding a test case for non-square matricies.
$endgroup$
– Οurous
4 hours ago
$begingroup$
@Ourous uh oh, I had been writing my program assuming they were guaranteed to be square, back to the drawing board I guess
$endgroup$
– rtpax
4 hours ago
$begingroup$
new to code golf; what format is the sample allowed to take these matrices in? Any format that exists in the language? String form exactly as written?
$endgroup$
– rtpax
6 hours ago
$begingroup$
new to code golf; what format is the sample allowed to take these matrices in? Any format that exists in the language? String form exactly as written?
$endgroup$
– rtpax
6 hours ago
2
2
$begingroup$
@rtpax Please take a look at the Input/Output methods allowed in this site.
$endgroup$
– Luis felipe De jesus Munoz
6 hours ago
$begingroup$
@rtpax Please take a look at the Input/Output methods allowed in this site.
$endgroup$
– Luis felipe De jesus Munoz
6 hours ago
$begingroup$
Basically you can use any format that your language can represent as a multidimensional array (list of lists, etc...)
$endgroup$
– Luis felipe De jesus Munoz
6 hours ago
$begingroup$
Basically you can use any format that your language can represent as a multidimensional array (list of lists, etc...)
$endgroup$
– Luis felipe De jesus Munoz
6 hours ago
$begingroup$
I suggest adding a test case for non-square matricies.
$endgroup$
– Οurous
4 hours ago
$begingroup$
I suggest adding a test case for non-square matricies.
$endgroup$
– Οurous
4 hours ago
$begingroup$
@Ourous uh oh, I had been writing my program assuming they were guaranteed to be square, back to the drawing board I guess
$endgroup$
– rtpax
4 hours ago
$begingroup$
@Ourous uh oh, I had been writing my program assuming they were guaranteed to be square, back to the drawing board I guess
$endgroup$
– rtpax
4 hours ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
C(GCC) 220 216 214 bytes
#define L(v)for(int v=2;~v--;)
#define P l/C+r<0|l/C+r>=R|l%C+c<0|l%C+c>=C
f(int R,int C,int*m){for(int*i=m+R*C;~*i;){int*M,l=*i+++C**i++,a=0,b;L(r)L(c)P?:++a;M=m+l;b=*M/a;b+=!b;*M-=b*a;L(r)L(c)*(M+r*C+c)+=P?0:b;}}
Run it here
a slightly less golfed version
#define L(v)for(int v=2;~v--;)
#define P l/C+r<0|l/C+r>=R|l%C+c<0|l%C+c>=C
f(int R, int C, int*m) {
for(int*i=m+R*C;~*i;) {
int*M,l=*i+++C**i++,a=0,b;
L(r)
L(c)
P?:++a;
M=m+l;
b=*M/a;
b+=!b;
*M-=b*a;
L(r)
L(c)
*(M+r*C+c)+=P?0:b;
}
}
The calling code with an example
int main()
{
int matrix[] = {3,3,3,3,3,3,3,3,3,0,0,0,1,0,2,-1};
int rows = 3;
int columns = 3;
f(rows,columns,matrix);
for(int r = 0; r < rows; ++r) {
for(int c = 0; c < columns; ++c) {
printf("%03d,",matrix[r*columns + c]);
}
printf("n");
}
}
and the output
001,005,003,
000,006,003,
001,005,003,
New contributor
$endgroup$
7
$begingroup$
Welcome to PPCG :)
$endgroup$
– Shaggy
3 hours ago
add a comment |
$begingroup$
Clean, 181 167 bytes
import StdEnv;
foldlm(x,y)={{if(d>2)0b+e-if(d>0)0b*n\e<-:l&v<-[0..],let{b=max m.[y,x]n/n;$a b=2+sign a-(a+1)/size b;n= $x l* $y m;d=(v-x)^2+(u-y)^2}}\l<-:m&u<-[0..]}
Try it online!
In the form of a partially-applied function literal.
Expanded (first version):
f // functinon f on {{Int}} and [(Int,Int)]
= foldl m (x, y) // fold :: (a -> b -> a) a [b] -> a with first argument {{Int}} (Int,Int) -> {{Int}} giving {{Int}} [(Int,Int)] -> {{Int}}
= { // an array of
{ // arrays of
if(d > 2) 0 b // the amount we give to the neighbors
+ e // plus the current entry
- if(d > 0) 0 b // minus the amount taken from the target entry
* n // times the number of neighbors, if we're on the target
\ // for each
e <-: l // element of row l
& v <- [0..] // and x-index v
, let // local definitions:
b // the amount given to the neighbors
= max // we need at least 1 each, so take the largest of
m.[y, x] // the target entry
n // or the number of neighbors
/ n // divide it by the number of neighbors
n // the number of neighbors
= ( // sum of
1 // one
+ s x // if x is at the left edge = 0 else 1
+ s ( // if x is at the right edge = 0 else 1
size l
- x
- 1
)
) * ( // times the sum of
1 // one
+ s y // if y is at the top edge = 0 else 1
+ s ( // if y is at the bottom edge = 0 else 1
size m
- y
- 1
)
)
d // distance from the target point
= (v - x)^2
+ (u - y)^2
}
\ // for each
l <-: m // row l in matrix m
& u <- [0..] // and y-index u
}
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f180188%2fexploding-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
C(GCC) 220 216 214 bytes
#define L(v)for(int v=2;~v--;)
#define P l/C+r<0|l/C+r>=R|l%C+c<0|l%C+c>=C
f(int R,int C,int*m){for(int*i=m+R*C;~*i;){int*M,l=*i+++C**i++,a=0,b;L(r)L(c)P?:++a;M=m+l;b=*M/a;b+=!b;*M-=b*a;L(r)L(c)*(M+r*C+c)+=P?0:b;}}
Run it here
a slightly less golfed version
#define L(v)for(int v=2;~v--;)
#define P l/C+r<0|l/C+r>=R|l%C+c<0|l%C+c>=C
f(int R, int C, int*m) {
for(int*i=m+R*C;~*i;) {
int*M,l=*i+++C**i++,a=0,b;
L(r)
L(c)
P?:++a;
M=m+l;
b=*M/a;
b+=!b;
*M-=b*a;
L(r)
L(c)
*(M+r*C+c)+=P?0:b;
}
}
The calling code with an example
int main()
{
int matrix[] = {3,3,3,3,3,3,3,3,3,0,0,0,1,0,2,-1};
int rows = 3;
int columns = 3;
f(rows,columns,matrix);
for(int r = 0; r < rows; ++r) {
for(int c = 0; c < columns; ++c) {
printf("%03d,",matrix[r*columns + c]);
}
printf("n");
}
}
and the output
001,005,003,
000,006,003,
001,005,003,
New contributor
$endgroup$
7
$begingroup$
Welcome to PPCG :)
$endgroup$
– Shaggy
3 hours ago
add a comment |
$begingroup$
C(GCC) 220 216 214 bytes
#define L(v)for(int v=2;~v--;)
#define P l/C+r<0|l/C+r>=R|l%C+c<0|l%C+c>=C
f(int R,int C,int*m){for(int*i=m+R*C;~*i;){int*M,l=*i+++C**i++,a=0,b;L(r)L(c)P?:++a;M=m+l;b=*M/a;b+=!b;*M-=b*a;L(r)L(c)*(M+r*C+c)+=P?0:b;}}
Run it here
a slightly less golfed version
#define L(v)for(int v=2;~v--;)
#define P l/C+r<0|l/C+r>=R|l%C+c<0|l%C+c>=C
f(int R, int C, int*m) {
for(int*i=m+R*C;~*i;) {
int*M,l=*i+++C**i++,a=0,b;
L(r)
L(c)
P?:++a;
M=m+l;
b=*M/a;
b+=!b;
*M-=b*a;
L(r)
L(c)
*(M+r*C+c)+=P?0:b;
}
}
The calling code with an example
int main()
{
int matrix[] = {3,3,3,3,3,3,3,3,3,0,0,0,1,0,2,-1};
int rows = 3;
int columns = 3;
f(rows,columns,matrix);
for(int r = 0; r < rows; ++r) {
for(int c = 0; c < columns; ++c) {
printf("%03d,",matrix[r*columns + c]);
}
printf("n");
}
}
and the output
001,005,003,
000,006,003,
001,005,003,
New contributor
$endgroup$
7
$begingroup$
Welcome to PPCG :)
$endgroup$
– Shaggy
3 hours ago
add a comment |
$begingroup$
C(GCC) 220 216 214 bytes
#define L(v)for(int v=2;~v--;)
#define P l/C+r<0|l/C+r>=R|l%C+c<0|l%C+c>=C
f(int R,int C,int*m){for(int*i=m+R*C;~*i;){int*M,l=*i+++C**i++,a=0,b;L(r)L(c)P?:++a;M=m+l;b=*M/a;b+=!b;*M-=b*a;L(r)L(c)*(M+r*C+c)+=P?0:b;}}
Run it here
a slightly less golfed version
#define L(v)for(int v=2;~v--;)
#define P l/C+r<0|l/C+r>=R|l%C+c<0|l%C+c>=C
f(int R, int C, int*m) {
for(int*i=m+R*C;~*i;) {
int*M,l=*i+++C**i++,a=0,b;
L(r)
L(c)
P?:++a;
M=m+l;
b=*M/a;
b+=!b;
*M-=b*a;
L(r)
L(c)
*(M+r*C+c)+=P?0:b;
}
}
The calling code with an example
int main()
{
int matrix[] = {3,3,3,3,3,3,3,3,3,0,0,0,1,0,2,-1};
int rows = 3;
int columns = 3;
f(rows,columns,matrix);
for(int r = 0; r < rows; ++r) {
for(int c = 0; c < columns; ++c) {
printf("%03d,",matrix[r*columns + c]);
}
printf("n");
}
}
and the output
001,005,003,
000,006,003,
001,005,003,
New contributor
$endgroup$
C(GCC) 220 216 214 bytes
#define L(v)for(int v=2;~v--;)
#define P l/C+r<0|l/C+r>=R|l%C+c<0|l%C+c>=C
f(int R,int C,int*m){for(int*i=m+R*C;~*i;){int*M,l=*i+++C**i++,a=0,b;L(r)L(c)P?:++a;M=m+l;b=*M/a;b+=!b;*M-=b*a;L(r)L(c)*(M+r*C+c)+=P?0:b;}}
Run it here
a slightly less golfed version
#define L(v)for(int v=2;~v--;)
#define P l/C+r<0|l/C+r>=R|l%C+c<0|l%C+c>=C
f(int R, int C, int*m) {
for(int*i=m+R*C;~*i;) {
int*M,l=*i+++C**i++,a=0,b;
L(r)
L(c)
P?:++a;
M=m+l;
b=*M/a;
b+=!b;
*M-=b*a;
L(r)
L(c)
*(M+r*C+c)+=P?0:b;
}
}
The calling code with an example
int main()
{
int matrix[] = {3,3,3,3,3,3,3,3,3,0,0,0,1,0,2,-1};
int rows = 3;
int columns = 3;
f(rows,columns,matrix);
for(int r = 0; r < rows; ++r) {
for(int c = 0; c < columns; ++c) {
printf("%03d,",matrix[r*columns + c]);
}
printf("n");
}
}
and the output
001,005,003,
000,006,003,
001,005,003,
New contributor
edited 2 hours ago
New contributor
answered 3 hours ago
rtpaxrtpax
1413
1413
New contributor
New contributor
7
$begingroup$
Welcome to PPCG :)
$endgroup$
– Shaggy
3 hours ago
add a comment |
7
$begingroup$
Welcome to PPCG :)
$endgroup$
– Shaggy
3 hours ago
7
7
$begingroup$
Welcome to PPCG :)
$endgroup$
– Shaggy
3 hours ago
$begingroup$
Welcome to PPCG :)
$endgroup$
– Shaggy
3 hours ago
add a comment |
$begingroup$
Clean, 181 167 bytes
import StdEnv;
foldlm(x,y)={{if(d>2)0b+e-if(d>0)0b*n\e<-:l&v<-[0..],let{b=max m.[y,x]n/n;$a b=2+sign a-(a+1)/size b;n= $x l* $y m;d=(v-x)^2+(u-y)^2}}\l<-:m&u<-[0..]}
Try it online!
In the form of a partially-applied function literal.
Expanded (first version):
f // functinon f on {{Int}} and [(Int,Int)]
= foldl m (x, y) // fold :: (a -> b -> a) a [b] -> a with first argument {{Int}} (Int,Int) -> {{Int}} giving {{Int}} [(Int,Int)] -> {{Int}}
= { // an array of
{ // arrays of
if(d > 2) 0 b // the amount we give to the neighbors
+ e // plus the current entry
- if(d > 0) 0 b // minus the amount taken from the target entry
* n // times the number of neighbors, if we're on the target
\ // for each
e <-: l // element of row l
& v <- [0..] // and x-index v
, let // local definitions:
b // the amount given to the neighbors
= max // we need at least 1 each, so take the largest of
m.[y, x] // the target entry
n // or the number of neighbors
/ n // divide it by the number of neighbors
n // the number of neighbors
= ( // sum of
1 // one
+ s x // if x is at the left edge = 0 else 1
+ s ( // if x is at the right edge = 0 else 1
size l
- x
- 1
)
) * ( // times the sum of
1 // one
+ s y // if y is at the top edge = 0 else 1
+ s ( // if y is at the bottom edge = 0 else 1
size m
- y
- 1
)
)
d // distance from the target point
= (v - x)^2
+ (u - y)^2
}
\ // for each
l <-: m // row l in matrix m
& u <- [0..] // and y-index u
}
$endgroup$
add a comment |
$begingroup$
Clean, 181 167 bytes
import StdEnv;
foldlm(x,y)={{if(d>2)0b+e-if(d>0)0b*n\e<-:l&v<-[0..],let{b=max m.[y,x]n/n;$a b=2+sign a-(a+1)/size b;n= $x l* $y m;d=(v-x)^2+(u-y)^2}}\l<-:m&u<-[0..]}
Try it online!
In the form of a partially-applied function literal.
Expanded (first version):
f // functinon f on {{Int}} and [(Int,Int)]
= foldl m (x, y) // fold :: (a -> b -> a) a [b] -> a with first argument {{Int}} (Int,Int) -> {{Int}} giving {{Int}} [(Int,Int)] -> {{Int}}
= { // an array of
{ // arrays of
if(d > 2) 0 b // the amount we give to the neighbors
+ e // plus the current entry
- if(d > 0) 0 b // minus the amount taken from the target entry
* n // times the number of neighbors, if we're on the target
\ // for each
e <-: l // element of row l
& v <- [0..] // and x-index v
, let // local definitions:
b // the amount given to the neighbors
= max // we need at least 1 each, so take the largest of
m.[y, x] // the target entry
n // or the number of neighbors
/ n // divide it by the number of neighbors
n // the number of neighbors
= ( // sum of
1 // one
+ s x // if x is at the left edge = 0 else 1
+ s ( // if x is at the right edge = 0 else 1
size l
- x
- 1
)
) * ( // times the sum of
1 // one
+ s y // if y is at the top edge = 0 else 1
+ s ( // if y is at the bottom edge = 0 else 1
size m
- y
- 1
)
)
d // distance from the target point
= (v - x)^2
+ (u - y)^2
}
\ // for each
l <-: m // row l in matrix m
& u <- [0..] // and y-index u
}
$endgroup$
add a comment |
$begingroup$
Clean, 181 167 bytes
import StdEnv;
foldlm(x,y)={{if(d>2)0b+e-if(d>0)0b*n\e<-:l&v<-[0..],let{b=max m.[y,x]n/n;$a b=2+sign a-(a+1)/size b;n= $x l* $y m;d=(v-x)^2+(u-y)^2}}\l<-:m&u<-[0..]}
Try it online!
In the form of a partially-applied function literal.
Expanded (first version):
f // functinon f on {{Int}} and [(Int,Int)]
= foldl m (x, y) // fold :: (a -> b -> a) a [b] -> a with first argument {{Int}} (Int,Int) -> {{Int}} giving {{Int}} [(Int,Int)] -> {{Int}}
= { // an array of
{ // arrays of
if(d > 2) 0 b // the amount we give to the neighbors
+ e // plus the current entry
- if(d > 0) 0 b // minus the amount taken from the target entry
* n // times the number of neighbors, if we're on the target
\ // for each
e <-: l // element of row l
& v <- [0..] // and x-index v
, let // local definitions:
b // the amount given to the neighbors
= max // we need at least 1 each, so take the largest of
m.[y, x] // the target entry
n // or the number of neighbors
/ n // divide it by the number of neighbors
n // the number of neighbors
= ( // sum of
1 // one
+ s x // if x is at the left edge = 0 else 1
+ s ( // if x is at the right edge = 0 else 1
size l
- x
- 1
)
) * ( // times the sum of
1 // one
+ s y // if y is at the top edge = 0 else 1
+ s ( // if y is at the bottom edge = 0 else 1
size m
- y
- 1
)
)
d // distance from the target point
= (v - x)^2
+ (u - y)^2
}
\ // for each
l <-: m // row l in matrix m
& u <- [0..] // and y-index u
}
$endgroup$
Clean, 181 167 bytes
import StdEnv;
foldlm(x,y)={{if(d>2)0b+e-if(d>0)0b*n\e<-:l&v<-[0..],let{b=max m.[y,x]n/n;$a b=2+sign a-(a+1)/size b;n= $x l* $y m;d=(v-x)^2+(u-y)^2}}\l<-:m&u<-[0..]}
Try it online!
In the form of a partially-applied function literal.
Expanded (first version):
f // functinon f on {{Int}} and [(Int,Int)]
= foldl m (x, y) // fold :: (a -> b -> a) a [b] -> a with first argument {{Int}} (Int,Int) -> {{Int}} giving {{Int}} [(Int,Int)] -> {{Int}}
= { // an array of
{ // arrays of
if(d > 2) 0 b // the amount we give to the neighbors
+ e // plus the current entry
- if(d > 0) 0 b // minus the amount taken from the target entry
* n // times the number of neighbors, if we're on the target
\ // for each
e <-: l // element of row l
& v <- [0..] // and x-index v
, let // local definitions:
b // the amount given to the neighbors
= max // we need at least 1 each, so take the largest of
m.[y, x] // the target entry
n // or the number of neighbors
/ n // divide it by the number of neighbors
n // the number of neighbors
= ( // sum of
1 // one
+ s x // if x is at the left edge = 0 else 1
+ s ( // if x is at the right edge = 0 else 1
size l
- x
- 1
)
) * ( // times the sum of
1 // one
+ s y // if y is at the top edge = 0 else 1
+ s ( // if y is at the bottom edge = 0 else 1
size m
- y
- 1
)
)
d // distance from the target point
= (v - x)^2
+ (u - y)^2
}
\ // for each
l <-: m // row l in matrix m
& u <- [0..] // and y-index u
}
edited 48 mins ago
answered 1 hour ago
ΟurousΟurous
7,12111035
7,12111035
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f180188%2fexploding-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
new to code golf; what format is the sample allowed to take these matrices in? Any format that exists in the language? String form exactly as written?
$endgroup$
– rtpax
6 hours ago
2
$begingroup$
@rtpax Please take a look at the Input/Output methods allowed in this site.
$endgroup$
– Luis felipe De jesus Munoz
6 hours ago
$begingroup$
Basically you can use any format that your language can represent as a multidimensional array (list of lists, etc...)
$endgroup$
– Luis felipe De jesus Munoz
6 hours ago
$begingroup$
I suggest adding a test case for non-square matricies.
$endgroup$
– Οurous
4 hours ago
$begingroup$
@Ourous uh oh, I had been writing my program assuming they were guaranteed to be square, back to the drawing board I guess
$endgroup$
– rtpax
4 hours ago