diff options
Diffstat (limited to 'day22/day22-1.js')
-rw-r--r-- | day22/day22-1.js | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/day22/day22-1.js b/day22/day22-1.js new file mode 100644 index 0000000..f2528f5 --- /dev/null +++ b/day22/day22-1.js @@ -0,0 +1,21 @@ +const data = require('./data') +const [player1, player2] = data.split('\n\n').filter(Boolean) + .map(p => p.split(/\n/g).filter(Boolean).slice(1)) + +const round = () => { + const c1 = player1.shift() + const c2 = player2.shift() + if (parseInt(c1) > parseInt(c2)) { + player1.push(c1, c2) + } else { + player2.push(c2, c1) + } +} + +while (player1.length>0 && player2.length>0) { round() } + +const cards = player1.length > 0 ? player1 : player2 +const score = cards.reverse().map((c, i) => parseInt(c) * (i+1)).reduce((a,b) => a + b, 0) + +console.log('Answer: ', score) + |