11
Credit
My thanks to Rand Al'Thor's letter-based question for the inspiration for this code-golf challenge.
Background
The nature of this challenge is based off the algorithm mentioned by Rand in his "A Triangle Formed of Three Letters":
- Start with a sequence of 10 letters each of which is X, Y, or Z.
- Under each row, construct the next row as follows. If two adjacent letters are the same, write the same letter beneath them; if they're different, write the third letter beneath them.
You would then repeat the previous step until you have one letter in your tenth row.
Challenge
We're going to put a mathematical spin on the above algorithm:
- Let's start with a sequences of 10 digits, each separated by a space, and each of which is either 1, 2 or 3.
- Under each row, construct the next row as follows. If two adjacent digits are the same, write the same digit beneath them; if they're different, write the third digit beneath them.
- Repeat the previous step until you have one final number.
So, following this algorithm, if starting with the row 1 2 3 3 1 3 1 3 1 2, for example, the following triangle is generated:
Input: 1 2 3 3 1 3 1 3 1 2
Output:
1 2 3 3 1 3 1 3 1 2
3 1 3 2 2 2 2 2 3
2 2 1 2 2 2 2 1
2 3 3 2 2 2 3
1 3 1 2 2 1
2 2 3 2 3
2 1 1 1
3 1 1
2 1
3
I'm also curious to know the sum of all the digits in the number triangle, so add all these digits, and put this total in an eleventh row, right-justified to the last digit in the first row. So, our number triangle will look something like the following (spaces in my example are represented below by the . character to show formatting.)
Input: 1 2 3 3 1 3 1 3 1 2
Output:
1.2.3.3.1.3.1.3.1.2
.3.1.3.2.2.2.2.2.3.
..2.2.1.2.2.2.2.1..
...2.3.3.2.2.2.3...
....1.3.1.2.2.1....
.....2.2.3.2.3.....
......2.1.1.1......
.......3.1.1.......
........2.1........
.........3.........
................109
Your challenge is to write code that can start with an inputted string/array/etc. of ten digits, as per my example, and then apply the algorithm to generate the ten rows that would create the number triangle, followed by an 11th row that would display the total of all digits with right-justification.
Testing
Testing of this string can be performed with a randomly generated string of ten digits of your choosing, or one generated from the snippet below...
c1=()=>('1331123221'+(Math.random()*(1<<24)|0).toString(4)).replace(/0/g, "").slice(-10).split("").join(" ");
$("#btn").click(function(){
$("#str").val(c1());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="str"><button id="btn">Get numbers</button>
<br>
Please use this snippet to generate a starting row for testing your code. Alternatively, you can also use a string of your choice, so long as it's ten digits (ranging from 1 to 3), separated by single spaces.
Rules
- Code-golf rules apply, so lowest number of bytes wins the challenge. In the event that there are two entries with the same low score, the winner will be awarded based on the number of up-votes.
- What we're basically after is 11 rows, 19 characters long... How you render your final output is entirely up to you: array, console, file output, STDOUT, etc., so please use whatever output method you like that will work to your advantage. The only rule in the output is that we have 11 rows with 19 characters in each row in a similar format to above...
- If it helps your code, use whatever separator for the digits... Just remember that legibility may be a contributing factor.
- No silly loopholes.
- Hard-coding of the input is not allowed. The purposes of this code is such that it can be used to produce different results each time with varying input. Hard-coding of
1 1 1 1 1 1 1 1 1 1for example, completely negates the whole point of the algorithm.
Look forward to seeing what you all can come up with!
Sorta related – James – 2016-09-26T00:24:36.843
@DJMcMayhem Not related, the triangle in your referenced question has differences used to populating the next row. There's nothing like that in this question. The next row is populated through two specifc rules (A: if the two parents are the same, the child is the same; and B: if the two parents are different, the child is the third from the set) – WallyWest – 2016-09-26T01:27:05.057
1Do I need a separator if my triangle is center-aligned (which is thus legible)? – JungHwan Min – 2016-09-26T03:44:50.800
@JHM... It sounds like in your case it may be optional... Let's take a look – WallyWest – 2016-09-26T03:52:14.147
1
It looks like this without the space (my answer has space, which takes 10 bytes).
– JungHwan Min – 2016-09-26T03:55:03.457So is it okay if I remove the spaces? – JungHwan Min – 2016-09-26T05:14:23.503
2Permission granted – WallyWest – 2016-09-26T05:21:26.637
1Note that for a string of 10 (or any number 1 greater than a power of 3) digits, the final digit is trivially calculated from the first and last digit in the string; the other digits make no difference. – Neil – 2016-09-26T13:42:47.457