21
Input
A non-empty binary matrix consisting of 3x3 sub-matrices put side by side.
Task
Your task is to identify valid dice patterns (as described below) among the 3x3 sub-matrices. Each valid pattern is worth the value of the corresponding dice. Invalid patterns are worth 0.
Output
The sum of the valid dice values.
Dice patterns
$$\begin{align} &1:\pmatrix{\color{gray}0,\color{gray}0,\color{gray}0\\\color{gray}0,1,\color{gray}0\\\color{gray}0,\color{gray}0,\color{gray}0} &&2:\pmatrix{1,\color{gray}0,\color{gray}0\\\color{gray}0,\color{gray}0,\color{gray}0\\\color{gray}0,\color{gray}0,1}\text{or}\pmatrix{\color{gray}0,\color{gray}0,1\\\color{gray}0,\color{gray}0,\color{gray}0\\1,\color{gray}0,\color{gray}0}\\ &3:\pmatrix{1,\color{gray}0,\color{gray}0\\\color{gray}0,1,\color{gray}0\\\color{gray}0,\color{gray}0,1}\text{or}\pmatrix{\color{gray}0,\color{gray}0,1\\\color{gray}0,1,\color{gray}0\\1,\color{gray}0,\color{gray}0} &&4:\pmatrix{1,\color{gray}0,1\\\color{gray}0,\color{gray}0,\color{gray}0\\1,\color{gray}0,1}\\ &5:\pmatrix{1,\color{gray}0,1\\\color{gray}0,1,\color{gray}0\\1,\color{gray}0,1} &&6:\pmatrix{1,\color{gray}0,1\\1,\color{gray}0,1\\1,\color{gray}0,1}\text{or}\pmatrix{1,1,1\\\color{gray}0,\color{gray}0,\color{gray}0\\1,1,1} \end{align}$$
Example
The expected output for the following matrix is 14 because it contains the dice 5, 6 and 3, followed by an invalid pattern (from left to right and from top to bottom).
$$\pmatrix{1,0,1,1,1,1\\ 0,1,0,0,0,0\\ 1,0,1,1,1,1\\ 1,0,0,0,0,0\\ 0,1,0,0,1,0\\ 0,0,1,0,1,0}$$
Rules
- Both the width and the height of the matrix are guaranteed to be multiples of 3.
- You must ignore sub-matrices that are not properly aligned on the grid (see the 3rd test case). More formally and assuming 0-indexing: the coordinates of the top-left cell of each sub-matrix to be considered are of the form \$(3x, 3y)\$.
- This is code-golf.
Test cases
// 0
[ [ 1,0,0 ],
[ 0,0,1 ],
[ 1,0,0 ] ]
// 2
[ [ 0,0,1 ],
[ 0,0,0 ],
[ 1,0,0 ] ]
// 0 (0 + 0)
[ [ 0,0,1,0,1,0 ],
[ 0,0,0,1,0,0 ],
[ 0,0,1,0,1,0 ] ]
// 9 (3 + 3 + 3)
[ [ 1,0,0,0,0,1,1,0,0 ],
[ 0,1,0,0,1,0,0,1,0 ],
[ 0,0,1,1,0,0,0,0,1 ] ]
// 6 (6 + 0)
[ [ 1,0,1 ],
[ 1,0,1 ],
[ 1,0,1 ],
[ 1,0,1 ],
[ 1,0,0 ],
[ 1,0,1 ] ]
// 14 (5 + 6 + 3 + 0)
[ [ 1,0,1,1,1,1 ],
[ 0,1,0,0,0,0 ],
[ 1,0,1,1,1,1 ],
[ 1,0,0,0,0,0 ],
[ 0,1,0,0,1,0 ],
[ 0,0,1,0,1,0 ] ]
// 16 (1 + 2 + 3 + 4 + 0 + 6)
[ [ 0,0,0,1,0,0,1,0,0 ],
[ 0,1,0,0,0,0,0,1,0 ],
[ 0,0,0,0,0,1,0,0,1 ],
[ 1,0,1,1,1,1,1,0,1 ],
[ 0,0,0,1,0,1,1,0,1 ],
[ 1,0,1,1,1,1,1,0,1 ] ]
I wonder if you could shorten this slightly by doing the same operation on the transpose of the matrix as well. That way you could remove all of the duplicate entries in your map that add 6 bytes each. Just need to add the transpose step in <18 bytes – Easton Bornemeier – 2018-08-03T16:07:50.563
188 bytes. – Jonathan Frech – 2018-08-03T16:30:37.410
Get rid of both instances of
//3
and use'0'+''.join...
to save two bytes :) – Jonathan Allan – 2018-08-03T16:44:55.750...combine that with enumerate to save two more: here
– Jonathan Allan – 2018-08-03T16:49:37.5432165 bytes. – Jonathan Frech – 2018-08-03T17:09:52.627
Possible 162 bytes.
– Jonathan Frech – 2018-08-03T17:13:35.840