From f3273983a827b01a12f1ddf6215f8d69938821eb Mon Sep 17 00:00:00 2001 From: Piotr Russ Date: Fri, 25 Dec 2020 01:02:32 +0100 Subject: day22 --- day22/day22-1.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 day22/day22-1.js (limited to 'day22/day22-1.js') 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) + -- cgit v1.2.3