Nathan (2021)
Day 4 Solutions:
/////////// JS File run using node.js ////////////
var fs = require('fs');
const { matrix, column, forEach, ones, flatten, row, dotMultiply } = require('mathjs');
var bingoCount = 0;
function main () {
var data = fs.readFileSync("./input.txt")
var fullArray = data.toString().split('\n');
var callList = fullArray.shift().split(',').map(Number);
const boardsArray = [];
const boardsMirror = [];
const winOrder = [];
///////// This chunk from sk1talets /////
const boards = [];
let board;
for (const line of fullArray) {
if (line === '') {
//initialize board array container for every empty array from the newline split
board = [];
boards.push(board);
} else {
board.push(line.trim().split(/\s+/).map(n => parseInt(n, 10)));
}
}
/////////////////////////////////////////
// Create array of matrices to represent boards
for (let boardb in boards){
boardsArray.push(matrix(boards[boardb]));
}
// Create mask for each board in mirror array (this is where we mark the numbers called)
for (let boards in boardsArray){
var mirror = ones(5,5);
boardsMirror.push(mirror);
}
///////////// BOARDS COMPLETE -> Begin Function //////
for (call of callList){
for (let curBoard in boardsArray){
var currentBoard = boardsArray[curBoard]
var currentMirror = boardsMirror[curBoard];
forEach(currentBoard, function(val, ind){
if (val === call){
// mark mask array at the same index
currentMirror.set(ind,0)
// check each row and col of mirror to see if Bingo
for (let x = 0; x < 5; x++){
var colCount = flatten(column(currentMirror, x))._data.reduce(function(a,b){
return a+b
})
var rowCount = flatten(row(currentMirror, x))._data.reduce(function(a,b){
return a+b
})
// Added the && curBoard == 45 for Part 2
if ((rowCount == 0 || colCount == 0) && curBoard == 45){
bingoCount++;
winBoard = curBoard;
// multiply the board with its mirror to get unmarked nums, flatten to single dimension...
// ... sum to get score
var unmatchedScore = flatten(dotMultiply(currentMirror, currentBoard))._data.reduce(function(a,b){
return a+b
})
console.log("Board: " + curBoard + ", call: " + call + ", unmatched score: " + unmatchedScore + ", Total:" + unmatchedScore*call);
// Added for Part 2
if (!winOrder.find(function (val){return val===curBoard})){
winOrder.push(curBoard)
}
}
}
}
})
}
if (bingoCount > 0){
//commented the break out for part 2
//break
}
}
// Prints the board needed for part 2
console.log(winOrder[winOrder.length-1])
}
main()
Day 6 Solution:
Day 6 Code/////// New code from sk1talets - https://github.com/sk1talets/advent-of-code/blob/main/2021/6/script.2.js
function main(){
.... var test = [3,4,3,1,2]
//var test = [5,1,2,1,5,3,1,1,1,1,1,2,5,4,1,1,1,1,2,1,2,1,1,1,1,1,2,1,5,1,1,1,3,1,1,1,3,1,1,3,1,1,4,3,1,1,4,1,1,1,1,2,1,1,1,5,1,1,5,1,1,1,4,4,2,5,1,1,5,1,1,2,2,1,2,1,1,5,3,1,2,1,1,3,1,4,3,3,1,1,3,1,5,1,1,3,1,1,4,4,1,1,1,5,1,1,1,4,4,1,3,1,4,1,1,4,5,1,1,1,4,3,1,4,1,1,4,4,3,5,1,2,2,1,2,2,1,1,1,2,1,1,1,4,1,1,3,1,1,2,1,4,1,1,1,1,1,1,1,1,2,2,1,1,5,5,1,1,1,5,1,1,1,1,5,1,3,2,1,1,5,2,3,1,2,2,2,5,1,1,3,1,1,1,5,1,4,1,1,1,3,2,1,3,3,1,3,1,1,1,1,1,1,1,2,3,1,5,1,4,1,3,5,1,1,1,2,2,1,1,1,1,5,4,1,1,3,1,2,4,2,1,1,3,5,1,1,1,3,1,1,1,5,1,1,1,1,1,3,1,1,1,4,1,1,1,1,2,2,1,1,1,1,5,3,1,2,3,4,1,1,5,1,2,4,2,1,1,1,2,1,1,1,1,1,1,1,4,1,5]
.... var dys = 3
.... var states = Array(9).fill(0)
.... test.forEach(function (v){
........ states[v] += 1;
.... })
.... for (let x = 0; x < dys; x++){
........ var births = states.shift();
........ states[6] += births;
........ states.push(births);
.... }
.... let total = states.reduce((cur, prev) => cur + prev)
.... console.log(states.join(','))
.... console.log(total)
.... //// Script worked for part 1 but errored out because used up heap in part 2///
.... /*for(let x = 0; x < dys; x++){
........ //console.log(x)
........ var birth = 0;
........ test.forEach(function(r, ind) {
............ if (r != 0){
................ test[ind] --
............ } else {
................ test[ind] +=6;
................ birth ++
................ //console.log(test)
............ }
........ });
........ if (birth != 0){
............ for (let x = 0; x < birth; x++){
................ test.push(8)
............ }
........ }
.... }*/
}
main()