Count the contiguous submatrices












12














Migrated from chat



Given two non-empty non-negative integer matrices A and B, answer the number of times A occurs as a contiguous, possibly overlapping, submatrix in B.



Examples/Rules



0. There may not be any submatrices



A:
[[3,1],
[1,4]]



B:
[[1,4],
[3,1]]



Answer:
0



1. Submatrices must be contiguous



A:
[[1,4],
[3,1]]



B:
[[3,1,4,0,5],
[6,3,1,0,4],
[5,6,3,0,1]]



Answer:
1 (marked in bold)



2. Submatrices may overlap



A:
[[1,4],
[3,1]]



B:
[[3,1,4,5],
[6,3,1,4],
[5,6,3,1]]



Answer:
2 (marked in bold and in italic respectively)



3. A (sub)matrix may be size 1-by-1 and up



A:
[[3]]



B:
[[3,1,4,5],
[6,3,1,4],
[5,6,3,1]]



Answer:
3 (marked in bold)



4. Matrices may be any shape



A:
[[3,1,3]]



[[3,1,3,1,3,1,3,1,3]]



Answer:
4 (two bold, two italic)










share|improve this question





























    12














    Migrated from chat



    Given two non-empty non-negative integer matrices A and B, answer the number of times A occurs as a contiguous, possibly overlapping, submatrix in B.



    Examples/Rules



    0. There may not be any submatrices



    A:
    [[3,1],
    [1,4]]



    B:
    [[1,4],
    [3,1]]



    Answer:
    0



    1. Submatrices must be contiguous



    A:
    [[1,4],
    [3,1]]



    B:
    [[3,1,4,0,5],
    [6,3,1,0,4],
    [5,6,3,0,1]]



    Answer:
    1 (marked in bold)



    2. Submatrices may overlap



    A:
    [[1,4],
    [3,1]]



    B:
    [[3,1,4,5],
    [6,3,1,4],
    [5,6,3,1]]



    Answer:
    2 (marked in bold and in italic respectively)



    3. A (sub)matrix may be size 1-by-1 and up



    A:
    [[3]]



    B:
    [[3,1,4,5],
    [6,3,1,4],
    [5,6,3,1]]



    Answer:
    3 (marked in bold)



    4. Matrices may be any shape



    A:
    [[3,1,3]]



    [[3,1,3,1,3,1,3,1,3]]



    Answer:
    4 (two bold, two italic)










    share|improve this question



























      12












      12








      12


      1





      Migrated from chat



      Given two non-empty non-negative integer matrices A and B, answer the number of times A occurs as a contiguous, possibly overlapping, submatrix in B.



      Examples/Rules



      0. There may not be any submatrices



      A:
      [[3,1],
      [1,4]]



      B:
      [[1,4],
      [3,1]]



      Answer:
      0



      1. Submatrices must be contiguous



      A:
      [[1,4],
      [3,1]]



      B:
      [[3,1,4,0,5],
      [6,3,1,0,4],
      [5,6,3,0,1]]



      Answer:
      1 (marked in bold)



      2. Submatrices may overlap



      A:
      [[1,4],
      [3,1]]



      B:
      [[3,1,4,5],
      [6,3,1,4],
      [5,6,3,1]]



      Answer:
      2 (marked in bold and in italic respectively)



      3. A (sub)matrix may be size 1-by-1 and up



      A:
      [[3]]



      B:
      [[3,1,4,5],
      [6,3,1,4],
      [5,6,3,1]]



      Answer:
      3 (marked in bold)



      4. Matrices may be any shape



      A:
      [[3,1,3]]



      [[3,1,3,1,3,1,3,1,3]]



      Answer:
      4 (two bold, two italic)










      share|improve this question















      Migrated from chat



      Given two non-empty non-negative integer matrices A and B, answer the number of times A occurs as a contiguous, possibly overlapping, submatrix in B.



      Examples/Rules



      0. There may not be any submatrices



      A:
      [[3,1],
      [1,4]]



      B:
      [[1,4],
      [3,1]]



      Answer:
      0



      1. Submatrices must be contiguous



      A:
      [[1,4],
      [3,1]]



      B:
      [[3,1,4,0,5],
      [6,3,1,0,4],
      [5,6,3,0,1]]



      Answer:
      1 (marked in bold)



      2. Submatrices may overlap



      A:
      [[1,4],
      [3,1]]



      B:
      [[3,1,4,5],
      [6,3,1,4],
      [5,6,3,1]]



      Answer:
      2 (marked in bold and in italic respectively)



      3. A (sub)matrix may be size 1-by-1 and up



      A:
      [[3]]



      B:
      [[3,1,4,5],
      [6,3,1,4],
      [5,6,3,1]]



      Answer:
      3 (marked in bold)



      4. Matrices may be any shape



      A:
      [[3,1,3]]



      [[3,1,3,1,3,1,3,1,3]]



      Answer:
      4 (two bold, two italic)







      code-golf array-manipulation matrix search






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 31 '18 at 14:59







      Adám

















      asked Dec 31 '18 at 11:50









      AdámAdám

      29k269192




      29k269192






















          13 Answers
          13






          active

          oldest

          votes


















          5















          Jelly, 7 bytes



          ZẆ$⁺€Ẏċ


          Try it online!



          How it works



          ZẆ$⁺€Ẏċ  Main link. Arguments: B, A

          $ Combine the two links to the left into a monadic chain.
          Z Zip; transpose the matrix.
          Ẇ Window; yield all contiguous subarrays of rows.
          ⁺ Duplicate the previous link chain.
          € Map it over the result of applying it to B.
          This generates all contiguous submatrices of B, grouped by the selected
          columns of B.
          Ẏ Tighten; dump all generated submatrices in a single array.
          ċ Count the occurrences of A.





          share|improve this answer































            5















            Brachylog (v2), 10 bytes



            {{ss}ᵈ}ᶜ


            Try it online!



            I like how clear and straightforward this program is in Brachylog; unfortunately, it's not that short byte-wise because the metapredicate syntax takes up three bytes and has to be used twice in this program.



            Explanation



            {{ss}ᵈ}ᶜ
            s Contiguous subset of rows
            s Contiguous subset of columns (i.e. transpose, subset rows, transpose)
            { }ᵈ The operation above transforms the first input to the second input
            { }ᶜ Count the number of ways in which this is possible





            share|improve this answer































              4















              MATL, 12 bytes



              ZyYC2MX:=XAs


              Inputs are A, then B.



              Try it online! Or verify all test cases.



              Explanation



              Consider inputs [1,4; 3 1], [3,1,4,5; 6,3,1,4; 5,6,3,1]. The stack is shown with the most recent element below.



              Zy    % Implicit input: A. Push size as a vector of two numbers
              % STACK: [2 2]
              YC % Implicit input: B. Arrange sliding blocks of specified size as columns,
              % in column-major order
              % STACK: [3 6 1 3 4 1;
              6 5 3 6 1 3;
              1 3 4 1 5 4;
              3 6 1 3 4 1]
              2M % Push input to second to last function again; that is, A
              % STACK: [3 6 1 3 4 1;
              6 5 3 6 1 3;
              1 3 4 1 5 4;
              3 6 1 3 4 1],
              [1 4;
              3 1]
              X: % Linearize to a column vector, in column-major order
              % STACK: [3 6 1 3 4 1;
              6 5 3 6 1 3;
              1 3 4 1 5 4;
              3 6 1 3 4 1],
              [1;
              3;
              4;
              1]
              = % Test for equality, element-wise with broadcast
              % STACK: [0 0 1 0 0 1
              0 0 1 0 0 1;
              0 0 1 0 0 1;
              0 0 1 0 0 1]
              XA % True for columns containing all true values
              % STACK: [0 0 1 0 0 1]
              s % Sum. Implicit display
              % STACK: 2





              share|improve this answer































                2















                05AB1E, 10 bytes



                øŒεøŒI.¢}O


                Try it online!



                øŒεøŒI.¢}O     Full program. Takes 2 matrices as input. First B, then A.
                øŒ For each column of B, take all its sublists.
                ε } And map a function through all those lists of sublists.
                øŒ Transpose the list and again generate all its sublists.
                This essentially computes all sub-matrices of B.
                I.¢ In the current collection of sub-matrices, count the occurrences of A.
                O At the end of the loop sum the results.





                share|improve this answer































                  2














                  Dyalog APL, 6 4 bytes



                  ≢∘⍸⍷


                  This is nearly a builtin (thanks H.PWiz and ngn).



                    ⍷       Binary matrix containing locations of left argument in right argument
                  ≢∘⍸ Size of the array of indices of 1s


                  Alternative non-builtin:



                  {+/,((*⍺)≡⊢)⌺(⍴⍺)*⍵}


                  Dyadic function that takes the big array on right and subarray on left.



                                    *⍵       exp(⍵), to make ⍵ positive.
                  ((*⍺)≡⊢)⌺(⍴⍺) Stencil;
                  all subarrays of ⍵ (plus some partial subarrays
                  containing 0, which we can ignore)
                  ⍴⍺ of same shape as ⍺
                  (*⍺)≡⊢ processed by checking whether they're equal to exp(⍺).
                  Result is a matrix of 0/1.
                  , Flatten
                  +/ Sum.


                  Try it here.






                  share|improve this answer























                  • You should checkout
                    – H.PWiz
                    Jan 2 at 3:57












                  • you can use compose () to shorten the train: +/∘∊⍷ or even ≢∘⍸⍷
                    – ngn
                    Jan 2 at 15:16



















                  1














                  JavaScript (ES6), 93 bytes



                  Takes input as (A)(B).





                  a=>b=>b.map((r,y)=>r.map((_,x)=>s+=!a.some((R,Y)=>R.some((v,X)=>v!=(b[y+Y]||0)[x+X]))),s=0)|s


                  Try it online!






                  share|improve this answer





























                    1















                    R, 95 bytes





                    function(A,B,x=dim(A),D=dim(B)-x){for(i in 0:D)for(j in 0:D[2])F=F+all(B[1:x+i,1:x[2]+j]==A);F}


                    Try it online!






                    share|improve this answer































                      1















                      Clean, 118 97 95 bytes



                      import StdEnv,Data.List
                      ?x=[transpose y\z<-tails x,y<-inits z]
                      $a b=sum[1\x<- ?b,y<- ?x|y==a]


                      Try it online!






                      share|improve this answer































                        1















                        Python 2, 101 bytes





                        lambda a,b:sum(a==[l[j:j+len(a[0])]for l in b[i:i+len(a)]]for i,L in e(b)for j,_ in e(L))
                        e=enumerate


                        Try it online!






                        share|improve this answer





























                          1















                          Charcoal, 36 27 bytes



                          IΣ⭆η⭆ι⁼θE✂ηκ⁺Lθκ¹✂νμ⁺L§θ⁰μ¹


                          Try it online! Much shorter now that Equals works for arrays again. Explanation:



                             η                        Input array B
                          ⭆ Mapped over rows and joined
                          ι Current row
                          ⭆ Mapped over columns and joined
                          θ Input array A
                          ⁼ Is equal to
                          η Input array B
                          ✂ Sliced
                          ¹ All elements from
                          κ Current row index to
                          L Length of
                          θ Input array A
                          ⁺ Plus
                          κ Current row index
                          E Mapped over rows
                          ν Current inner row
                          ✂ Sliced
                          ¹ All elements from
                          μ Current column index to
                          L Length of
                          θ Input array A
                          § Indexed by
                          ⁰ Literal 0
                          ⁺ Plus
                          μ Current column index
                          Σ Digital sum
                          I Cast to string
                          Implicitly printed





                          share|improve this answer































                            0















                            Python 2, 211 bytes





                            a,b=input()
                            l,w,L,W,c=len(a),len(a[0]),len(b),len(b[0]),0
                            for i in range(L):
                            for j in range(W):
                            if j<=W-w and i<=L-l:
                            if not sum([a[x][y]!=b[i+x][j+y]for x in range(l)for y in range(w)]):
                            c+=1
                            print c


                            Try it online!



                            Fairly straightforward. Step through the larger matrix, and check if the smaller matrix can fit.



                            The only even slightly tricky step is the list comprehension in the 6th line, which relies on Python's conventions for mixing Boolean and integer arithmetic.






                            share|improve this answer





























                              0















                              Groovy, 109 bytes



                              {a,b->(0..<b.size()).sum{i->(0..<b[i].size()).count{j->k=i-1
                              a.every{l=j;k++
                              it.every{(b[k]?:b)[l++]==it}}}}}


                              Try it online!






                              share|improve this answer































                                0















                                Scala, 151 bytes





                                (a,b)=>{(0 to b.size-a.size).map(i=>(0 to b(0).size-a(0).size).count(j=>{var k=i-1
                                a.forall(c=>{var l=j-1;k+=1
                                c.forall(d=>{l+=1
                                b(k)(l)==d})})})).sum}


                                Try it online!






                                share|improve this answer























                                  Your Answer





                                  StackExchange.ifUsing("editor", function () {
                                  return StackExchange.using("mathjaxEditing", function () {
                                  StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
                                  StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
                                  });
                                  });
                                  }, "mathjax-editing");

                                  StackExchange.ifUsing("editor", function () {
                                  StackExchange.using("externalEditor", function () {
                                  StackExchange.using("snippets", function () {
                                  StackExchange.snippets.init();
                                  });
                                  });
                                  }, "code-snippets");

                                  StackExchange.ready(function() {
                                  var channelOptions = {
                                  tags: "".split(" "),
                                  id: "200"
                                  };
                                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                                  StackExchange.using("externalEditor", function() {
                                  // Have to fire editor after snippets, if snippets enabled
                                  if (StackExchange.settings.snippets.snippetsEnabled) {
                                  StackExchange.using("snippets", function() {
                                  createEditor();
                                  });
                                  }
                                  else {
                                  createEditor();
                                  }
                                  });

                                  function createEditor() {
                                  StackExchange.prepareEditor({
                                  heartbeatType: 'answer',
                                  autoActivateHeartbeat: false,
                                  convertImagesToLinks: false,
                                  noModals: true,
                                  showLowRepImageUploadWarning: true,
                                  reputationToPostImages: null,
                                  bindNavPrevention: true,
                                  postfix: "",
                                  imageUploader: {
                                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                                  allowUrls: true
                                  },
                                  onDemand: true,
                                  discardSelector: ".discard-answer"
                                  ,immediatelyShowMarkdownHelp:true
                                  });


                                  }
                                  });














                                  draft saved

                                  draft discarded


















                                  StackExchange.ready(
                                  function () {
                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178173%2fcount-the-contiguous-submatrices%23new-answer', 'question_page');
                                  }
                                  );

                                  Post as a guest















                                  Required, but never shown

























                                  13 Answers
                                  13






                                  active

                                  oldest

                                  votes








                                  13 Answers
                                  13






                                  active

                                  oldest

                                  votes









                                  active

                                  oldest

                                  votes






                                  active

                                  oldest

                                  votes









                                  5















                                  Jelly, 7 bytes



                                  ZẆ$⁺€Ẏċ


                                  Try it online!



                                  How it works



                                  ZẆ$⁺€Ẏċ  Main link. Arguments: B, A

                                  $ Combine the two links to the left into a monadic chain.
                                  Z Zip; transpose the matrix.
                                  Ẇ Window; yield all contiguous subarrays of rows.
                                  ⁺ Duplicate the previous link chain.
                                  € Map it over the result of applying it to B.
                                  This generates all contiguous submatrices of B, grouped by the selected
                                  columns of B.
                                  Ẏ Tighten; dump all generated submatrices in a single array.
                                  ċ Count the occurrences of A.





                                  share|improve this answer




























                                    5















                                    Jelly, 7 bytes



                                    ZẆ$⁺€Ẏċ


                                    Try it online!



                                    How it works



                                    ZẆ$⁺€Ẏċ  Main link. Arguments: B, A

                                    $ Combine the two links to the left into a monadic chain.
                                    Z Zip; transpose the matrix.
                                    Ẇ Window; yield all contiguous subarrays of rows.
                                    ⁺ Duplicate the previous link chain.
                                    € Map it over the result of applying it to B.
                                    This generates all contiguous submatrices of B, grouped by the selected
                                    columns of B.
                                    Ẏ Tighten; dump all generated submatrices in a single array.
                                    ċ Count the occurrences of A.





                                    share|improve this answer


























                                      5












                                      5








                                      5







                                      Jelly, 7 bytes



                                      ZẆ$⁺€Ẏċ


                                      Try it online!



                                      How it works



                                      ZẆ$⁺€Ẏċ  Main link. Arguments: B, A

                                      $ Combine the two links to the left into a monadic chain.
                                      Z Zip; transpose the matrix.
                                      Ẇ Window; yield all contiguous subarrays of rows.
                                      ⁺ Duplicate the previous link chain.
                                      € Map it over the result of applying it to B.
                                      This generates all contiguous submatrices of B, grouped by the selected
                                      columns of B.
                                      Ẏ Tighten; dump all generated submatrices in a single array.
                                      ċ Count the occurrences of A.





                                      share|improve this answer















                                      Jelly, 7 bytes



                                      ZẆ$⁺€Ẏċ


                                      Try it online!



                                      How it works



                                      ZẆ$⁺€Ẏċ  Main link. Arguments: B, A

                                      $ Combine the two links to the left into a monadic chain.
                                      Z Zip; transpose the matrix.
                                      Ẇ Window; yield all contiguous subarrays of rows.
                                      ⁺ Duplicate the previous link chain.
                                      € Map it over the result of applying it to B.
                                      This generates all contiguous submatrices of B, grouped by the selected
                                      columns of B.
                                      Ẏ Tighten; dump all generated submatrices in a single array.
                                      ċ Count the occurrences of A.






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Dec 31 '18 at 15:15

























                                      answered Dec 31 '18 at 13:10









                                      DennisDennis

                                      187k32297736




                                      187k32297736























                                          5















                                          Brachylog (v2), 10 bytes



                                          {{ss}ᵈ}ᶜ


                                          Try it online!



                                          I like how clear and straightforward this program is in Brachylog; unfortunately, it's not that short byte-wise because the metapredicate syntax takes up three bytes and has to be used twice in this program.



                                          Explanation



                                          {{ss}ᵈ}ᶜ
                                          s Contiguous subset of rows
                                          s Contiguous subset of columns (i.e. transpose, subset rows, transpose)
                                          { }ᵈ The operation above transforms the first input to the second input
                                          { }ᶜ Count the number of ways in which this is possible





                                          share|improve this answer




























                                            5















                                            Brachylog (v2), 10 bytes



                                            {{ss}ᵈ}ᶜ


                                            Try it online!



                                            I like how clear and straightforward this program is in Brachylog; unfortunately, it's not that short byte-wise because the metapredicate syntax takes up three bytes and has to be used twice in this program.



                                            Explanation



                                            {{ss}ᵈ}ᶜ
                                            s Contiguous subset of rows
                                            s Contiguous subset of columns (i.e. transpose, subset rows, transpose)
                                            { }ᵈ The operation above transforms the first input to the second input
                                            { }ᶜ Count the number of ways in which this is possible





                                            share|improve this answer


























                                              5












                                              5








                                              5







                                              Brachylog (v2), 10 bytes



                                              {{ss}ᵈ}ᶜ


                                              Try it online!



                                              I like how clear and straightforward this program is in Brachylog; unfortunately, it's not that short byte-wise because the metapredicate syntax takes up three bytes and has to be used twice in this program.



                                              Explanation



                                              {{ss}ᵈ}ᶜ
                                              s Contiguous subset of rows
                                              s Contiguous subset of columns (i.e. transpose, subset rows, transpose)
                                              { }ᵈ The operation above transforms the first input to the second input
                                              { }ᶜ Count the number of ways in which this is possible





                                              share|improve this answer















                                              Brachylog (v2), 10 bytes



                                              {{ss}ᵈ}ᶜ


                                              Try it online!



                                              I like how clear and straightforward this program is in Brachylog; unfortunately, it's not that short byte-wise because the metapredicate syntax takes up three bytes and has to be used twice in this program.



                                              Explanation



                                              {{ss}ᵈ}ᶜ
                                              s Contiguous subset of rows
                                              s Contiguous subset of columns (i.e. transpose, subset rows, transpose)
                                              { }ᵈ The operation above transforms the first input to the second input
                                              { }ᶜ Count the number of ways in which this is possible






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              answered Dec 31 '18 at 16:27


























                                              community wiki





                                              ais523
























                                                  4















                                                  MATL, 12 bytes



                                                  ZyYC2MX:=XAs


                                                  Inputs are A, then B.



                                                  Try it online! Or verify all test cases.



                                                  Explanation



                                                  Consider inputs [1,4; 3 1], [3,1,4,5; 6,3,1,4; 5,6,3,1]. The stack is shown with the most recent element below.



                                                  Zy    % Implicit input: A. Push size as a vector of two numbers
                                                  % STACK: [2 2]
                                                  YC % Implicit input: B. Arrange sliding blocks of specified size as columns,
                                                  % in column-major order
                                                  % STACK: [3 6 1 3 4 1;
                                                  6 5 3 6 1 3;
                                                  1 3 4 1 5 4;
                                                  3 6 1 3 4 1]
                                                  2M % Push input to second to last function again; that is, A
                                                  % STACK: [3 6 1 3 4 1;
                                                  6 5 3 6 1 3;
                                                  1 3 4 1 5 4;
                                                  3 6 1 3 4 1],
                                                  [1 4;
                                                  3 1]
                                                  X: % Linearize to a column vector, in column-major order
                                                  % STACK: [3 6 1 3 4 1;
                                                  6 5 3 6 1 3;
                                                  1 3 4 1 5 4;
                                                  3 6 1 3 4 1],
                                                  [1;
                                                  3;
                                                  4;
                                                  1]
                                                  = % Test for equality, element-wise with broadcast
                                                  % STACK: [0 0 1 0 0 1
                                                  0 0 1 0 0 1;
                                                  0 0 1 0 0 1;
                                                  0 0 1 0 0 1]
                                                  XA % True for columns containing all true values
                                                  % STACK: [0 0 1 0 0 1]
                                                  s % Sum. Implicit display
                                                  % STACK: 2





                                                  share|improve this answer




























                                                    4















                                                    MATL, 12 bytes



                                                    ZyYC2MX:=XAs


                                                    Inputs are A, then B.



                                                    Try it online! Or verify all test cases.



                                                    Explanation



                                                    Consider inputs [1,4; 3 1], [3,1,4,5; 6,3,1,4; 5,6,3,1]. The stack is shown with the most recent element below.



                                                    Zy    % Implicit input: A. Push size as a vector of two numbers
                                                    % STACK: [2 2]
                                                    YC % Implicit input: B. Arrange sliding blocks of specified size as columns,
                                                    % in column-major order
                                                    % STACK: [3 6 1 3 4 1;
                                                    6 5 3 6 1 3;
                                                    1 3 4 1 5 4;
                                                    3 6 1 3 4 1]
                                                    2M % Push input to second to last function again; that is, A
                                                    % STACK: [3 6 1 3 4 1;
                                                    6 5 3 6 1 3;
                                                    1 3 4 1 5 4;
                                                    3 6 1 3 4 1],
                                                    [1 4;
                                                    3 1]
                                                    X: % Linearize to a column vector, in column-major order
                                                    % STACK: [3 6 1 3 4 1;
                                                    6 5 3 6 1 3;
                                                    1 3 4 1 5 4;
                                                    3 6 1 3 4 1],
                                                    [1;
                                                    3;
                                                    4;
                                                    1]
                                                    = % Test for equality, element-wise with broadcast
                                                    % STACK: [0 0 1 0 0 1
                                                    0 0 1 0 0 1;
                                                    0 0 1 0 0 1;
                                                    0 0 1 0 0 1]
                                                    XA % True for columns containing all true values
                                                    % STACK: [0 0 1 0 0 1]
                                                    s % Sum. Implicit display
                                                    % STACK: 2





                                                    share|improve this answer


























                                                      4












                                                      4








                                                      4







                                                      MATL, 12 bytes



                                                      ZyYC2MX:=XAs


                                                      Inputs are A, then B.



                                                      Try it online! Or verify all test cases.



                                                      Explanation



                                                      Consider inputs [1,4; 3 1], [3,1,4,5; 6,3,1,4; 5,6,3,1]. The stack is shown with the most recent element below.



                                                      Zy    % Implicit input: A. Push size as a vector of two numbers
                                                      % STACK: [2 2]
                                                      YC % Implicit input: B. Arrange sliding blocks of specified size as columns,
                                                      % in column-major order
                                                      % STACK: [3 6 1 3 4 1;
                                                      6 5 3 6 1 3;
                                                      1 3 4 1 5 4;
                                                      3 6 1 3 4 1]
                                                      2M % Push input to second to last function again; that is, A
                                                      % STACK: [3 6 1 3 4 1;
                                                      6 5 3 6 1 3;
                                                      1 3 4 1 5 4;
                                                      3 6 1 3 4 1],
                                                      [1 4;
                                                      3 1]
                                                      X: % Linearize to a column vector, in column-major order
                                                      % STACK: [3 6 1 3 4 1;
                                                      6 5 3 6 1 3;
                                                      1 3 4 1 5 4;
                                                      3 6 1 3 4 1],
                                                      [1;
                                                      3;
                                                      4;
                                                      1]
                                                      = % Test for equality, element-wise with broadcast
                                                      % STACK: [0 0 1 0 0 1
                                                      0 0 1 0 0 1;
                                                      0 0 1 0 0 1;
                                                      0 0 1 0 0 1]
                                                      XA % True for columns containing all true values
                                                      % STACK: [0 0 1 0 0 1]
                                                      s % Sum. Implicit display
                                                      % STACK: 2





                                                      share|improve this answer















                                                      MATL, 12 bytes



                                                      ZyYC2MX:=XAs


                                                      Inputs are A, then B.



                                                      Try it online! Or verify all test cases.



                                                      Explanation



                                                      Consider inputs [1,4; 3 1], [3,1,4,5; 6,3,1,4; 5,6,3,1]. The stack is shown with the most recent element below.



                                                      Zy    % Implicit input: A. Push size as a vector of two numbers
                                                      % STACK: [2 2]
                                                      YC % Implicit input: B. Arrange sliding blocks of specified size as columns,
                                                      % in column-major order
                                                      % STACK: [3 6 1 3 4 1;
                                                      6 5 3 6 1 3;
                                                      1 3 4 1 5 4;
                                                      3 6 1 3 4 1]
                                                      2M % Push input to second to last function again; that is, A
                                                      % STACK: [3 6 1 3 4 1;
                                                      6 5 3 6 1 3;
                                                      1 3 4 1 5 4;
                                                      3 6 1 3 4 1],
                                                      [1 4;
                                                      3 1]
                                                      X: % Linearize to a column vector, in column-major order
                                                      % STACK: [3 6 1 3 4 1;
                                                      6 5 3 6 1 3;
                                                      1 3 4 1 5 4;
                                                      3 6 1 3 4 1],
                                                      [1;
                                                      3;
                                                      4;
                                                      1]
                                                      = % Test for equality, element-wise with broadcast
                                                      % STACK: [0 0 1 0 0 1
                                                      0 0 1 0 0 1;
                                                      0 0 1 0 0 1;
                                                      0 0 1 0 0 1]
                                                      XA % True for columns containing all true values
                                                      % STACK: [0 0 1 0 0 1]
                                                      s % Sum. Implicit display
                                                      % STACK: 2






                                                      share|improve this answer














                                                      share|improve this answer



                                                      share|improve this answer








                                                      edited Dec 31 '18 at 12:45

























                                                      answered Dec 31 '18 at 12:39









                                                      Luis MendoLuis Mendo

                                                      74k886291




                                                      74k886291























                                                          2















                                                          05AB1E, 10 bytes



                                                          øŒεøŒI.¢}O


                                                          Try it online!



                                                          øŒεøŒI.¢}O     Full program. Takes 2 matrices as input. First B, then A.
                                                          øŒ For each column of B, take all its sublists.
                                                          ε } And map a function through all those lists of sublists.
                                                          øŒ Transpose the list and again generate all its sublists.
                                                          This essentially computes all sub-matrices of B.
                                                          I.¢ In the current collection of sub-matrices, count the occurrences of A.
                                                          O At the end of the loop sum the results.





                                                          share|improve this answer




























                                                            2















                                                            05AB1E, 10 bytes



                                                            øŒεøŒI.¢}O


                                                            Try it online!



                                                            øŒεøŒI.¢}O     Full program. Takes 2 matrices as input. First B, then A.
                                                            øŒ For each column of B, take all its sublists.
                                                            ε } And map a function through all those lists of sublists.
                                                            øŒ Transpose the list and again generate all its sublists.
                                                            This essentially computes all sub-matrices of B.
                                                            I.¢ In the current collection of sub-matrices, count the occurrences of A.
                                                            O At the end of the loop sum the results.





                                                            share|improve this answer


























                                                              2












                                                              2








                                                              2







                                                              05AB1E, 10 bytes



                                                              øŒεøŒI.¢}O


                                                              Try it online!



                                                              øŒεøŒI.¢}O     Full program. Takes 2 matrices as input. First B, then A.
                                                              øŒ For each column of B, take all its sublists.
                                                              ε } And map a function through all those lists of sublists.
                                                              øŒ Transpose the list and again generate all its sublists.
                                                              This essentially computes all sub-matrices of B.
                                                              I.¢ In the current collection of sub-matrices, count the occurrences of A.
                                                              O At the end of the loop sum the results.





                                                              share|improve this answer















                                                              05AB1E, 10 bytes



                                                              øŒεøŒI.¢}O


                                                              Try it online!



                                                              øŒεøŒI.¢}O     Full program. Takes 2 matrices as input. First B, then A.
                                                              øŒ For each column of B, take all its sublists.
                                                              ε } And map a function through all those lists of sublists.
                                                              øŒ Transpose the list and again generate all its sublists.
                                                              This essentially computes all sub-matrices of B.
                                                              I.¢ In the current collection of sub-matrices, count the occurrences of A.
                                                              O At the end of the loop sum the results.






                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited Dec 31 '18 at 14:22

























                                                              answered Dec 31 '18 at 14:17









                                                              Mr. XcoderMr. Xcoder

                                                              31.7k759198




                                                              31.7k759198























                                                                  2














                                                                  Dyalog APL, 6 4 bytes



                                                                  ≢∘⍸⍷


                                                                  This is nearly a builtin (thanks H.PWiz and ngn).



                                                                    ⍷       Binary matrix containing locations of left argument in right argument
                                                                  ≢∘⍸ Size of the array of indices of 1s


                                                                  Alternative non-builtin:



                                                                  {+/,((*⍺)≡⊢)⌺(⍴⍺)*⍵}


                                                                  Dyadic function that takes the big array on right and subarray on left.



                                                                                    *⍵       exp(⍵), to make ⍵ positive.
                                                                  ((*⍺)≡⊢)⌺(⍴⍺) Stencil;
                                                                  all subarrays of ⍵ (plus some partial subarrays
                                                                  containing 0, which we can ignore)
                                                                  ⍴⍺ of same shape as ⍺
                                                                  (*⍺)≡⊢ processed by checking whether they're equal to exp(⍺).
                                                                  Result is a matrix of 0/1.
                                                                  , Flatten
                                                                  +/ Sum.


                                                                  Try it here.






                                                                  share|improve this answer























                                                                  • You should checkout
                                                                    – H.PWiz
                                                                    Jan 2 at 3:57












                                                                  • you can use compose () to shorten the train: +/∘∊⍷ or even ≢∘⍸⍷
                                                                    – ngn
                                                                    Jan 2 at 15:16
















                                                                  2














                                                                  Dyalog APL, 6 4 bytes



                                                                  ≢∘⍸⍷


                                                                  This is nearly a builtin (thanks H.PWiz and ngn).



                                                                    ⍷       Binary matrix containing locations of left argument in right argument
                                                                  ≢∘⍸ Size of the array of indices of 1s


                                                                  Alternative non-builtin:



                                                                  {+/,((*⍺)≡⊢)⌺(⍴⍺)*⍵}


                                                                  Dyadic function that takes the big array on right and subarray on left.



                                                                                    *⍵       exp(⍵), to make ⍵ positive.
                                                                  ((*⍺)≡⊢)⌺(⍴⍺) Stencil;
                                                                  all subarrays of ⍵ (plus some partial subarrays
                                                                  containing 0, which we can ignore)
                                                                  ⍴⍺ of same shape as ⍺
                                                                  (*⍺)≡⊢ processed by checking whether they're equal to exp(⍺).
                                                                  Result is a matrix of 0/1.
                                                                  , Flatten
                                                                  +/ Sum.


                                                                  Try it here.






                                                                  share|improve this answer























                                                                  • You should checkout
                                                                    – H.PWiz
                                                                    Jan 2 at 3:57












                                                                  • you can use compose () to shorten the train: +/∘∊⍷ or even ≢∘⍸⍷
                                                                    – ngn
                                                                    Jan 2 at 15:16














                                                                  2












                                                                  2








                                                                  2






                                                                  Dyalog APL, 6 4 bytes



                                                                  ≢∘⍸⍷


                                                                  This is nearly a builtin (thanks H.PWiz and ngn).



                                                                    ⍷       Binary matrix containing locations of left argument in right argument
                                                                  ≢∘⍸ Size of the array of indices of 1s


                                                                  Alternative non-builtin:



                                                                  {+/,((*⍺)≡⊢)⌺(⍴⍺)*⍵}


                                                                  Dyadic function that takes the big array on right and subarray on left.



                                                                                    *⍵       exp(⍵), to make ⍵ positive.
                                                                  ((*⍺)≡⊢)⌺(⍴⍺) Stencil;
                                                                  all subarrays of ⍵ (plus some partial subarrays
                                                                  containing 0, which we can ignore)
                                                                  ⍴⍺ of same shape as ⍺
                                                                  (*⍺)≡⊢ processed by checking whether they're equal to exp(⍺).
                                                                  Result is a matrix of 0/1.
                                                                  , Flatten
                                                                  +/ Sum.


                                                                  Try it here.






                                                                  share|improve this answer














                                                                  Dyalog APL, 6 4 bytes



                                                                  ≢∘⍸⍷


                                                                  This is nearly a builtin (thanks H.PWiz and ngn).



                                                                    ⍷       Binary matrix containing locations of left argument in right argument
                                                                  ≢∘⍸ Size of the array of indices of 1s


                                                                  Alternative non-builtin:



                                                                  {+/,((*⍺)≡⊢)⌺(⍴⍺)*⍵}


                                                                  Dyadic function that takes the big array on right and subarray on left.



                                                                                    *⍵       exp(⍵), to make ⍵ positive.
                                                                  ((*⍺)≡⊢)⌺(⍴⍺) Stencil;
                                                                  all subarrays of ⍵ (plus some partial subarrays
                                                                  containing 0, which we can ignore)
                                                                  ⍴⍺ of same shape as ⍺
                                                                  (*⍺)≡⊢ processed by checking whether they're equal to exp(⍺).
                                                                  Result is a matrix of 0/1.
                                                                  , Flatten
                                                                  +/ Sum.


                                                                  Try it here.







                                                                  share|improve this answer














                                                                  share|improve this answer



                                                                  share|improve this answer








                                                                  edited Jan 3 at 3:36

























                                                                  answered Jan 2 at 2:20









                                                                  lirtosiastlirtosiast

                                                                  15.8k436107




                                                                  15.8k436107












                                                                  • You should checkout
                                                                    – H.PWiz
                                                                    Jan 2 at 3:57












                                                                  • you can use compose () to shorten the train: +/∘∊⍷ or even ≢∘⍸⍷
                                                                    – ngn
                                                                    Jan 2 at 15:16


















                                                                  • You should checkout
                                                                    – H.PWiz
                                                                    Jan 2 at 3:57












                                                                  • you can use compose () to shorten the train: +/∘∊⍷ or even ≢∘⍸⍷
                                                                    – ngn
                                                                    Jan 2 at 15:16
















                                                                  You should checkout
                                                                  – H.PWiz
                                                                  Jan 2 at 3:57






                                                                  You should checkout
                                                                  – H.PWiz
                                                                  Jan 2 at 3:57














                                                                  you can use compose () to shorten the train: +/∘∊⍷ or even ≢∘⍸⍷
                                                                  – ngn
                                                                  Jan 2 at 15:16




                                                                  you can use compose () to shorten the train: +/∘∊⍷ or even ≢∘⍸⍷
                                                                  – ngn
                                                                  Jan 2 at 15:16











                                                                  1














                                                                  JavaScript (ES6), 93 bytes



                                                                  Takes input as (A)(B).





                                                                  a=>b=>b.map((r,y)=>r.map((_,x)=>s+=!a.some((R,Y)=>R.some((v,X)=>v!=(b[y+Y]||0)[x+X]))),s=0)|s


                                                                  Try it online!






                                                                  share|improve this answer


























                                                                    1














                                                                    JavaScript (ES6), 93 bytes



                                                                    Takes input as (A)(B).





                                                                    a=>b=>b.map((r,y)=>r.map((_,x)=>s+=!a.some((R,Y)=>R.some((v,X)=>v!=(b[y+Y]||0)[x+X]))),s=0)|s


                                                                    Try it online!






                                                                    share|improve this answer
























                                                                      1












                                                                      1








                                                                      1






                                                                      JavaScript (ES6), 93 bytes



                                                                      Takes input as (A)(B).





                                                                      a=>b=>b.map((r,y)=>r.map((_,x)=>s+=!a.some((R,Y)=>R.some((v,X)=>v!=(b[y+Y]||0)[x+X]))),s=0)|s


                                                                      Try it online!






                                                                      share|improve this answer












                                                                      JavaScript (ES6), 93 bytes



                                                                      Takes input as (A)(B).





                                                                      a=>b=>b.map((r,y)=>r.map((_,x)=>s+=!a.some((R,Y)=>R.some((v,X)=>v!=(b[y+Y]||0)[x+X]))),s=0)|s


                                                                      Try it online!







                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Jan 1 at 12:26









                                                                      ArnauldArnauld

                                                                      72.9k689307




                                                                      72.9k689307























                                                                          1















                                                                          R, 95 bytes





                                                                          function(A,B,x=dim(A),D=dim(B)-x){for(i in 0:D)for(j in 0:D[2])F=F+all(B[1:x+i,1:x[2]+j]==A);F}


                                                                          Try it online!






                                                                          share|improve this answer




























                                                                            1















                                                                            R, 95 bytes





                                                                            function(A,B,x=dim(A),D=dim(B)-x){for(i in 0:D)for(j in 0:D[2])F=F+all(B[1:x+i,1:x[2]+j]==A);F}


                                                                            Try it online!






                                                                            share|improve this answer


























                                                                              1












                                                                              1








                                                                              1







                                                                              R, 95 bytes





                                                                              function(A,B,x=dim(A),D=dim(B)-x){for(i in 0:D)for(j in 0:D[2])F=F+all(B[1:x+i,1:x[2]+j]==A);F}


                                                                              Try it online!






                                                                              share|improve this answer















                                                                              R, 95 bytes





                                                                              function(A,B,x=dim(A),D=dim(B)-x){for(i in 0:D)for(j in 0:D[2])F=F+all(B[1:x+i,1:x[2]+j]==A);F}


                                                                              Try it online!







                                                                              share|improve this answer














                                                                              share|improve this answer



                                                                              share|improve this answer








                                                                              edited Jan 1 at 14:44

























                                                                              answered Jan 1 at 14:31









                                                                              digEmAlldigEmAll

                                                                              2,664413




                                                                              2,664413























                                                                                  1















                                                                                  Clean, 118 97 95 bytes



                                                                                  import StdEnv,Data.List
                                                                                  ?x=[transpose y\z<-tails x,y<-inits z]
                                                                                  $a b=sum[1\x<- ?b,y<- ?x|y==a]


                                                                                  Try it online!






                                                                                  share|improve this answer




























                                                                                    1















                                                                                    Clean, 118 97 95 bytes



                                                                                    import StdEnv,Data.List
                                                                                    ?x=[transpose y\z<-tails x,y<-inits z]
                                                                                    $a b=sum[1\x<- ?b,y<- ?x|y==a]


                                                                                    Try it online!






                                                                                    share|improve this answer


























                                                                                      1












                                                                                      1








                                                                                      1







                                                                                      Clean, 118 97 95 bytes



                                                                                      import StdEnv,Data.List
                                                                                      ?x=[transpose y\z<-tails x,y<-inits z]
                                                                                      $a b=sum[1\x<- ?b,y<- ?x|y==a]


                                                                                      Try it online!






                                                                                      share|improve this answer















                                                                                      Clean, 118 97 95 bytes



                                                                                      import StdEnv,Data.List
                                                                                      ?x=[transpose y\z<-tails x,y<-inits z]
                                                                                      $a b=sum[1\x<- ?b,y<- ?x|y==a]


                                                                                      Try it online!







                                                                                      share|improve this answer














                                                                                      share|improve this answer



                                                                                      share|improve this answer








                                                                                      edited Jan 2 at 1:34

























                                                                                      answered Dec 31 '18 at 23:45









                                                                                      ΟurousΟurous

                                                                                      6,54211033




                                                                                      6,54211033























                                                                                          1















                                                                                          Python 2, 101 bytes





                                                                                          lambda a,b:sum(a==[l[j:j+len(a[0])]for l in b[i:i+len(a)]]for i,L in e(b)for j,_ in e(L))
                                                                                          e=enumerate


                                                                                          Try it online!






                                                                                          share|improve this answer


























                                                                                            1















                                                                                            Python 2, 101 bytes





                                                                                            lambda a,b:sum(a==[l[j:j+len(a[0])]for l in b[i:i+len(a)]]for i,L in e(b)for j,_ in e(L))
                                                                                            e=enumerate


                                                                                            Try it online!






                                                                                            share|improve this answer
























                                                                                              1












                                                                                              1








                                                                                              1







                                                                                              Python 2, 101 bytes





                                                                                              lambda a,b:sum(a==[l[j:j+len(a[0])]for l in b[i:i+len(a)]]for i,L in e(b)for j,_ in e(L))
                                                                                              e=enumerate


                                                                                              Try it online!






                                                                                              share|improve this answer













                                                                                              Python 2, 101 bytes





                                                                                              lambda a,b:sum(a==[l[j:j+len(a[0])]for l in b[i:i+len(a)]]for i,L in e(b)for j,_ in e(L))
                                                                                              e=enumerate


                                                                                              Try it online!







                                                                                              share|improve this answer












                                                                                              share|improve this answer



                                                                                              share|improve this answer










                                                                                              answered Jan 2 at 14:48









                                                                                              TFeldTFeld

                                                                                              14.4k21240




                                                                                              14.4k21240























                                                                                                  1















                                                                                                  Charcoal, 36 27 bytes



                                                                                                  IΣ⭆η⭆ι⁼θE✂ηκ⁺Lθκ¹✂νμ⁺L§θ⁰μ¹


                                                                                                  Try it online! Much shorter now that Equals works for arrays again. Explanation:



                                                                                                     η                        Input array B
                                                                                                  ⭆ Mapped over rows and joined
                                                                                                  ι Current row
                                                                                                  ⭆ Mapped over columns and joined
                                                                                                  θ Input array A
                                                                                                  ⁼ Is equal to
                                                                                                  η Input array B
                                                                                                  ✂ Sliced
                                                                                                  ¹ All elements from
                                                                                                  κ Current row index to
                                                                                                  L Length of
                                                                                                  θ Input array A
                                                                                                  ⁺ Plus
                                                                                                  κ Current row index
                                                                                                  E Mapped over rows
                                                                                                  ν Current inner row
                                                                                                  ✂ Sliced
                                                                                                  ¹ All elements from
                                                                                                  μ Current column index to
                                                                                                  L Length of
                                                                                                  θ Input array A
                                                                                                  § Indexed by
                                                                                                  ⁰ Literal 0
                                                                                                  ⁺ Plus
                                                                                                  μ Current column index
                                                                                                  Σ Digital sum
                                                                                                  I Cast to string
                                                                                                  Implicitly printed





                                                                                                  share|improve this answer




























                                                                                                    1















                                                                                                    Charcoal, 36 27 bytes



                                                                                                    IΣ⭆η⭆ι⁼θE✂ηκ⁺Lθκ¹✂νμ⁺L§θ⁰μ¹


                                                                                                    Try it online! Much shorter now that Equals works for arrays again. Explanation:



                                                                                                       η                        Input array B
                                                                                                    ⭆ Mapped over rows and joined
                                                                                                    ι Current row
                                                                                                    ⭆ Mapped over columns and joined
                                                                                                    θ Input array A
                                                                                                    ⁼ Is equal to
                                                                                                    η Input array B
                                                                                                    ✂ Sliced
                                                                                                    ¹ All elements from
                                                                                                    κ Current row index to
                                                                                                    L Length of
                                                                                                    θ Input array A
                                                                                                    ⁺ Plus
                                                                                                    κ Current row index
                                                                                                    E Mapped over rows
                                                                                                    ν Current inner row
                                                                                                    ✂ Sliced
                                                                                                    ¹ All elements from
                                                                                                    μ Current column index to
                                                                                                    L Length of
                                                                                                    θ Input array A
                                                                                                    § Indexed by
                                                                                                    ⁰ Literal 0
                                                                                                    ⁺ Plus
                                                                                                    μ Current column index
                                                                                                    Σ Digital sum
                                                                                                    I Cast to string
                                                                                                    Implicitly printed





                                                                                                    share|improve this answer


























                                                                                                      1












                                                                                                      1








                                                                                                      1







                                                                                                      Charcoal, 36 27 bytes



                                                                                                      IΣ⭆η⭆ι⁼θE✂ηκ⁺Lθκ¹✂νμ⁺L§θ⁰μ¹


                                                                                                      Try it online! Much shorter now that Equals works for arrays again. Explanation:



                                                                                                         η                        Input array B
                                                                                                      ⭆ Mapped over rows and joined
                                                                                                      ι Current row
                                                                                                      ⭆ Mapped over columns and joined
                                                                                                      θ Input array A
                                                                                                      ⁼ Is equal to
                                                                                                      η Input array B
                                                                                                      ✂ Sliced
                                                                                                      ¹ All elements from
                                                                                                      κ Current row index to
                                                                                                      L Length of
                                                                                                      θ Input array A
                                                                                                      ⁺ Plus
                                                                                                      κ Current row index
                                                                                                      E Mapped over rows
                                                                                                      ν Current inner row
                                                                                                      ✂ Sliced
                                                                                                      ¹ All elements from
                                                                                                      μ Current column index to
                                                                                                      L Length of
                                                                                                      θ Input array A
                                                                                                      § Indexed by
                                                                                                      ⁰ Literal 0
                                                                                                      ⁺ Plus
                                                                                                      μ Current column index
                                                                                                      Σ Digital sum
                                                                                                      I Cast to string
                                                                                                      Implicitly printed





                                                                                                      share|improve this answer















                                                                                                      Charcoal, 36 27 bytes



                                                                                                      IΣ⭆η⭆ι⁼θE✂ηκ⁺Lθκ¹✂νμ⁺L§θ⁰μ¹


                                                                                                      Try it online! Much shorter now that Equals works for arrays again. Explanation:



                                                                                                         η                        Input array B
                                                                                                      ⭆ Mapped over rows and joined
                                                                                                      ι Current row
                                                                                                      ⭆ Mapped over columns and joined
                                                                                                      θ Input array A
                                                                                                      ⁼ Is equal to
                                                                                                      η Input array B
                                                                                                      ✂ Sliced
                                                                                                      ¹ All elements from
                                                                                                      κ Current row index to
                                                                                                      L Length of
                                                                                                      θ Input array A
                                                                                                      ⁺ Plus
                                                                                                      κ Current row index
                                                                                                      E Mapped over rows
                                                                                                      ν Current inner row
                                                                                                      ✂ Sliced
                                                                                                      ¹ All elements from
                                                                                                      μ Current column index to
                                                                                                      L Length of
                                                                                                      θ Input array A
                                                                                                      § Indexed by
                                                                                                      ⁰ Literal 0
                                                                                                      ⁺ Plus
                                                                                                      μ Current column index
                                                                                                      Σ Digital sum
                                                                                                      I Cast to string
                                                                                                      Implicitly printed






                                                                                                      share|improve this answer














                                                                                                      share|improve this answer



                                                                                                      share|improve this answer








                                                                                                      edited 2 days ago

























                                                                                                      answered Dec 31 '18 at 21:49









                                                                                                      NeilNeil

                                                                                                      79.6k744177




                                                                                                      79.6k744177























                                                                                                          0















                                                                                                          Python 2, 211 bytes





                                                                                                          a,b=input()
                                                                                                          l,w,L,W,c=len(a),len(a[0]),len(b),len(b[0]),0
                                                                                                          for i in range(L):
                                                                                                          for j in range(W):
                                                                                                          if j<=W-w and i<=L-l:
                                                                                                          if not sum([a[x][y]!=b[i+x][j+y]for x in range(l)for y in range(w)]):
                                                                                                          c+=1
                                                                                                          print c


                                                                                                          Try it online!



                                                                                                          Fairly straightforward. Step through the larger matrix, and check if the smaller matrix can fit.



                                                                                                          The only even slightly tricky step is the list comprehension in the 6th line, which relies on Python's conventions for mixing Boolean and integer arithmetic.






                                                                                                          share|improve this answer


























                                                                                                            0















                                                                                                            Python 2, 211 bytes





                                                                                                            a,b=input()
                                                                                                            l,w,L,W,c=len(a),len(a[0]),len(b),len(b[0]),0
                                                                                                            for i in range(L):
                                                                                                            for j in range(W):
                                                                                                            if j<=W-w and i<=L-l:
                                                                                                            if not sum([a[x][y]!=b[i+x][j+y]for x in range(l)for y in range(w)]):
                                                                                                            c+=1
                                                                                                            print c


                                                                                                            Try it online!



                                                                                                            Fairly straightforward. Step through the larger matrix, and check if the smaller matrix can fit.



                                                                                                            The only even slightly tricky step is the list comprehension in the 6th line, which relies on Python's conventions for mixing Boolean and integer arithmetic.






                                                                                                            share|improve this answer
























                                                                                                              0












                                                                                                              0








                                                                                                              0







                                                                                                              Python 2, 211 bytes





                                                                                                              a,b=input()
                                                                                                              l,w,L,W,c=len(a),len(a[0]),len(b),len(b[0]),0
                                                                                                              for i in range(L):
                                                                                                              for j in range(W):
                                                                                                              if j<=W-w and i<=L-l:
                                                                                                              if not sum([a[x][y]!=b[i+x][j+y]for x in range(l)for y in range(w)]):
                                                                                                              c+=1
                                                                                                              print c


                                                                                                              Try it online!



                                                                                                              Fairly straightforward. Step through the larger matrix, and check if the smaller matrix can fit.



                                                                                                              The only even slightly tricky step is the list comprehension in the 6th line, which relies on Python's conventions for mixing Boolean and integer arithmetic.






                                                                                                              share|improve this answer













                                                                                                              Python 2, 211 bytes





                                                                                                              a,b=input()
                                                                                                              l,w,L,W,c=len(a),len(a[0]),len(b),len(b[0]),0
                                                                                                              for i in range(L):
                                                                                                              for j in range(W):
                                                                                                              if j<=W-w and i<=L-l:
                                                                                                              if not sum([a[x][y]!=b[i+x][j+y]for x in range(l)for y in range(w)]):
                                                                                                              c+=1
                                                                                                              print c


                                                                                                              Try it online!



                                                                                                              Fairly straightforward. Step through the larger matrix, and check if the smaller matrix can fit.



                                                                                                              The only even slightly tricky step is the list comprehension in the 6th line, which relies on Python's conventions for mixing Boolean and integer arithmetic.







                                                                                                              share|improve this answer












                                                                                                              share|improve this answer



                                                                                                              share|improve this answer










                                                                                                              answered Jan 2 at 1:23









                                                                                                              CCB60CCB60

                                                                                                              1595




                                                                                                              1595























                                                                                                                  0















                                                                                                                  Groovy, 109 bytes



                                                                                                                  {a,b->(0..<b.size()).sum{i->(0..<b[i].size()).count{j->k=i-1
                                                                                                                  a.every{l=j;k++
                                                                                                                  it.every{(b[k]?:b)[l++]==it}}}}}


                                                                                                                  Try it online!






                                                                                                                  share|improve this answer




























                                                                                                                    0















                                                                                                                    Groovy, 109 bytes



                                                                                                                    {a,b->(0..<b.size()).sum{i->(0..<b[i].size()).count{j->k=i-1
                                                                                                                    a.every{l=j;k++
                                                                                                                    it.every{(b[k]?:b)[l++]==it}}}}}


                                                                                                                    Try it online!






                                                                                                                    share|improve this answer


























                                                                                                                      0












                                                                                                                      0








                                                                                                                      0







                                                                                                                      Groovy, 109 bytes



                                                                                                                      {a,b->(0..<b.size()).sum{i->(0..<b[i].size()).count{j->k=i-1
                                                                                                                      a.every{l=j;k++
                                                                                                                      it.every{(b[k]?:b)[l++]==it}}}}}


                                                                                                                      Try it online!






                                                                                                                      share|improve this answer















                                                                                                                      Groovy, 109 bytes



                                                                                                                      {a,b->(0..<b.size()).sum{i->(0..<b[i].size()).count{j->k=i-1
                                                                                                                      a.every{l=j;k++
                                                                                                                      it.every{(b[k]?:b)[l++]==it}}}}}


                                                                                                                      Try it online!







                                                                                                                      share|improve this answer














                                                                                                                      share|improve this answer



                                                                                                                      share|improve this answer








                                                                                                                      edited Jan 2 at 2:59

























                                                                                                                      answered Jan 2 at 2:38









                                                                                                                      ASCII-onlyASCII-only

                                                                                                                      3,2341136




                                                                                                                      3,2341136























                                                                                                                          0















                                                                                                                          Scala, 151 bytes





                                                                                                                          (a,b)=>{(0 to b.size-a.size).map(i=>(0 to b(0).size-a(0).size).count(j=>{var k=i-1
                                                                                                                          a.forall(c=>{var l=j-1;k+=1
                                                                                                                          c.forall(d=>{l+=1
                                                                                                                          b(k)(l)==d})})})).sum}


                                                                                                                          Try it online!






                                                                                                                          share|improve this answer




























                                                                                                                            0















                                                                                                                            Scala, 151 bytes





                                                                                                                            (a,b)=>{(0 to b.size-a.size).map(i=>(0 to b(0).size-a(0).size).count(j=>{var k=i-1
                                                                                                                            a.forall(c=>{var l=j-1;k+=1
                                                                                                                            c.forall(d=>{l+=1
                                                                                                                            b(k)(l)==d})})})).sum}


                                                                                                                            Try it online!






                                                                                                                            share|improve this answer


























                                                                                                                              0












                                                                                                                              0








                                                                                                                              0







                                                                                                                              Scala, 151 bytes





                                                                                                                              (a,b)=>{(0 to b.size-a.size).map(i=>(0 to b(0).size-a(0).size).count(j=>{var k=i-1
                                                                                                                              a.forall(c=>{var l=j-1;k+=1
                                                                                                                              c.forall(d=>{l+=1
                                                                                                                              b(k)(l)==d})})})).sum}


                                                                                                                              Try it online!






                                                                                                                              share|improve this answer















                                                                                                                              Scala, 151 bytes





                                                                                                                              (a,b)=>{(0 to b.size-a.size).map(i=>(0 to b(0).size-a(0).size).count(j=>{var k=i-1
                                                                                                                              a.forall(c=>{var l=j-1;k+=1
                                                                                                                              c.forall(d=>{l+=1
                                                                                                                              b(k)(l)==d})})})).sum}


                                                                                                                              Try it online!







                                                                                                                              share|improve this answer














                                                                                                                              share|improve this answer



                                                                                                                              share|improve this answer








                                                                                                                              edited Jan 2 at 6:49

























                                                                                                                              answered Jan 2 at 6:34









                                                                                                                              ASCII-onlyASCII-only

                                                                                                                              3,2341136




                                                                                                                              3,2341136






























                                                                                                                                  draft saved

                                                                                                                                  draft discarded




















































                                                                                                                                  If this is an answer to a challenge…




                                                                                                                                  • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                                                                                                                                  • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                                                                                                                    Explanations of your answer make it more interesting to read and are very much encouraged.


                                                                                                                                  • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.



                                                                                                                                  More generally…




                                                                                                                                  • …Please make sure to answer the question and provide sufficient detail.


                                                                                                                                  • …Avoid asking for help, clarification or responding to other answers (use comments instead).





                                                                                                                                  draft saved


                                                                                                                                  draft discarded














                                                                                                                                  StackExchange.ready(
                                                                                                                                  function () {
                                                                                                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178173%2fcount-the-contiguous-submatrices%23new-answer', 'question_page');
                                                                                                                                  }
                                                                                                                                  );

                                                                                                                                  Post as a guest















                                                                                                                                  Required, but never shown





















































                                                                                                                                  Required, but never shown














                                                                                                                                  Required, but never shown












                                                                                                                                  Required, but never shown







                                                                                                                                  Required, but never shown

































                                                                                                                                  Required, but never shown














                                                                                                                                  Required, but never shown












                                                                                                                                  Required, but never shown







                                                                                                                                  Required, but never shown







                                                                                                                                  Popular posts from this blog

                                                                                                                                  MongoDB - Not Authorized To Execute Command

                                                                                                                                  How to fix TextFormField cause rebuild widget in Flutter

                                                                                                                                  in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith