Horray, a day I could finish! ;) I've been quite as I'm still struggling to solve part 2 for both day 6 and day 7....taking a break from those for a few days! (So I say, anyways lol)
JavaScript solution below - I used the Chalk library to colorize my text so the image was easier to read, and grabbed a screenshot. A little sad my image didn't spell out anything obvious, like I saw some other people's on the subreddit! My little terminal snowman makes me happy though. lol :)
Also certain this could have been done in fewer passes through the layers, but it was still very fast so not concerned about it.
constfs=require('fs');constchalk=require('chalk');constdata=fs.readFileSync('../2019 Solutions/inputs/day08input.txt').toString();constinput=data.split('').map(Number);// image will be 25 px wide (rows) by 6 px tall (cols)// so each layer is 25x6 - need to know how many of each digit (0,1,2) exist on ea layer// find layer with the fewest 0s, then find # of 1s x # of 2sletwidth=25;letheight=6;letlayers=[];letzeros=0;letones=0;lettwos=0;letlowestZeros=0;letmulti=0;letcurrRow=[];letcurrLayer=[];letcurrCol=1;// build out the layersfor(leti=0;i<=input.length;i++){if(currRow.length<width){currRow.push(input[i]);}elseif(currRow.length===width){if(currCol<height){currLayer.push(currRow);currRow=[];currRow.push(input[i]);currCol++;}elseif(currCol===height){currLayer.push(currRow);layers.push(currLayer);currLayer=[];currRow=[];currCol=1;currRow.push(input[i]);}}}// part 1 - count the valueslayers.forEach(arr=>{for(leti=0;i<arr.length;i++){letcurrArr=arr[i];for(letc=0;c<currArr.length;c++){letval=currArr[c];if(val===0){zeros++;}elseif(val===1){ones++;}elseif(val===2){twos++;}}}if(lowestZeros===0){lowestZeros=zeros;multi=ones*twos;}elseif(zeros<lowestZeros){lowestZeros=zeros;multi=ones*twos;}ones=0;twos=0;zeros=0;});console.log(`Part 1: ones * twos is ${multi}`);letfinalImage=[];letsingleRow=[];// part 2 - determine which pixels show uplayers.forEach(layer=>{for(lety=0;y<layer.length;y++){for(letx=0;x<layer[y].length;x++){if(finalImage[y]){singleRow=finalImage[y];letdata=singleRow[x];if(data===2){if(layer[y][x]===0){singleRow.splice(x,1,0);}elseif(layer[y][x]===1){singleRow.splice(x,1,1);}}}else{singleRow=layer[y];}}finalImage.splice(y,1,singleRow);}})// colorize each pixel to read messagefinalImage.forEach(row=>{for(leth=0;h<row.length;h++){if(row[h]===0){row[h]=chalk.black(0);}elseif(row[h]===1){row[h]=chalk.white.bgWhite(1);}elseif(row[h]===2){row[h]=chalk.hidden(2);}}row.join();console.log(`${row}`);})
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Horray, a day I could finish! ;) I've been quite as I'm still struggling to solve part 2 for both day 6 and day 7....taking a break from those for a few days! (So I say, anyways lol)
JavaScript solution below - I used the Chalk library to colorize my text so the image was easier to read, and grabbed a screenshot. A little sad my image didn't spell out anything obvious, like I saw some other people's on the subreddit! My little terminal snowman makes me happy though. lol :)
Also certain this could have been done in fewer passes through the layers, but it was still very fast so not concerned about it.