73
\$\begingroup\$

The brethren from the order of St Golfus the Concise have a tradition of reciting a short prayer whenever they see someone make the sign of the cross. Due to the high level of sin measured among tourists recently, they have installed CCTV in the monastery, and they've hired you to help them keep old tradition alive in the age of AI.

Your task is to analyse output from the friars' finger-tracking software and tell how many prayers are due. The input is a matrix containing integers between 0 and 4. 1,2,3,4 represent the positions of fingers at consecutive moments in time. 0 represents non-fingers.

The One True WayTM to cross oneself is:

.1.
3.4
.2.

("." matches any digit). However, due to uncertainty about the rotation of the camera and the presence of pious Eastern Orthodox brethren in the crowd (whose One True WayTM is in the opposite sideways direction), you should count all rotations and reflections too:

.4. .2. .3. .1. .3. .2. .4.
1.2 4.3 2.1 4.3 1.2 3.4 2.1
.3. .1. .4. .2. .4. .1. .3.

One digit may be part of multiple crosses. Help the friars determine the number of times their AI should .pray() by counting how many of the above 3x3 submatrices are present. Write a program or a function. Take input in any reasonable convenient form.

Testament cases:

// in
[[0,4,2,0],
 [1,3,2,4],
 [2,3,1,0]]

// out
2

// in
[[4,3,3,2,4,4,1,3,2,2],
 [0,3,0,2,1,1,2,3,2,3],
 [0,3,1,3,2,4,3,3,1,1],
 [4,3,2,3,2,4,1,4,2,3],
 [0,4,2,3,4,0,2,3,2,4],
 [2,1,0,0,2,0,0,1,2,4],
 [4,0,3,1,3,2,0,3,2,3],
 [1,4,3,3,1,4,0,1,4,4],
 [0,2,4,3,4,3,1,3,0,4],
 [3,0,1,0,4,0,3,3,3,3]]

// out
3

// in
[[3,2,3,1,0,3,4,2,1,1,1,1,4,0,1,3,1,1,2,1,1,3,0,1,0,1,1,0,0,1,0,3,4,0,1,1,2,3,1,2,4,1,0,2,3,0,2,4,3,2],
 [2,4,1,1,0,3,0,2,4,2,3,2,1,3,0,2,3,2,4,4,4,3,2,1,1,3,2,1,2,3,2,4,0,3,1,4,4,1,1,0,1,1,0,2,2,3,1,2,0,2],
 [3,4,0,0,4,4,0,3,4,4,1,3,2,1,3,2,3,2,2,0,4,0,1,2,3,0,4,3,2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,3,2,0,3,0,3,0,1,1,2,1,3,0,4,4,2,2,2,1,3,4,1,1,1],
 [3,0,1,4,2,0,0,3,1,1,1,4,4,0,2,2,0,4,0,3,1,0,2,2,4,4,4,0,4,4,4,4,4,4,3,0,4,4,4,1,2,4,4,3,0,0,4,0,4,2],
 [2,0,1,2,1,1,3,0,3,1,0,4,3,1,2,1,1,3,0,1,2,4,2,1,2,3,4,2,4,4,2,2,3,4,0,0,1,0,0,4,1,3,3,4,1,2,1,3,3,2],
 [4,0,2,0,3,1,2,1,1,1,1,2,3,0,3,1,0,4,3,0,0,0,2,0,1,4,0,2,1,3,4,2,2,4,2,3,1,2,0,2,0,2,4,0,1,2,3,4,1,3],
 [3,0,2,4,2,0,3,4,3,2,3,4,2,0,4,1,0,4,3,3,1,0,2,2,2,1,3,3,1,1,0,3,3,0,3,2,1,1,0,1,2,2,0,4,4,2,0,1,3,1],
 [0,4,4,4,0,3,0,3,0,2,2,0,1,2,3,3,4,3,0,4,1,2,3,3,0,2,2,3,0,0,0,2,4,2,3,4,2,3,4,0,2,0,1,1,3,4,2,2,4,4],
 [2,1,2,3,4,3,1,2,0,0,0,0,0,0,3,4,3,3,1,2,2,1,3,4,1,2,4,0,1,4,1,0,0,0,2,1,1,1,3,0,0,3,1,1,4,2,1,3,4,1],
 [1,0,3,0,2,1,4,2,3,3,1,1,3,4,4,0,1,2,1,3,0,3,1,1,3,0,2,4,4,4,2,3,1,4,3,4,0,1,4,1,1,1,4,0,0,2,3,4,0,4]]

