Add 2023/Day2/part2.js

This commit is contained in:
x0x7 2024-11-30 00:06:12 -05:00
parent b8ca93c943
commit f8b43904f3
1 changed files with 26 additions and 0 deletions

26
2023/Day2/part2.js Normal file
View File

@ -0,0 +1,26 @@
#!/usr/bin/env node
var fs = require('fs');
var games = fs.readFileSync('input.txt','utf8').split('\n').filter(i=>i).map(i=>i.split(':').pop()).map(i=>i.split(';')).map(i=>i.map(i=>i.split(','))).map(i=>i.map(i=>i.map(i=>i.split(' ').filter(i=>i))));
var maxes = {
red:12,
green:13,
blue:14
};
//var sum=0;
//for(var gameid=1;gameid<=games.length;++gameid) {
// var game = games[gameid-1];
// var bad = game.some(sample=>sample.some(([count,color])=>count>maxes[color]));
// if(!bad) sum+=gameid;
//}
var allpower = games.reduce((acc,game)=>{
var base={red:0,blue:0,green:0};
game.forEach(sample=>sample.forEach(([count,color])=>base[color]=Math.max(base[color],count)));
return acc+base.red*base.blue*base.green;
},0);
console.log('Result:',allpower);