11
1
Executive summary
Given input representing two vectors and their respective "weights", produce output that also represents the weighted sum of those vectors.
Challenge
The input will consist of one or more lines of the following characters:
- exactly one occurrence of the digit 0, which represents the origin in a two-dimensional plane;
- exactly two other digits (1-9; may or may not be the same digit), whose positions relative to the origin represent vectors, and whose values represent the weights attached to thse vectors;
- some number of "background characters". The solver may choose a specific background character; for example, I will choose "." (mostly for human readability). Alternately, the background characters can be anything that look like blank space.
(The solver can choose whether the input is a single multi-line string or an array of one-line strings.)
For example, the input
....2
.0...
...3.
represents a vector at coordinates (3,1) with weight 2, and a vector at coordinates (2,-1) with weight 3.
The output should be almost the same as the input, with the following changes:
- a "result character", chosen by the solver, to be added at the position specified by weighted sum of the input vectors (equivalently, at the position that is the appropriate linear combination of the input vectors);
- as many background characters as are needed to fit the origin, the two input vectors, and the output vector in the same picture. Extra background characters can be included if desired; the only constraint is that, if the background character is a visible character, then the entire output must be rectangular in shape and every character not representing a vector must be the background character. (If blank space is used as background characters, then these constraints don't need to be enforced.)
(In general, if we have one vector (v,w) with weight a and second vector (x,y) with weight b, their weighted sum is a(v,w)+b(x,y) = (av+bx,aw+by).)
In the previous example, the appropriate linear combination is 2*(3,1) + 3*(2,-1) = (12,-1). If we use "X" as the result character, then the output could look like
....2.........
.0............
...3.........X
or
................
...2............
0...............
..3.........X...
................
................
Usual code-golf scoring: the shortest answer, in bytes, wins.
Example input and output
If blank space is used, the above input would look like
2
0
3
and the output would look like
2
0
3 X
Leading/trailing whitespace characters/lines are irrelevant; if they're invisible to the reader, it's fine. (That being said, for the rest of the examples I'll go back to using "." for the background character, to make it easier to read.)
If both vectors have weight 1, then the result will look like a parallelogram: the input
.1.
...
1.0
leads to the output
X.1.
....
.1.0
Note this parallelogram can be degenerate if the input vectors are collinear: the input
0.1..1
leads to the output
0.1..1.X
It is possible for the result vector to equal one of the input vectors or the origin; in this case, it simply overwrites the input character. For example, the input
..2.0.1...
yields the output
..X.0.1...
(where in input and/or output, the leading and trailing periods could be deleted). The input
.....3
......
...0..
......
......
2.....
yields the output
.....3
......
...X..
......
......
2.....
Finally, the input
90
.8
yields the output
........90
.........8
..........
..........
..........
..........
..........
..........
X.........
1Welcome to PPCG! Nice first challenge. – AdmBorkBork – 2016-07-29T18:29:15.193
@TimmyD Thank you for the welcome and encouragement :) – Greg Martin – 2016-07-29T18:30:12.153
1
Finally, since I'm sure others will bring it up, this is flirtingly close to a chameleon challenge since a sizable chunk of code is going to be simply parsing the input, when that's not really the main thrust of the challenge.
– AdmBorkBork – 2016-07-29T18:41:02.713is there any limit on the number of rows/columns in the input or correct output? – Sparr – 2016-07-29T19:13:59.870
@TimmyD I've added the general formula for the weighted sum, and also clarified that either input format is fine. I agree that this is close to a chameleon challenge (though I was hoping that some language(s) might have the capability of "walking" directly on the board to solve the problem); however the feedback on Sandbox was modestly more positive than negative, so I decided to go with it. – Greg Martin – 2016-07-29T20:38:43.023
Regarding max input size: I don't particularly want to have a hard limit, but I also don't want solvers to worry about overflowing string/array/number sizes. What's the best way to proceed? – Greg Martin – 2016-07-29T20:39:38.450
@LuisMendo quite right!—fixed – Greg Martin – 2016-07-29T23:45:45.640