// out
8

"Blessed be the shortest of shortests, for it winneth the kingdom of upvotes." -Book of St Golfus 13:37

"Thou shalt not use loopholes, for they are the wicked works of Lucifer." -Letter to the Meta 13:666

Slight visualization of people making the cross

var html = $('pre').html();
html = html.split('0').join('<span class="zero">0</span>')
.split('1').join('<span class="one">1</span>')
.split('2').join('<span class="two">2</span>')
.split('3').join('<span class="three">3</span>')
.split('4').join('<span class="four">4</span>')
$('pre').html(html);
var count =['zero','one','two','three','four'];
var counter = 0;
var $all = $('span');
window.setInterval(function() {
  var clazz = count[counter];
  counter++;
  if(counter == count.length) {
     counter = 0;
  }
  $all.css({'color':'white'});
  $('span.'+clazz).css({'color':''});
  
},1000) 
.zero {
    color:blue;
}
.one {
    color:green;
}
.two {
    color:gray;
}
.three {
  color:orange;
}
.four {
  color: red;
}
span {
  transition:1s ease-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre>
  // in
[[0,4,2,0],
 [1,3,2,4],
 [2,3,1,0]]

// out
2

// in
[[4,3,3,2,4,4,1,3,2,2],
 [0,3,0,2,1,1,2,3,2,3],
 [0,3,1,3,2,4,3,3,1,1],
 [4,3,2,3,2,4,1,4,2,3],
 [0,4,2,3,4,0,2,3,2,4],
 [2,1,0,0,2,0,0,1,2,4],
 [4,0,3,1,3,2,0,3,2,3],
 [1,4,3,3,1,4,0,1,4,4],
 [0,2,4,3,4,3,1,3,0,4],
 [3,0,1,0,4,0,3,3,3,3]]

// out
3

// in
[[3,2,3,1,0,3,4,2,1,1,1,1,4,0,1,3,1,1,2,1,1,3,0,1,0,1,1,0,0,1,0,3,4,0,1,1,2,3,1,2,4,1,0,2,3,0,2,4,3,2],
 [2,4,1,1,0,3,0,2,4,2,3,2,1,3,0,2,3,2,4,4,4,3,2,1,1,3,2,1,2,3,2,4,0,3,1,4,4,1,1,0,1,1,0,2,2,3,1,2,0,2],
 [3,4,0,0,4,4,0,3,4,4,1,3,2,1,3,2,3,2,2,0,4,0,1,2,3,0,4,3,2,2,2,0,3,3,4,4,2,2,1,4,4,1,3,1,1,2,0,1,1,0],
 [1,4,2,2,2,1,3,4,1,1,2,1,4,0,3,2,2,4,1,3,3,0,4,1,1,0,0,1,2,2,1,3,4,0,4,1,0,1,1,0,2,1,3,1,4,4,0,4,3,2],
 [4,4,2,0,4,4,1,1,2,2,3,3,2,3,0,3,2,1,0,3,3,4,2,2,2,1,1,4,3,2,1,1,4,3,4,2,4,0,1,0,2,4,2,2,0,3,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4,1,2,1,3,3,2],
 [4,0,2,0,3,1,2,1,1,1,1,2,3,0,3,1,0,4,3,0,0,0,2,0,1,4,0,2,1,3,4,2,2,4,2,3,1,2,0,2,0,2,4,0,1,2,3,4,1,3],
 [3,0,2,4,2,0,3,4,3,2,3,4,2,0,4,1,0,4,3,3,1,0,2,2,2,1,3,3,1,1,0,3,3,0,3,2,1,1,0,1,2,2,0,4,4,2,0,1,3,1],
 [0,4,4,4,0,3,0,3,0,2,2,0,1,2,3,3,4,3,0,4,1,2,3,3,0,2,2,3,0,0,0,2,4,2,3,4,2,3,4,0,2,0,1,1,3,4,2,2,4,4],
 [2,1,2,3,4,3,1,2,0,0,0,0,0,0,3,4,3,3,1,2,2,1,3,4,1,2,4,0,1,4,1,0,0,0,2,1,1,1,3,0,0,3,1,1,4,2,1,3,4,1],
 [1,0,3,0,2,1,4,2,3,3,1,1,3,4,4,0,1,2,1,3,0,3,1,1,3,0,2,4,4,4,2,3,1,4,3,4,0,1,4,1,1,1,4,0,0,2,3,4,0,4]]

</pre>


Thanks @Tschallacka for the visualization.

\$\endgroup\$
  • 57
    \$\begingroup\$ +1 for the verses at the end, they gave me a good laugh :D \$\endgroup\$ – HyperNeutrino Feb 4 '18 at 3:42
  • 6
    \$\begingroup\$ Can one digit be part of multiple crosses? \$\endgroup\$ – Martin Ender Feb 4 '18 at 8:25
  • 9
    \$\begingroup\$ Oh sweet Jesus, you did make a Friar simulator. \$\endgroup\$ – Magic Octopus Urn Feb 5 '18 at 12:07
  • 1
    \$\begingroup\$ Why is the first “T” linked? \$\endgroup\$ – JakeGould Feb 5 '18 at 16:07
  • 4
    \$\begingroup\$ @JakeGould So as to link this question to the question the "T" leads to. \$\endgroup\$ – Erik the Outgolfer Feb 5 '18 at 18:12

13 Answers 13

19
\$\begingroup\$

Grime, 20 bytes

n`.\1./\3.\4/.\2.voO

Try it online!

A very literal implementation of the spec:

  • n` make Grime count the number of subrectangles of the input that yield a match.
  • .\1./\3.\4/.\2. defines the 3x3 square:

    .1.
    3.4
    .2.
    

    Where . can be any character.

  • oO is an orientation modifier which allows this rectangle to appear in any rotation or reflection. The v is used to lower os precedence so that we don't need parentheses around the square.
\$\endgroup\$
  • 7
    \$\begingroup\$ Correct for the first two test, segfault for the last. I guess this should be ok as long as the program is correct in principle and is only limited by resources. \$\endgroup\$ – ngn Feb 4 '18 at 9:49
  • \$\begingroup\$ FYI: I just tested the last test case and it ran fine for me. \$\endgroup\$ – 3D1T0R May 30 '18 at 3:21
16
\$\begingroup\$

Snails, 17 16 bytes

Ao
\1.=\2o=\3b\4

Try it online!

Explanation

A makes Snails count the number of possible matching paths in the entire input. o sets the starting direction to any orthogonal direction (instead of just eastward). The pattern itself is on the second line:

\1    Match a 1.
.     Match an arbitrary character (the one in the centre).
=\2   Check that the next character ahead is a 2, but don't move there.
o     Turn to any orthogonal direction. In practice, this will only be one
      of the two directions from the centre we haven't checked yet.
=\3   Check that the next character ahead is a 3.
b     Turn around 180 degrees.
\4    Match a 4 in that direction.
\$\endgroup\$
7
\$\begingroup\$

Haskell, 108 102 93 bytes

f((_:t:x):w@((l:q@(_:r:_)):(_:b:z):_))=sum$f((t:x):q:[b:z]):f w:[1|(l*r-1)*(t*b-1)==11]
f _=0

Try it online!

No regex. Pattern match

.t.
l.r
.b.

in the top left corner of the matrix , take a 1 if (l*r-1)*(t*b-1)==11 and recursively go to the right (drop .l.) and down (drop first row). If the pattern cannot be matched (at the right or bottom border) take a 0. Sum all results.

Edit: -9 bytes thanks to @xnor.

\$\endgroup\$
  • \$\begingroup\$ I was going to suggest 2^(l*r)+2^(t*b)==4100 to check the numbers, but it seems both our solutions are tricked by 2,6 in place of 3,4. \$\endgroup\$ – xnor Feb 4 '18 at 19:57
  • \$\begingroup\$ @xnor but the numbers are restricted to 0..4 \$\endgroup\$ – Οurous Feb 4 '18 at 20:01
  • 3
    \$\begingroup\$ @Οurous Thanks, I missed that. Then arithmetic expressions can be optimized even more, like (l*r-1)*(t*b-1)==11. \$\endgroup\$ – xnor Feb 4 '18 at 20:04
7
\$\begingroup\$

Perl, 70 bytes

Includes +2 for 0p

Give input matrix as a block of digits without spaces on STDIN:

perl -0pe '$_=map{/.$/m+y/1-4/3421/;/(?=1.{@{-}}(3.4|4.3).{@{-}}2)/sg}($_)x4'
0420
1324
2310
^D

Rotates the cross by rotating the digits

\$\endgroup\$
6
\$\begingroup\$

Retina, 92 83 bytes

L$v`(?<=((.))*).(.)..*¶(?<-1>.)*(.).(.).*¶(?<-2>.)*.(.)
$6$3$4$5
/../_O`.
1234|3412

Try it online! Explanation:

L$v`(?<=((.))*).(.)..*¶(?<-1>.)*(.).(.).*¶(?<-2>.)*.(.)
$6$3$4$5

Look for all overlapping 3x3 squares. A lookbehind captures the indent twice so that it can be balanced on the second and third lines. The input is assumed to be rectangular so we don't have to check that the groups balance. The bottom/top middle cells and the left/right middle cells are then captured.

/../_O`.

Sort each pair of cells into order.

1234|3412

Count the remaining valid patterns.

\$\endgroup\$
5
\$\begingroup\$

Jelly, 26 bytes

Z3Ƥṡ€3ẎµFḊm2ṙ-s2Ṣ€ṢFµ€ċ4R¤

Try it online!

Explanation

Z3Ƥṡ€3ẎµFḊm2ṙ-s2Ṣ€ṢFµ€ċ4R¤  Main Link
 3Ƥ                         For each 3 rows
Z                           Zip the rows
   ṡ€3                      Get all arrangements of 3 consecutive columns from these rows. At this step, we have all 3x3 contiguous submatrices
      Ẏ                     Unwrap by one layer; previously grouped by rows, now ungrouped
       µ           µ€       For each 3x3 submatrix
        F                   Flatten it
         Ḋ                  Get rid of the first element
          m2                Take every second element (this only keeps the edges)
            ṙ-              Rotate right 1 element
              s2            Group into pairs; now the top and bottom are grouped and the left and right are grouped
                Ṣ€          Sort each pair
                  Ṣ         Sort the pairs
                   F        Flatten
                      ċ4R¤  Count the number of occurrences of range(4); i.e. [1,2,3,4]

-3 bytes thanks to Jonathan Allan (2) and Mr. Xcoder (3) (merged)

\$\endgroup\$
  • \$\begingroup\$ @ngn lol the dot is meant to be under the D. My bad, fixed. \$\endgroup\$ – HyperNeutrino Feb 4 '18 at 9:47
  • \$\begingroup\$ Save 2 bytes using ...Fµ€ċ4R¤ rather than ...Ḍµ€ċ1234 (also note that 1234 could have been replaced by ⁽¡ḋ to save one) \$\endgroup\$ – Jonathan Allan Feb 4 '18 at 17:26
  • \$\begingroup\$ 26 bytes by using Z3Ƥ instead of ṡ3Z€ and ⁼J$µ€S instead of Ḍµ€ċ1234. \$\endgroup\$ – Mr. Xcoder Feb 4 '18 at 17:54
  • \$\begingroup\$ @JonathanAllan oh cool, thanks \$\endgroup\$ – HyperNeutrino Feb 4 '18 at 18:52
5
\$\begingroup\$

Java 8, 135 133 131 bytes

m->{int r=0,i=0,j;for(;++i<m.length-1;)for(j=1;j<m[i].length-1;)if(~(m[i-1][j]*m[i+1][j])*~(m[i][j-1]*m[i][++j])==39)r++;return r;}

-2 bytes thanks to @tehtmi for a shorter formula: (l*r-1)*(t*b-1)==11 to ~(l*r)*~(t*b)==39

Explanation:

Try it online.

m->{                     // Method with integer-matrix parameter and integer return-type
  int r=0,               //  Result-integer, starting at 0
      i=0,j;             //  Index integers
  for(;++i<m.length-1;)  //  Loop over the rows, skipping the first and last
    for(j=1;j<m[i].length-1;)
                         //   Inner loop over the columns, skipping the first and last
      if(~(m[i-1][j]*m[i+1][j])*~(m[i][j-1]*m[i][++j])==39)
                         //    If (-(bottom*top)-1) * (-(left*right)-1) is exactly 39:
        r++;             //     Increase the result-counter by 1
  return r;}             //  Return the result-counter
\$\endgroup\$
  • 1
    \$\begingroup\$ Shorter formula: ~(l*r)*~(t*b)==39 (Haskell doesn't have 1-byte ~ apparently.) \$\endgroup\$ – tehtmi Feb 6 '18 at 3:23
3
\$\begingroup\$

Husk, 23 bytes

#ö§&¦ḣ4SδΛ≈↔Ċ2tΣṁoX3TX3

Try it online!

Explanation

#ö§&¦ḣ4SδΛ≈↔Ċ2tΣṁoX3TX3  Implicit input, a list of lists of integers.
                     X3  Length-3 slices.
                ṁ        Map over them and concatenate:
                    T     Transpose,
                 oX3      then get length-3 slices.
                         Now we have a list of 3x3-arrays of the (transposed) input.
#ö                       Return number of 3x3-arrays that satisfy this:
                          Take m = [[0,3,0],[2,4,1],[1,4,0]] as an example.
               Σ          Concatenate: [0,3,0,2,4,1,1,4,0]
              t           Tail: [3,0,2,4,1,1,4,0]
            Ċ2            Take every second element: c = [3,2,1,4]
  §&                      c satisfies both of the following:
    ¦                     1) It contains every element of
     ḣ4                      the range [1,2,3,4].
       Sδ                 2) If you zip c
           ↔                 with its reverse,
         Λ                   then each corresponding pair
          ≈                  has absolute difference at most 1.
                         Implicitly print the result.
