Day 13: Point of Incidence

At first my code solved the test input but not the real input. I knew there was some kind of bug and suspected it was to do with the boundaries of the blocks, like when a mirror is at the edge of a block, but that wasn't it. After a long time searching for the bug I've found the issue… When I was checking each block for the found mirror position, I was expecting it to either be null or a number for the position: if the position was 0, although it was something, and not null, I was saying if( 0 ), which obviously returns false. Face palm. Part one done now, although I am quite dissatisfied with how many issues I had with this towards the end.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 const inputToUse = realInput; // console.log('inputToUse',inputToUse); const inputGroups = inputToUse.split(/\n\s*\n/); console.log('inputGroups',inputGroups); const blocks = []; inputGroups.forEach(function(group){ const block = []; const lines = group.split(/\n/); lines.forEach(function(line){ // console.log('line',line); const cleanLine = line.trim(); const chars = cleanLine.split(''); block.push(chars); }); blocks.push(block); }); // console.log('blocks',blocks); // we now have blocks // check blocks for horz mirrors const horzMirrors = []; blocks.forEach(function(block, blockId){ const blockLength = block.length; // console.log('blockLength',blockLength); var horzMirrorAt = null; block.forEach(function(row,i){ var lookingForMirror = true; var checkingRowNo = i; var nextCheckingRowNo = i+1; var thisRowMirror = null; while( lookingForMirror ) { if( checkingRowNo >= 0 && nextCheckingRowNo <= blockLength-1 ) { const checkingRow = block[checkingRowNo]; const checkingRowChars = block[checkingRowNo].toString(); const nextRow = block[nextCheckingRowNo]; const nextRowChars = nextRow.toString(); // if mirror image found if( checkingRowChars === nextRowChars ) { if( thisRowMirror == null ) { thisRowMirror = i; } checkingRowNo = checkingRowNo - 1; nextCheckingRowNo = nextCheckingRowNo + 1; } else { thisRowMirror = null; lookingForMirror = false; } } else { // if a mirror found if( thisRowMirror != null ) { horzMirrorAt = thisRowMirror } lookingForMirror = false; } } }); if( horzMirrorAt != null ) { // console.log(`block ${blockId} has a horz mirror at ${horzMirrorAt + 1}`); horzMirrors.push(horzMirrorAt + 1); } }); // console.log('horzMirrors',horzMirrors); // check blocks for vert mirrors const vertMirrors = []; blocks.forEach(function(block, blockId) { const blockWidth = block[0].length; var vertMirrorAt = null; // for each column for( var i=0; i<blockWidth; i++ ) { // Check for Horz mirrors var lookingForMirror = true; var checkingColNo = i; var nextCheckingColNo = i+1; var thisColMirror = null; while( lookingForMirror ) { if( checkingColNo >= 0 && nextCheckingColNo <= blockWidth-1 ) { const checkingCol = []; const nextCheckingCol = []; block.forEach(function(row){ const char = row[checkingColNo]; checkingCol.push(char); const nextColChar = row[nextCheckingColNo]; nextCheckingCol.push(nextColChar); }); const checkingColChars = checkingCol.toString(); const nextCheckingColChars = nextCheckingCol.toString(); // if mirror image found if( checkingColChars === nextCheckingColChars ) { if( thisColMirror === null ) { thisColMirror = i; } checkingColNo = checkingColNo - 1; nextCheckingColNo = nextCheckingColNo + 1; } else { thisColMirror = null; lookingForMirror = false; } } else { if( thisColMirror != null ) { vertMirrorAt = thisColMirror; } lookingForMirror = false; } } } // console.log('vertMirrorAt',vertMirrorAt); if( vertMirrorAt != null) { vertMirrors.push(vertMirrorAt + 1); } }); console.log('horzMirrors',horzMirrors); // console.log('vertMirrors',vertMirrors); var horzTotal = 0; horzMirrors.forEach(function(value) { horzTotal = horzTotal + value; }); var vertTotal = 0; vertMirrors.forEach(function(value) { vertTotal = vertTotal + value; }); console.log('vertTotal',vertTotal); console.log('horzTotal',horzTotal); const adjustedHorzTot = parseFloat(`${horzTotal}00`); console.log('adjustedHorzTot',adjustedHorzTot); const part1FinalCalc = vertTotal + adjustedHorzTot; console.log('part1FinalCalc',part1FinalCalc);
All days / Day 14