daily pastebin goal
42%
SHARE
TWEET

Untitled

a guest Dec 13th, 2017 145 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const sequence = [...Array(256).keys()];
  2. const GRID_SIZE = 128
  3. function part1(input) {
  4.     const grid = Array(GRID_SIZE).fill(Array(GRID_SIZE).fill(0));
  5.     let used = 0;
  6.     for (let i = 0; i < GRID_SIZE; ++i) {
  7.         const toHash = `${input}-${i}`;
  8.         used += getKnotHash(toHash)
  9.             .split("")
  10.             .map(hexDigit => parseInt(hexDigit, 16).toString(2))
  11.             .join("")
  12.             .match(/1/g)
  13.             .length;
  14.     }
  15.     return used;
  16. }
  17.  
  18. function part2(input) {
  19.     const grid = [];
  20.     const INITIAL_GROUP_NUMBER = 2;
  21.     let groupNumber = INITIAL_GROUP_NUMBER;
  22.     for (let i = 0; i < GRID_SIZE; ++i) {
  23.         const toHash = `${input}-${i}`;
  24.         grid[i] = getKnotHash(toHash)
  25.             .split("")
  26.             .map(hexDigit => parseInt(hexDigit, 16).toString(2).padStart(4, "0"))
  27.             .join("")
  28.             .split("")
  29.             .map(Number);
  30.     }
  31.  
  32.     for (let rowNumber = 0; rowNumber < GRID_SIZE; ++rowNumber) {
  33.         const row = grid[rowNumber];
  34.         for (let columnNumber = 0; columnNumber < GRID_SIZE; ++columnNumber) {
  35.             const column = row[columnNumber];
  36.             let markedSomething = markGroup(groupNumber, grid, JSON.parse( JSON.stringify(rowNumber) ), JSON.parse( JSON.stringify(columnNumber) ));
  37.             if (markedSomething) {
  38.                 ++groupNumber;
  39.             }
  40.         }
  41.     }
  42.     return groupNumber - INITIAL_GROUP_NUMBER;
  43. }
  44.  
  45. function markGroup(groupNumber, grid, rowNumber, columnNumber) {
  46.     let row = grid[rowNumber];
  47.     if (row) {
  48.         if (grid[rowNumber][columnNumber] === 1) {
  49.             grid[rowNumber][columnNumber] = groupNumber;
  50.             markGroup(groupNumber, grid, rowNumber - 1, columnNumber);
  51.             markGroup(groupNumber, grid, rowNumber + 1, columnNumber);
  52.             markGroup(groupNumber, grid, rowNumber, columnNumber - 1);
  53.             markGroup(groupNumber, grid, rowNumber, columnNumber + 1);
  54.             return true;
  55.         }
  56.     }
  57.     return false;
  58. }
  59.  
  60. function getKnotHash(input) {
  61.     let position = 0;
  62.     let skip = 0;
  63.     input = input.split("").map(c => c.charCodeAt(0)).concat([17, 31, 73, 47, 23]);
  64.    
  65.     let knot = tieKnot(sequence, input, 64);
  66.    
  67.     return getHexForArray(getDenseHash(knot));
  68. }
  69. function tieKnot(input, lengths, rounds = 1) {
  70.     let result = input.slice();
  71.     let position = 0;
  72.     let skip = 0;
  73.  
  74.     for (let round = 0; round < rounds; round++) {
  75.       for (let i = 0; i < lengths.length; i++) {
  76.         let loopLength = lengths[i];
  77.         let reversedSection = [];
  78.  
  79.         for (let at = position, x = 0; x < loopLength; x++) {
  80.           at = (position + x) % result.length;
  81.           reversedSection.unshift(result[at]);
  82.         }
  83.  
  84.         for (let at = position, x = 0; x < loopLength; x++) {
  85.           at = (position + x) % result.length;
  86.           result[at] = reversedSection[x];
  87.         }
  88.  
  89.         position = (position + loopLength + skip) % result.length;
  90.         skip++;
  91.       }
  92.     }
  93.  
  94.     return result;
  95.   }
  96. function getDenseHash(sparseHash) {
  97.     let result = [];
  98.  
  99.     for (let blockNr = 0; blockNr < 16; blockNr++) {
  100.         let block = sparseHash.slice(blockNr * 16, (blockNr + 1) * 16);
  101.         result[blockNr] = block.reduce((a,b) => a ^ b);
  102.     }
  103.  
  104.     return result;
  105. }
  106.  
  107. function getHexForArray(denseHash) {
  108.     return denseHash
  109.         .map(digit => ("0" + digit.toString(16)).substr(-2))
  110.         .join("");
  111. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top