\$\endgroup\$
3
\$\begingroup\$

Dyalog APL, 30 29 28 27 26 bytes (SBSC)

+/∘,{12 2⍷×∘⌽⍨⊢/4 2⍴⍵}⌺3 3

Try it online!

\$\endgroup\$
  • \$\begingroup\$ very good! ⌽∘⊖×⊢ could be shorter by 2 bytes, can you guess how? \$\endgroup\$ – ngn Feb 6 '18 at 18:47
  • \$\begingroup\$ @ngn What I really want is a symbol like ø ;) \$\endgroup\$ – H.PWiz Feb 6 '18 at 19:41
  • \$\begingroup\$ @ngn I have ×∘⌽∘⊖⍨ \$\endgroup\$ – H.PWiz Feb 6 '18 at 20:48
  • \$\begingroup\$ this is shorter: (⌽×⊖) \$\endgroup\$ – ngn Feb 6 '18 at 20:52
  • \$\begingroup\$ Oh, that's clever, you moved the selfie (...⍨) to left of ⊢/ for another -1. I didn't think of that. In this situation you shouldn't credit me. \$\endgroup\$ – ngn Feb 7 '18 at 15:09
2
\$\begingroup\$

Jelly, 36 bytes

“ƒɦƈạ%ȤFE’b4s4
ṡ3µZṡ3F€µ€ẎḊm2$$€’f¢L

