advent-of-code

Entries to advent of code, multiple years
git clone git://git.finwo.net/misc/advent-of-code
Log | Files | Refs

commit 958abb797bc364dd7ce30ad02cd6d3742bf79143
parent 9983339ef40f385a54274bf728c7a302066f39a5
Author: finwo <finwo@pm.me>
Date:   Fri,  2 Dec 2022 11:44:53 +0100

2022/02 solution

Diffstat:
A2022/day-02/index.js | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 88 insertions(+), 0 deletions(-)

diff --git a/2022/day-02/index.js b/2022/day-02/index.js @@ -0,0 +1,88 @@ +const fs = require('fs'); +const through = require('through2'); +const line_for_line = require('../common/line-for-line'); + +// Fully expecting other operations later, otherwise would've just kept a single counter +let score_1 = 0; +let score_2 = 0; + +const choices = [ + { + name : 'rock', + opp : 'A', + me : 'X', + points : 1, + map : 0, + Z : 6 + 2, + Y : 3 + 1, + X : 0 + 3, + }, + { + name : 'paper', + opp : 'B', + me : 'Y', + points : 2, + map : 1, + Z : 6 + 3, + Y : 3 + 2, + X : 0 + 1, + }, + { + name : 'scissors', + opp : 'C', + me : 'Z', + points : 3, + map : 2, + Z : 6 + 1, + Y : 3 + 3, + X : 0 + 2, + }, +]; + +fs.createReadStream('input') + .pipe(line_for_line()) + + // Business logic + .pipe(through(function(line, enc, cb) { + line = line.toString(); + + const [c_them, c_me] = line.split(' '); + const them = choices.find(choice => choice.opp == c_them); + const me = choices.find(choice => choice.me == c_me ); + + // Step 1, run the match + const match = them.map - me.map; + let result = ''; + switch(match) { + case -1: + case 2: + score_1 += 6 + me.points; + result = 'win'; + break; + case 0: + score_1 += 3 + me.points; + result = 'draw'; + break; + case 1: + case -2: + score_1 += 0 + me.points; + result = 'lose'; + break; + default: + throw new Error(`other: ${match}`); + break; + } + + // Step 2. chosen winner + + score_2 += them[c_me]; + + console.log({ them, me, match, result }); + cb(); + })) + + .on('finish', () => { + // done + console.log('Step 1:', score_1); + console.log('Step 2:', score_2); + })