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 1
for example, completely negates the whole point of the algorithm.
Look forward to seeing what you all can come up with!