Try it online!

37 bytes

For some reason I can't move the Ḋm2$$ to the top link.

ṙ1s2I€FPA,P⁼1,24
ṡ3µZṡ3F€µ€ẎḊm2$$€Ç€S

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Clean, 255 ... 162 bytes

It isn't beneficial that often to use pattern filters in comprehensions, but in this case, it is.

import StdEnv,StdLib
? =reverse
@ =transpose
t=tails
$m=sum[1\\f<-[id,@,?,?o@,@o?,@o?o@,?o@o?o@,?o@o?],[a,b,c:_]<-t(f m),[_,1:_]<-t a&[3,_,4:_]<-t b&[_,2:_]<-t c]

Try it online!

Defines the function $, taking [[Int]] and returning Int.

It first generates all symmetries of the matrix m (transforming via f), takes the tails of which have three or more rows, and synchronously checks how many of the first three sets of columns from the tails of each row match the pattern of the cross.

This is equivalent to counting the number of ordered tails-of-tails matching the pattern [[_,1,_:_],[3,_,4:_],[_,2,_:_]:_] - which is in turn logically the same as checking if, for each cell in the matrix, that cell is the upper-left corner of any rotation of the cross.

\$\endgroup\$
2
\$\begingroup\$

Python 3, 120 118 bytes

