Compare commits
2 Commits
32cab0c401
...
f458ca0c9b
| Author | SHA1 | Date | |
|---|---|---|---|
| f458ca0c9b | |||
| 198ac9c607 |
196
src/04a.js
Normal file
196
src/04a.js
Normal file
@ -0,0 +1,196 @@
|
||||
const { inputs } = require("./inputs/04.json");
|
||||
|
||||
/**
|
||||
* @param {String} inputStr
|
||||
* @returns {Array} Array of column values for the row
|
||||
*/
|
||||
function parseRow(inputStr) {
|
||||
const cols = new Array();
|
||||
for (let i = 0; i < inputStr.length; i++) {
|
||||
cols.push(inputStr[i]);
|
||||
}
|
||||
return cols;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Number} rowIdx
|
||||
* @param {Number} colIdx
|
||||
* @returns {Boolean} true if the spot is occupied, false if free
|
||||
*/
|
||||
function checkNW(rowIdx, colIdx) {
|
||||
const row = rows[rowIdx - 1];
|
||||
const value = row[colIdx - 1];
|
||||
if (value === '@') return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Number} rowIdx
|
||||
* @param {Number} colIdx
|
||||
* @returns {Boolean} true if the spot is occupied, false if free
|
||||
*/
|
||||
function checkN(rowIdx, colIdx) {
|
||||
const row = rows[rowIdx - 1];
|
||||
const value = row[colIdx];
|
||||
if (value === '@') return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Number} rowIdx
|
||||
* @param {Number} colIdx
|
||||
* @returns {Boolean} true if the spot is occupied, false if free
|
||||
*/
|
||||
function checkNE(rowIdx, colIdx) {
|
||||
const row = rows[rowIdx - 1];
|
||||
const value = row[colIdx + 1];
|
||||
if (value === '@') return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Number} rowIdx
|
||||
* @param {Number} colIdx
|
||||
* @returns {Boolean} true if the spot is occupied, false if free
|
||||
*/
|
||||
function checkW(rowIdx, colIdx) {
|
||||
const row = rows[rowIdx];
|
||||
const value = row[colIdx - 1];
|
||||
if (value === '@') return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Number} rowIdx
|
||||
* @param {Number} colIdx
|
||||
* @returns {Boolean} true if the spot is occupied, false if free
|
||||
*/
|
||||
function checkE(rowIdx, colIdx) {
|
||||
const row = rows[rowIdx];
|
||||
const value = row[colIdx + 1];
|
||||
if (value === '@') return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Number} rowIdx
|
||||
* @param {Number} colIdx
|
||||
* @returns {Boolean} true if the spot is occupied, false if free
|
||||
*/
|
||||
function checkSW(rowIdx, colIdx) {
|
||||
const row = rows[rowIdx + 1];
|
||||
const value = row[colIdx - 1];
|
||||
if (value === '@') return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Number} rowIdx
|
||||
* @param {Number} colIdx
|
||||
* @returns {Boolean} true if the spot is occupied, false if free
|
||||
*/
|
||||
function checkS(rowIdx, colIdx) {
|
||||
const row = rows[rowIdx + 1];
|
||||
const value = row[colIdx];
|
||||
if (value === '@') return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Number} rowIdx
|
||||
* @param {Number} colIdx
|
||||
* @returns {Boolean} true if the spot is occupied, false if free
|
||||
*/
|
||||
function checkSE(rowIdx, colIdx) {
|
||||
const row = rows[rowIdx + 1];
|
||||
const value = row[colIdx + 1];
|
||||
if (value === '@') return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function checkAdjacent(rowIdx, colIdx) {
|
||||
let paperCount = 0;
|
||||
const maxRow = rows.length - 1;
|
||||
const maxCol = rows[colIdx].length - 1;
|
||||
if ((rowIdx > 0) && (rowIdx < maxRow)) { // We're not on the top or bottom row so we need to check above and below
|
||||
if ((colIdx > 0) && (colIdx < maxCol)) { // We're not in the leftmost or rightmost spot so we need to check all 8
|
||||
if (checkNW(rowIdx, colIdx)) paperCount++;
|
||||
if (checkN(rowIdx, colIdx)) paperCount++;
|
||||
if (checkNE(rowIdx, colIdx)) paperCount++;
|
||||
if (checkW(rowIdx, colIdx)) paperCount++;
|
||||
if (checkE(rowIdx, colIdx)) paperCount++;
|
||||
if (checkSW(rowIdx, colIdx)) paperCount++;
|
||||
if (checkS(rowIdx, colIdx)) paperCount++;
|
||||
if (checkSE(rowIdx, colIdx)) paperCount++;
|
||||
} else if (colIdx === 0) { // We are in the leftmost column so only check Easterly
|
||||
if (checkN(rowIdx, colIdx)) paperCount++;
|
||||
if (checkNE(rowIdx, colIdx)) paperCount++;
|
||||
if (checkE(rowIdx, colIdx)) paperCount++;
|
||||
if (checkS(rowIdx, colIdx)) paperCount++;
|
||||
if (checkSE(rowIdx, colIdx)) paperCount++;
|
||||
} else if (colIdx === maxCol) { // We are in the rightmost column so only check Westerly
|
||||
if (checkNW(rowIdx, colIdx)) paperCount++;
|
||||
if (checkN(rowIdx, colIdx)) paperCount++;
|
||||
if (checkW(rowIdx, colIdx)) paperCount++;
|
||||
if (checkSW(rowIdx, colIdx)) paperCount++;
|
||||
if (checkS(rowIdx, colIdx)) paperCount++;
|
||||
}
|
||||
} else if (rowIdx === 0) { // We are in the top row so only check Southerly
|
||||
if ((colIdx > 0) && (colIdx < maxCol)) { // We're not in the leftmost or rightmost spot so we need to check all
|
||||
if (checkW(rowIdx, colIdx)) paperCount++;
|
||||
if (checkE(rowIdx, colIdx)) paperCount++;
|
||||
if (checkSW(rowIdx, colIdx)) paperCount++;
|
||||
if (checkS(rowIdx, colIdx)) paperCount++;
|
||||
if (checkSE(rowIdx, colIdx)) paperCount++;
|
||||
} else if (colIdx === 0) { // We are in the leftmost column so only check Easterly
|
||||
if (checkE(rowIdx, colIdx)) paperCount++;
|
||||
if (checkS(rowIdx, colIdx)) paperCount++;
|
||||
if (checkSE(rowIdx, colIdx)) paperCount++;
|
||||
} else if (colIdx === maxCol) { // We are in the rightmost column so only check Westerly
|
||||
if (checkW(rowIdx, colIdx)) paperCount++;
|
||||
if (checkSW(rowIdx, colIdx)) paperCount++;
|
||||
if (checkS(rowIdx, colIdx)) paperCount++;
|
||||
}
|
||||
} else if (rowIdx === maxRow) { // We are in the bottom row so only check Northerly
|
||||
if ((colIdx > 0) && (colIdx < maxCol)) { // We're not in the leftmost or rightmost spot so we need to check all 8
|
||||
if (checkNW(rowIdx, colIdx)) paperCount++;
|
||||
if (checkN(rowIdx, colIdx)) paperCount++;
|
||||
if (checkNE(rowIdx, colIdx)) paperCount++;
|
||||
if (checkW(rowIdx, colIdx)) paperCount++;
|
||||
if (checkE(rowIdx, colIdx)) paperCount++;
|
||||
} else if (colIdx === 0) { // We are in the leftmost column so only check Easterly
|
||||
if (checkN(rowIdx, colIdx)) paperCount++;
|
||||
if (checkNE(rowIdx, colIdx)) paperCount++;
|
||||
if (checkE(rowIdx, colIdx)) paperCount++;
|
||||
} else if (colIdx === maxCol) { // We are in the rightmost column so only check Westerly
|
||||
if (checkNW(rowIdx, colIdx)) paperCount++;
|
||||
if (checkN(rowIdx, colIdx)) paperCount++;
|
||||
if (checkW(rowIdx, colIdx)) paperCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (paperCount < 4) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const rows = new Array();
|
||||
inputs.forEach(row => {
|
||||
const cols = parseRow(row);
|
||||
rows.push(cols);
|
||||
return;
|
||||
});
|
||||
|
||||
let moveableCount = 0;
|
||||
for (let rowIdx = 0; rowIdx < rows.length; rowIdx++) {
|
||||
for (let colIdx = 0; colIdx < rows[rowIdx].length; colIdx++) {
|
||||
const row = rows[rowIdx];
|
||||
const value = row[colIdx];
|
||||
if (value === '@') {
|
||||
if (checkAdjacent(rowIdx, colIdx)) moveableCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(moveableCount);
|
||||
Loading…
Reference in New Issue
Block a user