From d3a7237b256880cd94cd16ac56ec90f41c4d7e11 Mon Sep 17 00:00:00 2001 From: amclin Date: Fri, 2 Dec 2022 06:44:42 +0000 Subject: [PATCH 01/11] docs: Update badges [skip ci] --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index af406bc..7eb4302 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,9 @@ ## Status ### 2021 -![](https://img.shields.io/badge/day%20📅-25-blue) -![](https://img.shields.io/badge/stars%20⭐-20-yellow) -![](https://img.shields.io/badge/days%20completed-10-red) +![](https://img.shields.io/badge/day%20📅-2-blue) +![](https://img.shields.io/badge/stars%20⭐-2-yellow) +![](https://img.shields.io/badge/days%20completed-1-red) ## Run the currently configured default day `npm start` From fe6691a4f3354a651533b6b61b28f364131fc731 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Thu, 1 Dec 2022 22:50:57 -0800 Subject: [PATCH 02/11] ci: bump version of checkout actions --- .github/workflows/release.yml | 2 +- .github/workflows/run-tests.yml | 2 +- .github/workflows/update-badges.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 57fb614..b3f385d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -18,7 +18,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - uses: actions/setup-node@v2 with: node-version: 16 diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index d8370e5..e0f78d1 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -20,7 +20,7 @@ jobs: # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v2 with: diff --git a/.github/workflows/update-badges.yml b/.github/workflows/update-badges.yml index eadc517..bc3a265 100644 --- a/.github/workflows/update-badges.yml +++ b/.github/workflows/update-badges.yml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 # clones your repo, make sure the ssh secret is set! + - uses: actions/checkout@v3 # clones your repo, make sure the ssh secret is set! with: ref: ${{ github.head_ref }} token: ${{ secrets.GH_PAT }} # token-based checkout is required because of branch protections on master From 033077fe543297877f5c55934869063bac233e37 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Thu, 1 Dec 2022 22:52:26 -0800 Subject: [PATCH 03/11] docs: update badge status title --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7eb4302..ab00339 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ## Status -### 2021 +### 2022 ![](https://img.shields.io/badge/day%20📅-2-blue) ![](https://img.shields.io/badge/stars%20⭐-2-yellow) ![](https://img.shields.io/badge/days%20completed-1-red) From d13ae6d04c3b8eae0f48b7661a0bae474d0e90e5 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 2 Dec 2022 00:00:23 -0800 Subject: [PATCH 04/11] feat(2022-day-02): score rochambeau games --- 2022/day-02/index.js | 3 ++ 2022/day-02/rochambeau.js | 53 ++++++++++++++++++++++++++++++++++ 2022/day-02/rochambeau.test.js | 26 +++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 2022/day-02/index.js create mode 100644 2022/day-02/rochambeau.js create mode 100644 2022/day-02/rochambeau.test.js diff --git a/2022/day-02/index.js b/2022/day-02/index.js new file mode 100644 index 0000000..af7e035 --- /dev/null +++ b/2022/day-02/index.js @@ -0,0 +1,3 @@ +// eslint-disable-next-line no-unused-vars +const console = require('../helpers') +require('./solution') diff --git a/2022/day-02/rochambeau.js b/2022/day-02/rochambeau.js new file mode 100644 index 0000000..f51804d --- /dev/null +++ b/2022/day-02/rochambeau.js @@ -0,0 +1,53 @@ + +// Lookup tables for possible rock / paper / scissor values +const selfCodes = ['X', 'Y', 'Z'] +const opponentCodes = ['A', 'B', 'C'] + +const scoreRound = (opponent, self) => { + const scoreShape = (self) => { + return selfCodes.indexOf(self) + 1 + } + + const scoreOutcome = (opponent, self) => { + const selfScore = selfCodes.indexOf(self) + const oppScore = opponentCodes.indexOf(opponent) + // Win + if ( + (selfScore - 1 === oppScore) || + (selfScore === -1 && oppScore === 2) + ) { + return 6 + } + // Lose + if ( + (oppScore - 1 === selfScore) || + (oppScore === -1 && selfScore === 2) + ) { + return 0 + } + // Draw + if (selfCodes.indexOf(self) === opponentCodes.indexOf(opponent)) { + return 3 + } + + throw new Error(`Could not calculate the results of the match: ${opponent}, ${self}`) + } + + return scoreShape(self) + scoreOutcome(opponent, self) +} + +/** + * Tallies the results of all rounds in a match + * @param {*} guide + * @returns + */ +const scoreMatch = (guide) => { + return guide.map((match) => { + return scoreRound(...match) + }).reduce((sum, value) => sum + value, 0) +} + +module.exports = { + scoreMatch, + scoreRound +} diff --git a/2022/day-02/rochambeau.test.js b/2022/day-02/rochambeau.test.js new file mode 100644 index 0000000..b414580 --- /dev/null +++ b/2022/day-02/rochambeau.test.js @@ -0,0 +1,26 @@ +/* eslint-env mocha */ +const { expect } = require('chai') +const { scoreMatch, scoreRound } = require('./rochambeau') + +describe.only('--- Day 2: Rock Paper Scissors ---', () => { + describe('Part 1', () => { + describe('scoreRound', () => { + it('calculates the score of a round based on what the opponent played and what you played', () => { + expect(scoreRound('A', 'Y')).to.equal(8) + expect(scoreRound('B', 'X')).to.equal(1) + expect(scoreRound('C', 'Z')).to.equal(6) + }) + }) + describe('scoreMatch', () => { + it('calculates the total score of a match', () => { + expect( + scoreMatch([ + ['A', 'Y'], + ['B', 'X'], + ['C', 'Z'] + ]) + ).to.equal(15) + }) + }) + }) +}) From 5b58961d41e10fbdb02126a6aa38aaeb027a257f Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 2 Dec 2022 00:19:58 -0800 Subject: [PATCH 05/11] fix(2022-day-02): correctly register loss when opponent plays rock against scissors --- 2022/day-02/rochambeau.js | 4 ++-- 2022/day-02/rochambeau.test.js | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/2022/day-02/rochambeau.js b/2022/day-02/rochambeau.js index f51804d..a485bff 100644 --- a/2022/day-02/rochambeau.js +++ b/2022/day-02/rochambeau.js @@ -14,14 +14,14 @@ const scoreRound = (opponent, self) => { // Win if ( (selfScore - 1 === oppScore) || - (selfScore === -1 && oppScore === 2) + (selfScore === 0 && oppScore === 2) ) { return 6 } // Lose if ( (oppScore - 1 === selfScore) || - (oppScore === -1 && selfScore === 2) + (oppScore === 0 && selfScore === 2) ) { return 0 } diff --git a/2022/day-02/rochambeau.test.js b/2022/day-02/rochambeau.test.js index b414580..69197be 100644 --- a/2022/day-02/rochambeau.test.js +++ b/2022/day-02/rochambeau.test.js @@ -2,13 +2,16 @@ const { expect } = require('chai') const { scoreMatch, scoreRound } = require('./rochambeau') -describe.only('--- Day 2: Rock Paper Scissors ---', () => { +describe('--- Day 2: Rock Paper Scissors ---', () => { describe('Part 1', () => { describe('scoreRound', () => { it('calculates the score of a round based on what the opponent played and what you played', () => { + // provided expect(scoreRound('A', 'Y')).to.equal(8) expect(scoreRound('B', 'X')).to.equal(1) expect(scoreRound('C', 'Z')).to.equal(6) + // data from input + expect(scoreRound('A', 'Z')).to.equal(3) // loss and self played Z }) }) describe('scoreMatch', () => { From f3b28ad04b5d402e06e901192d705ef915bfb57b Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 2 Dec 2022 00:20:26 -0800 Subject: [PATCH 06/11] feat(2022-day-02): solution for part 1 --- 2022/day-02/input.txt | 2500 +++++++++++++++++++++++++++++++++++++++ 2022/day-02/solution.js | 35 + index.js | 2 +- 3 files changed, 2536 insertions(+), 1 deletion(-) create mode 100644 2022/day-02/input.txt create mode 100644 2022/day-02/solution.js diff --git a/2022/day-02/input.txt b/2022/day-02/input.txt new file mode 100644 index 0000000..d7d6a83 --- /dev/null +++ b/2022/day-02/input.txt @@ -0,0 +1,2500 @@ +B Z +B Z +C Z +C Z +B X +C Y +A Y +B Z +A Z +A Y +C Z +C X +C Z +C X +B X +B Z +B Z +A Z +C Z +C Z +C Y +C Z +B Z +B Z +A X +A Y +B Z +B Z +C Z +B Z +B Z +C X +C Z +A Y +A X +A X +C Z +C Z +B X +C X +B Z +A Z +B X +B Z +B Z +A Z +B Z +A Y +C Z +C Z +B X +C Z +A Z +C Z +C Z +C Z +C Z +A Z +B Z +C Z +A Z +C Z +C Z +C Y +A Y +C X +C Z +C Z +C Z +C Z +A Y +C X +A Y +A Y +C X +B Z +A Y +C Y +C Z +B Z +C Z +B Z +B Z +B Z +C Y +B Z +A Y +C Z +C Z +A X +C X +C Z +A X +C Z +C Z +C X +A Z +C Z +C Z +B Z +C Y +A Y +C Y +C Y +A Z +B Z +C Z +C Z +C Z +A Z +C Z +B X +C Z +A Y +C Z +C X +C Y +C Y +B Z +C Z +C Z +A X +C X +C Z +C Z +A Z +B Z +B Z +C X +C Z +B Z +C X +B Z +C Z +B Z +B Z +B Z +B Z +C Y +C Z +B Z +C Z +C Z +C Z +C Z +B Z +C Z +B Z +C Z +B Z +C Z +C Y +C Z +C Z +C X +C Z +C Z +C Y +C Z +C Y +C X +B Z +B Z +C Z +A Z +C Z +C Z +C Z +B Z +C Z +B Z +C Z +B Z +C Z +C Z +B X +B Z +C Z +C Z +A Z +A Z +B Z +C Z +C Z +A Z +B X +C X +A Z +C Z +C Z +B Y +B Z +B Z +A Y +B Z +C Z +C Z +A Y +B Z +C Y +A Z +C X +A Z +A Y +C X +C Y +C Z +B Z +C Z +A Y +B Z +B Z +C Z +C Z +C Z +C Z +C Z +B Z +C Z +A X +C Z +C Z +A Y +C Z +A Y +C Z +B Z +C Z +A Y +C Z +C Y +C Z +B Z +C X +A Y +C X +A Y +B X +C Y +B X +C Z +C Y +C Z +C Y +C Y +B Z +C Z +A Y +C X +C Z +C Z +B Z +A Z +C Z +C Z +C Y +C Z +C Y +B X +C Z +C Z +C Z +C X +A Y +B Z +C Z +C Z +C Z +B Z +A Y +A Z +A Y +B Z +A Z +A Y +B Z +C Z +A Y +A Z +B Z +B Z +B Z +C Z +C Z +C Z +C Z +C Z +C Z +B Z +C Z +C Z +C Z +A Z +A Y +C X +C Z +A Z +C Z +A Y +C Z +A Y +C Z +C Z +A X +C Y +C Z +A X +C X +A X +B Z +C Z +A X +C Z +B Z +A X +C Z +A Y +A Z +C Y +A Y +C Y +A Z +B Z +A Z +B Z +C X +C X +C Z +C Z +B Z +A Y +C Z +A Y +C Z +B Z +B X +B X +B X +A Z +B Z +C X +C Z +A Y +C Z +B X +A Y +C X +B Z +C Z +C Z +C Z +C Z +A Z +B Z +B Z +B X +A Y +C Z +C X +A Y +C Z +C Z +C Z +A Y +A X +B Z +C Z +A Z +C Y +A Z +B Z +A Y +C Z +C Z +B Z +A Y +B Z +A Z +A Y +C Z +A X +C Z +C Z +B Z +C Z +C X +A Y +A Y +B Z +C Z +B X +A Y +C Z +C Z +A X +A Z +C Z +C Z +A Y +C Z +C Y +A Y +A Y +B Z +C Z +C Z +C X +C Z +A Y +B Z +C Z +C Z +A Y +B Z +C Z +C Z +B Z +A Y +B Z +C Z +C Z +C Z +C X +C Z +C Z +C Z +C Y +C X +C Y +C Z +B Z +A Y +C X +B Z +C X +C X +C Z +A Z +C Z +B X +A Y +C Z +B Z +A X +B X +C X +C Z +C Z +B Z +B Z +C Z +C Z +A Y +B Z +A Z +B Z +A Z +A X +C Z +A Z +A Z +B X +A Z +B Z +B Z +A Y +C Y +B Z +B Z +B Z +B Z +C Z +B Z +C Z +B Z +C Y +C Z +C Z +C X +C Z +C Y +C Y +C X +A Y +C Y +B Z +C Z +A Y +A Z +B X +C Z +C Z +A Y +A Y +A Y +C Z +A Z +C Z +B Y +A X +A Y +C Z +A X +B Z +C Z +C Z +B Z +C Z +A Z +C X +C Z +B Z +B Z +C Z +B Z +A Z +C Z +C Z +A Y +B Z +A Y +B Z +C Z +B Z +C Z +B Z +B Z +A Y +C Y +B Z +C Z +A Z +B Z +C Z +C Z +A Z +B Z +C Z +A Y +A Z +C X +B Z +A Y +A Y +C Z +A Z +B Z +B Z +C Z +C Z +C Z +C Z +A Y +A X +C Z +B Z +C Z +C Z +C Y +C Z +A X +A Z +C Z +B X +A Z +B X +B X +A Y +A Y +A Y +C Z +C Z +C Z +A Z +C X +A Y +C Z +B Z +C Z +A X +B Z +A X +A Z +C Z +C Z +C Z +B X +B Z +C X +B Z +C Z +C Z +C Z +A Y +B Z +B Z +C Z +A X +B Z +A Y +C Z +A Y +A Y +C Z +C Z +A Y +C X +C Z +A Y +C Z +B Z +B Z +C Z +C Y +B Z +B Z +C Z +A Y +C Z +B Z +B X +A Y +A Z +C Z +B Z +C Z +C X +A Y +A Z +A Y +B Z +A Z +B Z +A Y +A Y +B Z +C Z +C Z +C Z +A Y +A X +B Z +A Y +C Z +B X +C Z +C Z +A Y +C Z +C Z +C Y +C Z +C Y +B Z +B Z +C Z +C Z +B Z +B Z +B Z +C Z +B Z +B Z +C Z +C Z +C X +C Z +C Z +B Z +B Z +C X +A Y +C Y +C X +B Z +C X +B Z +C Z +A Z +C Y +C Z +C X +B X +A Y +C Y +A X +C Z +C Z +C Z +C Z +A Y +C Y +C Z +A Y +C Z +C Z +B Z +B Z +B Z +C Z +C Z +C Z +C Z +C Z +C Z +B Z +C Z +B Z +C Z +B Z +C Z +C Z +B Z +C Z +C Z +C Z +A Y +C Z +C Z +A X +C Z +C Z +A X +B Z +C Z +A Y +C X +C Z +A Z +C Y +A Y +C Z +B Z +C Z +B Z +C X +C Y +B Z +C Z +C Z +A Y +C X +C Z +C Z +B X +C Z +C Z +C Z +C Z +A Z +A Y +C Z +B Z +C Z +B Z +A Y +B Z +C Z +B Z +B Y +C Z +B Z +A Y +C Z +C Y +C Z +A X +B Z +C Z +A Z +A Z +B Z +A X +B Z +B Z +C Y +C Z +C Z +B Y +B Z +C Z +A Y +B X +B Z +C Z +C Z +C Z +B X +A Y +A Y +B Z +B Z +C Z +B Z +C Z +C Y +B Z +B Z +B X +A Y +A Y +A Y +B X +C Z +C Z +B Z +C Z +C X +B Z +C Z +C Z +B Z +C Z +C Z +A Y +B Z +A Y +C Y +B Z +B Z +B Z +C X +C Z +C Z +C Z +C X +C Z +A X +B Z +C Z +C Z +A Y +B Z +C Z +A Y +C Z +B Z +C Z +C Z +B Z +B Z +C Z +C Z +C Y +C Z +C Y +C X +C Z +C Y +C Z +A Y +B Z +B Z +A Y +B Z +B X +B X +A Z +C Y +C Z +C Z +B Z +C Z +C Z +C Z +C Z +C Y +C Z +B Z +C Z +C Z +C Z +B Z +B Z +C Z +B X +C Y +A X +C Z +A Z +C Y +C Z +C Z +B Z +C Z +C Z +C Z +B Z +A Y +A Z +B X +C X +A Y +A Y +C Z +C Z +C Z +C Z +C Z +C Z +C Y +C Z +C Z +C Z +A Y +A Z +C Z +A Y +C Z +C Z +B Z +B Z +B X +A Y +B Z +C Y +C Z +B Z +A Z +A Y +A X +C Z +B Z +C Z +B Z +B Z +C Z +C Z +C Z +C Z +A Y +A Z +C Z +C Z +A Y +C Y +C Z +A X +B X +B X +C Z +C Z +B Z +A Y +A Y +A Y +A X +C Z +B Z +B Z +B Z +A Y +C Y +B Z +A Z +C Z +B X +C Z +C Z +C X +C X +C Z +A Y +B Z +C Z +A Y +C Z +A X +C Z +B Z +B Z +B Z +C Z +B Z +C Z +A Z +C Z +B Z +C Z +A Y +C Z +A Z +C Z +C Y +B Z +A Z +C Z +B Y +A Z +C Z +A X +C Z +B Z +A X +C Z +B Z +C Z +C X +A Y +C Z +B Z +A X +C Z +B Z +C Z +A Y +C Z +C Z +C Z +B Z +C Y +C Z +C X +A Z +C Z +C Z +B Z +C Z +C Z +C Z +A Z +C Z +A Y +A X +C Z +A Y +C X +C Z +B Z +C Z +B Z +B Z +A Y +B Z +A Y +A Y +C X +B Z +C Z +C Z +C Z +C Z +C Z +A Y +B Z +C Z +A Y +B Z +C Z +C Z +A Y +B Z +C Z +C Y +B Z +C Z +B Z +A Z +C X +B Z +C Y +C Z +C Z +C X +C X +A X +C Z +B Z +A Z +C Z +B Z +B X +C Z +C Z +C Z +C Z +B Z +B X +C Z +C Z +C Z +B Z +C Y +B Z +B X +C Y +C Z +C Z +B Z +A Z +C Y +C X +C Z +B Z +C Z +C Z +C Z +B Z +B X +C Z +C Z +A Y +C Z +C Y +A Y +A Z +B Y +C Z +A Y +C X +C X +C X +C Z +B Z +C Z +C Z +B Z +C Z +A Y +C Z +C Z +B Z +C X +C Z +B Z +C Z +A Y +C Z +C Z +C X +C Z +B Z +C Y +C Z +A Z +C Z +B Z +A Z +B Z +C Z +A Y +B Z +C Z +C Z +B Z +A Z +C Z +C X +C Z +C Z +A Y +A Y +A Z +C Z +A Y +A X +C Z +C Z +C Z +C X +A Z +C Z +A Y +C Z +C Z +C Y +C Z +C Z +B Z +C Z +C Z +B Z +B Z +C Z +C Z +C Z +B Z +C Z +C Z +C Y +B Z +C Y +B Z +B X +C Z +C Z +A Y +B Z +C Y +B Z +B Z +B Z +B Z +C Z +C Y +A X +C Z +C Z +C Z +C Z +C Z +B Z +C Z +A Y +C Z +C Z +A Y +A Y +C Z +C Z +B Z +B Z +C X +C Z +C Z +A X +C Z +C Y +C Z +B Z +C Z +A Y +C Z +C Z +C Z +C X +A Y +C Z +C X +B Z +C Z +C Z +C Y +C Z +A Y +B Z +B Z +A X +C Y +C Z +B X +A X +C Z +C Z +C Z +C Z +C Y +A X +B Z +C Z +C Z +A Y +B X +A Z +A Y +B Z +C Z +C X +A X +C X +C Y +C Z +B X +A X +C Z +C Z +B X +A Y +A Y +A Y +C Z +C Z +A Y +C Z +A Z +A Y +C Z +B X +C Z +C Z +C X +C Z +C Z +A Z +B Z +A Z +C Y +C Y +C Y +C Z +C X +C Z +A Y +B Z +C Z +B Z +C Z +A Y +C X +C Z +C Z +B Z +C Z +C Y +A Y +B Z +B Z +B X +B Z +C Z +A Y +C Z +B Z +A Y +C Z +C Z +B Z +A Y +C Z +C Z +C Z +C Z +A Z +A Y +C Z +C X +A Z +A Z +C Z +C Z +C Z +A Y +B Z +B Z +B Z +B Z +C Z +C X +C Z +C Z +B Z +B Z +C Z +C X +A Z +A Y +C Z +B Z +C X +C X +A Y +A Y +C Z +B X +C Z +C Y +C X +A Y +C Y +C Z +C Z +B Z +A Z +C Z +C Z +C Z +C X +C Z +B Z +A Y +C Z +B Z +C Z +A Y +A Y +B X +B X +B Z +C Z +C Z +C Z +C Z +C X +C Z +B Z +C X +C X +A Z +B Y +C Z +C Z +C X +B Z +C Z +C Z +A Z +C Z +C X +A Y +A Z +C Z +B Z +C Z +B X +A Y +C Z +C Z +B Z +C X +B Z +B Z +C Z +C Z +C Z +B Z +A Z +C Z +A X +C Z +C Z +C Z +C X +C X +B Z +C Y +C Z +C Y +A Z +A Y +A X +B Z +A Y +C Z +C Y +B Z +B Z +B Z +B Y +B Z +A X +C Z +A Y +B Z +A Y +C Z +C Z +C Z +A Y +C Z +B Z +C Z +A Z +C Y +C X +A Y +A Y +A Y +B Z +C Y +C Z +C Z +C X +B Z +A Y +C Z +C Z +B Z +A Z +C Z +B Z +C X +C Z +B Z +C Z +C Z +B Y +B Z +C Y +B Z +A X +C Y +B Z +C Z +A Y +C Z +A Z +C Z +B Z +A X +C Y +C Y +A Y +A Z +B Z +A Z +C Z +C Y +C X +C Z +A Y +C Z +A Y +C Z +C Z +C Z +C Y +C X +C X +B Z +A Y +C Z +A X +C Z +A Z +C X +A X +A Z +A Y +C Z +C Z +C Z +B Z +B Z +C Z +C Z +C Z +A Y +C Z +A Z +C Z +C Z +C X +C X +C Z +C Z +C Z +C Z +C Z +A Y +B Z +B Z +C Z +B X +C Y +C Z +C Z +C Z +A Y +B X +C Z +C X +B Z +A Z +B X +C Z +C Z +B Z +C X +B Z +C Z +A Y +C X +A Y +A Z +C Y +C Z +C Y +C Z +C X +C Z +C Z +C Z +C Z +C Z +C Z +B Z +C Z +B Z +C X +C Z +C X +B Z +A Z +A X +C Z +A Y +C Y +C Z +C Z +C Z +B Z +B Z +A X +C Z +B Z +C Z +C Z +A X +C Z +B Z +C X +C Z +C Z +C Z +C Z +B X +C X +B Z +C Z +B Z +A Y +C Z +A Y +B X +C Z +C Z +C Z +C Z +A Y +C Z +C Z +B Z +C Z +C Z +B Z +A Y +C X +C Z +C Z +A Y +C X +B Z +B Z +A Y +C Z +B Z +C Z +A Y +B Z +A Z +C Z +B Z +C Z +B Z +C X +B Z +C Z +C Z +C Z +A Y +A Y +C Z +B Z +A Y +B X +C Z +B Z +C X +C Z +B Z +B Z +C Y +C Y +A Z +C X +C Z +B Z +A Y +C Z +C Z +B Z +A Y +B Z +A Z +C Z +B Z +B Z +B Z +C Z +C X +B Z +C Z +B Z +C Z +C Z +A Y +A Y +C Z +C X +A Y +B Z +C Z +A Z +C Z +B Z +A Y +C Z +C X +C Z +C Z +C Z +C Z +A Y +C Z +C X +B Z +B X +C Z +A Y +C Y +C Z +C Z +C X +C Z +C X +C X +C Z +B Z +B X +A Z +A Z +C Z +C Y +A Y +C Z +A Y +C Z +C Z +C Z +B Z +B Z +C Z +B Z +C Z +C Z +C Y +B Z +B Z +C Z +C Z +B Z +C X +A Z +C Z +C Z +A Y +A Y +A Y +C Z +C Z +B Z +A Z +A X +A Y +C X +C Z +B Z +C X +C X +B Z +B Z +B X +C Z +C Y +B Y +C Z +C Z +C Z +B Y +A Y +A Y +C Z +C Z +C Z +C Z +C Z +C Z +C Z +C Z +B X +A Z +A X +C Z +A Y +A Z +C X +C Z +C Y +C Z +C Z +C Z +B Z +B Z +A Y +A Z +C X +B X +B X +C Z +C Y +C Z +C Z +C Y +C Z +C Z +A Z +B Z +C Z +A Y +C Z +C Z +C Z +A Y +C Z +B Y +B X +C Z +A Y +B X +C Y +A Y +C Z +C Z +C Y +A Z +B Z +C Z +C Z +B Z +C Y +C X +C Y +C Y +B Z +C Z +B Z +C Z +A Y +A Y +A Y +B Z +C Z +A Z +C Z +C Y +B Z +B Z +C Y +C Z +C Z +A Y +C Z +C Z +B Z +C Z +C X +A X +A Y +A Y +B Z +C Z +B X +A Y +B Z +C Z +C Z +B Z +A Y +C Z +C Z +B X +A Y +C Z +B X +A Y +C Z +C X +A Z +B Z +B Z +C Z +C X +A Y +C Z +C Z +A Y +B Z +C Z +A Y +C Z +A Y +C Z +B Z +C Z +A X +B Z +C Z +C Z +C X +B Y +A Y +A X +C X +B Z +C Z +C X +B Z +C Z +C Z +C Z +C Z +A Z +C Z +A Y +B X +C Z +C Z +B Z +B Z +C X +B Z +B X +C Z +C Z +C Z +A Y +C Z +C Z +C Z +C Z +A Y +A Z +B Z +C Y +A Z +C Z +C Z +C Z +B X +B Z +C Z +B Z +C Z +C X +C Z +A X +C Z +C Z +C Z +C Z +C Z +A Y +A Y +B Z +B Z +B Z +B Z +C Z +C Z +C Z +C Z +A Z +A Y +C Z +A Z +A Y +C Z +C Z +C Y +A Y +B Z +C X +B Z +A Y +C Z +A Y +C Z +B Z +B Z +B Z +C Z +C Z +A Z +A Y +C X +A Y +C Z +A Y +C Z +C Z +A Y +B Z +C Z +C Y +C Z +B Z +B Z +B Z +C Y +C Z +B Z +C X +B Z +A Y +B Z +C Z +A Y +C Z +C Y +C Z +C Z +C Z +C Z +B X +B Z +C Z +C Z +A Y +C Y +C Z +C Z +C Z +C Z +C Z +C X +B Z +C X +A Y +B X +A Y +C Z +B Z +A Y +B Z +C Z +A Y +C Z +C Z +C X +C X +C Z +B Z +C X +C Z +A X +A Z +C Z +C Y +A Y +A X +B Z +C Z +C Z +B Z +A Y +C Z +A X +B Z +B Z +A X +C Y +C Z +A Y +B Z +C Z +C Z +C Z +B Z +C Z +B Z +C Z +B X +B Z +C Z +A X +C Z +A X +C Z +A Y +C Z +A Z +C Z +C Z +C Z +B X +A Y +A Y +A X +B Z +B Z +C Y +C Z +B Z +A X +C Z +A Y +C Z +C Z +B Z +C Z +C X +B X +B Z +B X +C Z +C Z +B Z +A Z +C Z +C Y +C Y +C Z +C Z +C Z +C Z +C Z +C Z +A X +A Z +C Z +B Z +B Z +B Z +C X +A Z +C Z +C Z +B Z +B Z +C Z +B X +C Z +B Z +C Z +C Z +C Z +C Z +A X +A Z +A Y +C Z +A Z +A Y +C Z +A Z +C Z +A Y +C Z +B Z +C Z +A Y +C Y +A Y +B Z +A Y +B Z +C Z +B X +C X +C Z +B Z +A Z +C Z +B X +C Y +C Z +C Y +A X +C X +C X +B Z +A Y +C Z +A Z +B Z +C Z +C X +C Z +C Z +C Z +C Z +C Z +B Z +B Z +C Z +C Z +C Z +C Z +C X +A Z +C Z +A Y +A Y +C Z +C Z +C Y +C Z +A Y +C Z +B Z +B X +A Z +A Y +A Y +C Z +A Y +C Z +A X +A Y +B X +C Z +C Z +A Z +C Z +A Y +B Z +A Y +C Z +C Z +B Z +C X +C Z +C Z +C Z +C Y +C Z +B X +C Z +C Z +B X +A Z +C Z +C X +C X +B Z +C Z +C Z +B Z +C Z +C Z +B Z +B Z +B Z +B Z +B Z +A Z +C X +C Y +C Z +A Y +C Z +B Z +A Y +C Z +C Z +B Z +B Z +C Z +A Z +A X +A Y +C Z +A Z +C Z +C X +B Z +C Z +A X +B Z +B X +C Z +C Z +C Z +B X +C Z +B Z +B Z +A Y +A Y +A Z +C Z +C Z +C Z +B Z +C Z +B Z +C Z +A Z +C Z +C X +C Z +C Z +C Z +B Z +A Y +C Y +C Z +C Z +C Z +C Z +B Z +C Y +C Z +A Z +C Y +C Z +B X +A Y +A Y +A X +B Z +B Z +C Z +A Y +B X +A Y +C X +C Z +A Y +C X +C Z +C Z +A Z +C X +C Z +C X +C Z +A Y +C Z +C Z +B Z +C Z +C Y +A Z +C X +C Y +C Z +C Z +C Y +B Z +B Z +C X +C Z +C Z +C Z +C Z +B Z +C Z +C Z +C Z +B Y +C Y +C Z +B Z +C Z +C Z +C Y +B Z +A Y +A Z +C Y +C Y +C Z +C X +C Z +A Z +C Z +C Z +A Y +A Z +C Z +A Z +C Z +B Z +B X +B Z +B Z +A Z +C Z +A Y +C Z +C Y +C Z +A Z +B Z +B Z +B Z +C Z +B Z +A Z +A Y +C Z +C Z +C Z +C Z +C Z +A Z +C Z +A Z +A X +B Z +A Y +C X +A Z +B X +B Y +A Y +B Z +A Z +B Z +B Z +C Z +A Z +B Z +B Z +A Y +C Y +C Y +A X +C Z +C Z +C Z +C X +C Z +C Z +C Z +C Z +B Z +A Y +C Z +C Z +C Z +A Z +C X +C Z +B Z +C Y +B X +B Y +C Z +C Z +A X +C Y +C X +B Z +B Z +C Z +B Z +C Y +C Y +C Y +C X +C Z +C Z +C Z +C X +A Y +C Z +C Z +B Z +A Z +C Z +A Y +B X +C Z +C X +C Z +A Z +B Z +A Y +C Z +C Z +C Z +A Z +C Z +C Z +C X +C Z +C Z +B Z +C Z +B Z +C X +C Z +A Y +A Y +C Z +C Z +B Z +C Z +C Z +C X +C Z +A Y +C Z +C Z +C Z +B Z +C Z +A Y diff --git a/2022/day-02/solution.js b/2022/day-02/solution.js new file mode 100644 index 0000000..c7f6603 --- /dev/null +++ b/2022/day-02/solution.js @@ -0,0 +1,35 @@ +const fs = require('fs') +const path = require('path') +const filePath = path.join(__dirname, 'input.txt') +const { inputToArray, linesToArray } = require('../../2018/inputParser') +const { scoreMatch } = require('./rochambeau') + +fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => { + if (err) throw err + + initData = linesToArray(initData.trim()).map(inputToArray) + + const resetInput = () => { + // Deep copy to ensure we aren't mutating the original data + return JSON.parse(JSON.stringify(initData)) + } + + const part1 = () => { + const data = resetInput() + return scoreMatch(data) + } + + const part2 = () => { + const data = resetInput() + console.debug(data) + return 'No answer yet' + } + const answers = [] + answers.push(part1()) + answers.push(part2()) + + answers.forEach((ans, idx) => { + console.info(`-- Part ${idx + 1} --`) + console.info(`Answer: ${ans}`) + }) +}) diff --git a/index.js b/index.js index e44e4ef..39627a9 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -require('./2022/day-01/solution') +require('./2022/day-02/solution') From 761c31d02f7261dc0024b3b4ed9afab9a6759069 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 2 Dec 2022 00:58:05 -0800 Subject: [PATCH 07/11] feat(2022-day-02): calculate rochambeau scoring based on strategy plays --- 2022/day-02/rochambeau.js | 27 ++++++++++++++++++++++++++- 2022/day-02/rochambeau.test.js | 24 ++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/2022/day-02/rochambeau.js b/2022/day-02/rochambeau.js index a485bff..a058fda 100644 --- a/2022/day-02/rochambeau.js +++ b/2022/day-02/rochambeau.js @@ -2,6 +2,7 @@ // Lookup tables for possible rock / paper / scissor values const selfCodes = ['X', 'Y', 'Z'] const opponentCodes = ['A', 'B', 'C'] +const strategyCodes = selfCodes // Same list, as lose / draw / win const scoreRound = (opponent, self) => { const scoreShape = (self) => { @@ -36,6 +37,22 @@ const scoreRound = (opponent, self) => { return scoreShape(self) + scoreOutcome(opponent, self) } +const strategizeRound = (opponent, outcome) => { + const scoreOutcome = 3 * strategyCodes.indexOf(outcome) + const scoreShape = (shape) => { + return opponentCodes.indexOf(shape) + 1 + } + + const findPlay = (opponent, outcome) => { + const offset = strategyCodes.indexOf(outcome) - 1 + let target = opponentCodes.indexOf(opponent) + offset + if (target >= opponentCodes.length) { target = 0 } + return opponentCodes[target] + } + console.debug(scoreShape(findPlay(opponent, outcome)), scoreOutcome) + return scoreShape(findPlay(opponent, outcome)) + scoreOutcome +} + /** * Tallies the results of all rounds in a match * @param {*} guide @@ -47,7 +64,15 @@ const scoreMatch = (guide) => { }).reduce((sum, value) => sum + value, 0) } +const strategizeMatch = (guide) => { + return guide.map((match) => { + return strategizeRound(...match) + }).reduce((sum, value) => sum + value, 0) +} + module.exports = { scoreMatch, - scoreRound + scoreRound, + strategizeMatch, + strategizeRound } diff --git a/2022/day-02/rochambeau.test.js b/2022/day-02/rochambeau.test.js index 69197be..dc1e0a2 100644 --- a/2022/day-02/rochambeau.test.js +++ b/2022/day-02/rochambeau.test.js @@ -1,8 +1,8 @@ /* eslint-env mocha */ const { expect } = require('chai') -const { scoreMatch, scoreRound } = require('./rochambeau') +const { scoreMatch, scoreRound, strategizeMatch, strategizeRound } = require('./rochambeau') -describe('--- Day 2: Rock Paper Scissors ---', () => { +describe.only('--- Day 2: Rock Paper Scissors ---', () => { describe('Part 1', () => { describe('scoreRound', () => { it('calculates the score of a round based on what the opponent played and what you played', () => { @@ -26,4 +26,24 @@ describe('--- Day 2: Rock Paper Scissors ---', () => { }) }) }) + describe('Part 2', () => { + describe('strategizeRound()', () => { + it('calculates the score of a round based on what the opponent played and the outcome you should achieve', () => { + expect(strategizeRound('A', 'Y')).to.equal(4) + expect(strategizeRound('B', 'X')).to.equal(1) + expect(strategizeRound('C', 'Z')).to.equal(7) + }) + }) + describe('strategizeMatch', () => { + it('calculates the total score of a strategized match', () => { + expect( + strategizeMatch([ + ['A', 'Y'], + ['B', 'X'], + ['C', 'Z'] + ]) + ).to.equal(12) + }) + }) + }) }) From d22be13b74b486c07744decb410f93e9950fd711 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 2 Dec 2022 01:08:43 -0800 Subject: [PATCH 08/11] fix(2022-day-02): record correct score when intending to lose against an opponent playing rock --- 2022/day-02/rochambeau.js | 2 +- 2022/day-02/rochambeau.test.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/2022/day-02/rochambeau.js b/2022/day-02/rochambeau.js index a058fda..95b27c4 100644 --- a/2022/day-02/rochambeau.js +++ b/2022/day-02/rochambeau.js @@ -47,9 +47,9 @@ const strategizeRound = (opponent, outcome) => { const offset = strategyCodes.indexOf(outcome) - 1 let target = opponentCodes.indexOf(opponent) + offset if (target >= opponentCodes.length) { target = 0 } + if (target < 0) { target = opponentCodes.length - 1} return opponentCodes[target] } - console.debug(scoreShape(findPlay(opponent, outcome)), scoreOutcome) return scoreShape(findPlay(opponent, outcome)) + scoreOutcome } diff --git a/2022/day-02/rochambeau.test.js b/2022/day-02/rochambeau.test.js index dc1e0a2..b6f8c4e 100644 --- a/2022/day-02/rochambeau.test.js +++ b/2022/day-02/rochambeau.test.js @@ -2,7 +2,7 @@ const { expect } = require('chai') const { scoreMatch, scoreRound, strategizeMatch, strategizeRound } = require('./rochambeau') -describe.only('--- Day 2: Rock Paper Scissors ---', () => { +describe('--- Day 2: Rock Paper Scissors ---', () => { describe('Part 1', () => { describe('scoreRound', () => { it('calculates the score of a round based on what the opponent played and what you played', () => { @@ -32,6 +32,8 @@ describe.only('--- Day 2: Rock Paper Scissors ---', () => { expect(strategizeRound('A', 'Y')).to.equal(4) expect(strategizeRound('B', 'X')).to.equal(1) expect(strategizeRound('C', 'Z')).to.equal(7) + // data from input + expect(scoreRound('A', 'X')).to.equal(3) // Opponent played Rock and self should lose }) }) describe('strategizeMatch', () => { From a01f2b4c050a08cb57c83dbacd9e2c677996ae25 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 2 Dec 2022 01:09:32 -0800 Subject: [PATCH 09/11] feat(2022-day-02): solution for part 2, calculating match score based on strategic win/loss guide --- 2022/day-02/solution.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/2022/day-02/solution.js b/2022/day-02/solution.js index c7f6603..d5f77b3 100644 --- a/2022/day-02/solution.js +++ b/2022/day-02/solution.js @@ -2,7 +2,7 @@ const fs = require('fs') const path = require('path') const filePath = path.join(__dirname, 'input.txt') const { inputToArray, linesToArray } = require('../../2018/inputParser') -const { scoreMatch } = require('./rochambeau') +const { scoreMatch, strategizeMatch } = require('./rochambeau') fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => { if (err) throw err @@ -21,8 +21,7 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => { const part2 = () => { const data = resetInput() - console.debug(data) - return 'No answer yet' + return strategizeMatch(data) } const answers = [] answers.push(part1()) From 5d0bf77b204ac16dd0a9b6cf795ea4939de4f747 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 2 Dec 2022 01:16:09 -0800 Subject: [PATCH 10/11] test(2022-day-02): validate for correct expect function call --- 2022/day-02/rochambeau.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2022/day-02/rochambeau.test.js b/2022/day-02/rochambeau.test.js index b6f8c4e..bf05bb5 100644 --- a/2022/day-02/rochambeau.test.js +++ b/2022/day-02/rochambeau.test.js @@ -33,7 +33,7 @@ describe('--- Day 2: Rock Paper Scissors ---', () => { expect(strategizeRound('B', 'X')).to.equal(1) expect(strategizeRound('C', 'Z')).to.equal(7) // data from input - expect(scoreRound('A', 'X')).to.equal(3) // Opponent played Rock and self should lose + expect(strategizeRound('A', 'X')).to.equal(3) // Opponent played Rock and self should lose }) }) describe('strategizeMatch', () => { From 85a3ad1d3a38a648c59a169f4e4574d7c7c1d6f8 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 2 Dec 2022 01:16:42 -0800 Subject: [PATCH 11/11] style: linting whitespace --- 2022/day-02/rochambeau.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2022/day-02/rochambeau.js b/2022/day-02/rochambeau.js index 95b27c4..a7d4f60 100644 --- a/2022/day-02/rochambeau.js +++ b/2022/day-02/rochambeau.js @@ -47,7 +47,7 @@ const strategizeRound = (opponent, outcome) => { const offset = strategyCodes.indexOf(outcome) - 1 let target = opponentCodes.indexOf(opponent) + offset if (target >= opponentCodes.length) { target = 0 } - if (target < 0) { target = opponentCodes.length - 1} + if (target < 0) { target = opponentCodes.length - 1 } return opponentCodes[target] } return scoreShape(findPlay(opponent, outcome)) + scoreOutcome