lambda x:sum({x[i+1][j]*x[i+1][j+2],x[i][j+1]*x[i+2][j+1]}=={2,12}for i in range(len(x)-2)for j in range(len(x[0])-2))

Try it online!

Uses the fact that the product of opposite pairs of numbers in the cross must be 2 and 12 respectively, and compares with a set to cover all the different orientations. Takes input as a 2D array of integers.

\$\endgroup\$
  • 1
    \$\begingroup\$ you don't have to count f= in the score \$\endgroup\$ – ngn Feb 6 '18 at 7:22
2
\$\begingroup\$

Japt -x, 39 38 33 bytes

ã3 Ëmã3 y x@[XXy]®g1 ë2 ×Ãn e[2C]

Try it online!

-1 byte thanks to @Shaggy.

-5 bytes thanks to @ETHproductions by refactoring the array.

Unpacked & How it works

Uã3 mD{Dmã3 y xX{[XXy]mZ{Zg1 ë2 r*1} n e[2C]

       Input: 2D Array of numbers
Uã3    Generate an array of length 3 segments of U
mD{    Map... (D = 2D array of numbers having 3 rows)
Dmã3     Map over each row of D to generate an array of length 3 segments
y        Transpose; make an array of 3x3 subsections
xX{      Map and sum... (x = 3x3 2D array of numbers)
[XXy]      Array of X and X transposed
mZ{        Map...
Zg1 ë2 r*1   Take row 1, take 0th and 2nd elements, reduce with *
}
n          Sort the array
e[2C]      Equals [2,12] element-wise?
           Implicit cast from boolean to number

       Result: 1D array of counts
-x     Sum of the result array

There should still be a better way to test for the cross...

\$\endgroup\$
  • \$\begingroup\$ You can save a byte by replacing the equality check at the end with e[2C]. \$\endgroup\$ – Shaggy May 31 '18 at 15:09
  • \$\begingroup\$ Can you get rid of the 2 in ë2? \$\endgroup\$ – Oliver Jun 1 '18 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.