From f8b43904f3def50e3a0245e99c44618ce76ec8d6 Mon Sep 17 00:00:00 2001 From: x0x7 Date: Sat, 30 Nov 2024 00:06:12 -0500 Subject: [PATCH] Add 2023/Day2/part2.js --- 2023/Day2/part2.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 2023/Day2/part2.js diff --git a/2023/Day2/part2.js b/2023/Day2/part2.js new file mode 100644 index 0000000..dd10d75 --- /dev/null +++ b/2023/Day2/part2.js @@ -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); \ No newline at end of file