Skip to content

Commit d13ae6d

Browse files
feat(2022-day-02): score rochambeau games
1 parent 033077f commit d13ae6d

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

2022/day-02/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const console = require('../helpers')
3+
require('./solution')

2022/day-02/rochambeau.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
// Lookup tables for possible rock / paper / scissor values
3+
const selfCodes = ['X', 'Y', 'Z']
4+
const opponentCodes = ['A', 'B', 'C']
5+
6+
const scoreRound = (opponent, self) => {
7+
const scoreShape = (self) => {
8+
return selfCodes.indexOf(self) + 1
9+
}
10+
11+
const scoreOutcome = (opponent, self) => {
12+
const selfScore = selfCodes.indexOf(self)
13+
const oppScore = opponentCodes.indexOf(opponent)
14+
// Win
15+
if (
16+
(selfScore - 1 === oppScore) ||
17+
(selfScore === -1 && oppScore === 2)
18+
) {
19+
return 6
20+
}
21+
// Lose
22+
if (
23+
(oppScore - 1 === selfScore) ||
24+
(oppScore === -1 && selfScore === 2)
25+
) {
26+
return 0
27+
}
28+
// Draw
29+
if (selfCodes.indexOf(self) === opponentCodes.indexOf(opponent)) {
30+
return 3
31+
}
32+
33+
throw new Error(`Could not calculate the results of the match: ${opponent}, ${self}`)
34+
}
35+
36+
return scoreShape(self) + scoreOutcome(opponent, self)
37+
}
38+
39+
/**
40+
* Tallies the results of all rounds in a match
41+
* @param {*} guide
42+
* @returns
43+
*/
44+
const scoreMatch = (guide) => {
45+
return guide.map((match) => {
46+
return scoreRound(...match)
47+
}).reduce((sum, value) => sum + value, 0)
48+
}
49+
50+
module.exports = {
51+
scoreMatch,
52+
scoreRound
53+
}

2022/day-02/rochambeau.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { scoreMatch, scoreRound } = require('./rochambeau')
4+
5+
describe.only('--- Day 2: Rock Paper Scissors ---', () => {
6+
describe('Part 1', () => {
7+
describe('scoreRound', () => {
8+
it('calculates the score of a round based on what the opponent played and what you played', () => {
9+
expect(scoreRound('A', 'Y')).to.equal(8)
10+
expect(scoreRound('B', 'X')).to.equal(1)
11+
expect(scoreRound('C', 'Z')).to.equal(6)
12+
})
13+
})
14+
describe('scoreMatch', () => {
15+
it('calculates the total score of a match', () => {
16+
expect(
17+
scoreMatch([
18+
['A', 'Y'],
19+
['B', 'X'],
20+
['C', 'Z']
21+
])
22+
).to.equal(15)
23+
})
24+
})
25+
})
26+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy