Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas width="400" height="400"></canvas>
<div>
  <p>Pattern: <span id="pattern"></span></p>
  <p>Subsection: <span id="subsection"></span></p>
  <p>Accuracy: <span id="accuracy"></span></p>
  <p>Row/Col: <span id="rowcol"></span></p>
</div>

<section>
  <button id="previous">Previous</button> <mark style="padding: 5px 20px;">0</mark> <button id="next">Next</button>
</section>

              
            
!

CSS

              
                body {
  display: flex;
  justify-content: center;
  align-items:center;
  height: 100vh;
  flex-direction: column;
  font-family: Arial, sans-serif;
}
canvas {
  border: 1px solid black;
}
div {
  padding: 20px;
}
              
            
!

JS

              
                console.clear();

// ALGORITHM:
function findPattern( pattern, target, overThan = 0 ){

  // ACCURACY BASED ON SAME VALUES: 0 AND 1
  function compare( arr1, arr2 ){
    let accuracy = 0;
    let missingOnes = 0;
    const result = arr1.map( (entry, index) => {
      const areEqual = arr2[index] === entry;
      if ( areEqual ){ 
          // accuracy++; 
          if ( entry === 1 ){
            accuracy += 2;
          } else {
            accuracy += 0.5;
          }
      } 
      if ( entry === -1 ){ missingOnes++; }
      return areEqual;
    });
    const valid = arr1.length - missingOnes;
    return [ result, ( accuracy / valid ) * ( valid / arr1.length ) ];
  }

  const targetWidth = target[0].length;
  const targetHeight = target.length;
  const patternWidth = pattern[0].length;
  const patternHeight = pattern.length;
  const vLimit = targetHeight + patternHeight;
  const hLimit = targetWidth + patternWidth;

  let results = {}
  let fullList = [];

  for ( let row = 1; row < vLimit; row++ ){

    for ( let col = 1; col < hLimit; col++ ){

      // Check how many rows of window we have at this point:
      let availRows; 
      if ( row === ( vLimit - 1 ) ){
        availRows = 1;
      } else if ( row > targetHeight ) {
        availRows = row - patternHeight;
      } else if ( row === 1 ) {
        availRows = 1;
      } else {
        availRows = row > patternHeight ? patternHeight : row;
      }

      // Check how many cells of pattern we have at this point:
      let availCells = patternWidth;
      if ( col > targetWidth || col === 1 ){
        availCells = 1;
      } 

      // COMPARISON:
      let targetValues = []; 
      let heightCursor = patternHeight;
      while ( heightCursor > 0 ){
        let widthCursor = patternWidth;
        while ( widthCursor > 0 ){
          const targetRow = row - heightCursor;
          const targetCol = col - widthCursor;
          if ( typeof target[ targetRow ] === "undefined" || targetRow < 0 || targetCol < 0 ) {
            targetValues.push( -1 );
          } else {
            const val = target[ targetRow ][ targetCol ];
            targetValues.push( typeof val !== "undefined" ? val : -1 );
          }
          widthCursor--;
        }
        heightCursor--;
      }

      // PRODUCE RESULTS:
      const [ , accuracy ] = compare( targetValues, pattern.flat() );
      const accuracyPercentage = ( accuracy * 100 ).toFixed();

      // console.log( `\nRow/Col: ${row} / ${col} | Accuracy: ${accuracyPercentage}%` );
      // console.log( `Target: ${targetValues} <- Pattern: ${pattern.flat()} `);
      const matched = {
        accuracy: accuracyPercentage,
        subsection: targetValues,
        pattern: pattern.flat(),
        position: { row, col },
        equal: targetValues.join(" ") === pattern.flat().join(" ")
        // subsectionString: targetValues.join(" "),
        // patternString: pattern.flat().join(" "),
        // positionString : `${row} / ${col}`
      }
      fullList.push( matched );
      if ( accuracyPercentage > overThan ){
        if ( !results[ accuracyPercentage ] ){
          results[ accuracyPercentage ] = [ matched ];
        } else {
          results[ accuracyPercentage ].push( matched );
        }
      }

    }
  }

  return [ results, fullList ];

}

// TESTS:

let target1 = [
  [ 1, 0, 0, 1 ],
  [ 0, 1, 0, 0 ],
  [ 1, 0, 1, 0 ],
  [ 1, 1, 0, 0 ],
];
let pattern1 = [
  [ 1, 0 ],
  [ 0, 1 ] 
];
// let [ result1 ] = findPattern( pattern1, target1, -1 );
// console.log( "Test #1", result1, "\n---" );

let target2 = [
  [ 1, 0, 0 ],
  [ 0, 1, 0 ],
  [ 1, 0, 1 ]
];
let pattern2 = [
  [ 1, 0 ],
  [ 0, 1 ] 
];
// let [ result2 ] = findPattern( pattern2, target2, 77 );
// console.log( "Test #2", result2, "\n---" );

// POSTED EXAMPLE:
let target3 = [
  [ 1, 0,	0,	0,	0,	0,	0,	0,	1,	0,	0,	1,	0,	0,	1, ],
  [ 0, 0,	1,	0,	1,	0,	0,	0,	0,	1,	0,	0,	0,	0,	0, ],
  [ 0, 0,	0,	1,	1,	0,	0,	0,	0,	1,	0,	0,	0,	0,	0, ],
  [ 0, 0,	0,	0,	0,	0,	1,	0,	0,	0,	0,	1,	0,	0,	1, ],
  [ 0, 0,	0,	0,	0,	0,	1,	0,	0,	0,	0,	1,	0,	0,	0, ],
  [ 0, 0,	1,	0,	0,	0,	0,	0,	1,	0,	0,	0,	0,	0,	1, ],
  [ 1, 0,	1,	0,	1,	0,	1,	1,	0,	0,	0,	0,	0,	0,	0, ],
  [ 0, 0,	0,	0,	0,	0,	0,	0,	0,	0,	0,	0,	0,	0,	0, ],
  [ 0, 0,	0,	1,	0,	0,	0,	0,	0,	1,	0,	0,	0,	0,	0, ],
  [ 0, 0,	1,	0,	1,	0,	0,	0,	0,	0,	0,	0,	0,	0,	1, ],
  [ 0, 0,	1,	0,	0,	0,	0,	0,	0,	0,	0,	0,	1,	0,	0, ],
  [ 0, 1,	1,	0,	0,	0,	0,	0,	0,	0,	0,	0,	0,	0,	0, ],
  [ 0, 0,	1,	0,	0,	1,	0,	1,	0,	0,	1,	0,	1,	0,	0, ]
];
let pattern3 = [
  [ 1, 0, 0, 0, 0 ],
  [ 0, 0, 1, 0, 0 ],
  [ 0, 0, 1, 0, 0 ],
  [ 0, 0, 0, 0, 1 ],
  [ 1, 0, 1, 1, 0 ]
];
let [ result3, fullList ] = findPattern( pattern3, target3, 80 );
// console.log( "Test #3", result3, "\n---" );

let target4 = [
  [ 0, 1, 0, 0, 0, 0, 0, 1 ],
  [ 1, 0, 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 1, 0, 0, 0, 0, 0 ],
  [ 0, 0, 0, 0, 0, 0, 0, 1 ],
  [ 0, 0, 0, 0, 0, 1, 0, 0 ],
  [ 0, 0, 0, 0, 0, 0, 0, 0 ],
  [ 1, 0, 0, 1, 0, 1, 0, 0 ]
]
let pattern4 = [
  [ 1, 0 ],
  [ 0, 0 ],
  [ 0, 0 ],
  [ 0, 0 ],
  [ 0, 1 ],
];
// let [ result4 ] = findPattern( pattern4, target4, 99 );
// console.log( "Test #4", result4, "\n---" );

// NON-BINARY VALUES
let target0 = [
  [  1,  2,  3,  4 ],
  [  5,  6,  7,  8 ],
  [  9, 10, 11, 12 ],
  [ 13, 14, 15, 16 ],
];

let pattern0 = [
  [  6,  7 ],
  [ 10, 4 ] 
];

//// CONTROLS | UI SECTION ////
const prev = document.querySelector("#previous");
const next = document.querySelector("#next");
const mark = document.querySelector("mark");
const pattern = document.querySelector("#pattern");
const subsection = document.querySelector("#subsection");
const accuracy = document.querySelector("#accuracy");
const rowcol = document.querySelector("#rowcol");
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
const pixels = 25;

// CREATE TARGET:
function renderTarget( target ){
  ctx.clearRect(0,0,canvas.width,canvas.height);
  const tableLen = target.length;
  const tableHeight = target[0].length;
  canvas.width = tableHeight * pixels;
  canvas.height = tableLen * pixels;
  ctx.lineWidth = 1;
  target.flat().forEach( (val,idx,arr) => {
    const col = idx % tableHeight;
    const row = Math.floor( ( idx ) / tableHeight );
    if ( val ){
      ctx.fillRect(col*pixels,row*pixels,pixels,pixels);
    } else {
      ctx.strokeRect(col*pixels,row*pixels,pixels,pixels);
    }
  });
  
}

// CREATE PATTERN:
function renderPattern( row, col, pattern ){

  const patternTableHeight = pattern[0].length;
  pattern.flat().forEach( (val,idx,arr) => {
    const _col = idx % patternTableHeight;
    const _row = Math.floor( ( idx ) / patternTableHeight );
    if ( val ){
      ctx.fillStyle = "rgba(255,0,0,0.75)";
      ctx.fillRect((_col+(col-patternTableHeight))*pixels,(_row+(row-patternTableHeight))*pixels,pixels,pixels);
    } else {
      ctx.fillStyle = "rgba(255,100,100,0.5)";
      ctx.fillRect((_col+(col-patternTableHeight))*pixels,(_row+(row-patternTableHeight))*pixels,pixels,pixels);
    }
  });

}

renderTarget( target3 );
renderPattern( 7, 9, pattern3 );

counter = 122;
displayStats( 122 );

prev.addEventListener("click", function(){
  if ( counter > 0 ){
    counter--;
    displayStats( counter );
  }
});
next.addEventListener("click", function(){
  if ( counter < fullList.length-1 ){
    counter++;
    displayStats( counter );
  }
});

function displayStats( counter ){
  mark.textContent = counter;
  
  // pattern.textContent = fullList[counter].pattern.join("");
  // subsection.textContent = fullList[counter].subsection.join("");
  
  const patternContent = fullList[counter].pattern.map( (digit,idx) => {
    if ( digit === fullList[counter].subsection[idx] ){
      return "<strong>" + digit + "</strong>";    
    } else {
      return "<span style='color:lightgray;'>" + digit + "</span>";
    }
  });
  pattern.innerHTML = patternContent.join('');

  const subsectionContent = fullList[counter].pattern.map( (digit,idx) => {
    if ( digit === fullList[counter].pattern[idx] ){
      return "<strong>" + digit + "</strong>";    
    } else {
      return "<span style='color:lightgray;'>" + digit + "</span>";
    }
  });
  subsection.innerHTML = subsectionContent.join('');

  accuracy.textContent = fullList[counter].accuracy + "%";
  rowcol.textContent = fullList[counter].position.row + "/" + fullList[counter].position.col;
  renderTarget( target3 );
  renderPattern( fullList[counter].position.row, fullList[counter].position.col, pattern3 );
 
}
              
            
!
999px

Console