Challenge:
Given a matrix input, determine the amount of diagonals and anti-diagonals with duplicated numbers.
So if we have a matrix like this:
[[aa,ab,ac,ad,ae,af],
[ba,bb,bc,bd,be,bf],
[ca,cb,cc,cd,ce,cf],
[da,db,dc,dd,de,df]]
All diagonals and anti-diagonals would be:
[[aa],[ab,ba],[ac,bb,ca],[ad,bc,cb,da],[ae,bd,cc,db],[af,be,cd,dc],[bf,ce,dd],[cf,de],[df],
[af],[ae,bf],[ad,be,cf],[ac,bd,ce,df],[ab,bc,cd,de],[aa,bb,cc,dd],[ba,cb,dc],[ca,db],[da]]
Example:
[[1,2,1,2,1,2],
[1,2,3,4,5,6],
[6,5,4,3,2,1],
[2,1,2,1,2,1]]
All diagonals and anti-diagonals would be:
[[1],[2,1],[1,2,6],[2,3,5,2],[1,4,4,1],[2,5,3,2],[6,2,1],[1,2],[1],
[2],[1,6],[2,5,1],[1,4,2,1],[2,3,3,2],[1,2,4,1],[1,5,2],[6,1],[2]]
Removing all diagonals and anti-diagonals only containing unique numbers:
[[2,3,5,2],[1,4,4,1],[2,5,3,2],[1,4,2,1],[2,3,3,2],[1,2,4,1]]
So the output is the amount of diagonals and anti-diagonals containing duplicated numbers:
6
Challenge rules:
- If the input matrix is empty, contains only 1 number, or contains only unique numbers across the entire matrix, the output is always
0
. - Input is guaranteed to only contain positive digits
[1,9]
(unless it's completely empty). - The matrix will always be rectangular (i.e. all the rows are the same length).
- I/O is flexible. Input can be taken as a list of lists of integers, or 2D array of integers, or a Matrix-object, as a string, etc. etc. You are also allowed to take one or both of the dimensions of the matrix as additional input if it would save bytes in your language of choice.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input: Output:
[[1,2,1,2,1,2], 6
[1,2,3,4,5,6],
[6,5,4,3,2,1],
[2,1,2,1,2,1]]
[[]] 0
[[1,2], 0
[3,4]]
[[1,1], 2
[1,1]]
[[9,9,9], 6
[9,9,9],
[9,9,9]]
[[7,7,7,7], 8
[7,7,7,7],
[7,7,7,7]]
[[1,1,1], 1
[2,3,4],
[2,5,1]]
[[1,8,4,2,9,4,4,4], 12
[5,1,2,7,7,4,2,3],
[1,4,5,2,4,2,3,8],
[8,5,4,2,3,4,1,5]]
[[1,2,3,4], 4
[5,6,6,7],
[8,6,6,9],
[8,7,6,5]]