Skip to content

Commit 761c31d

Browse files
feat(2022-day-02): calculate rochambeau scoring based on strategy plays
1 parent f3b28ad commit 761c31d

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

2022/day-02/rochambeau.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Lookup tables for possible rock / paper / scissor values
33
const selfCodes = ['X', 'Y', 'Z']
44
const opponentCodes = ['A', 'B', 'C']
5+
const strategyCodes = selfCodes // Same list, as lose / draw / win
56

67
const scoreRound = (opponent, self) => {
78
const scoreShape = (self) => {
@@ -36,6 +37,22 @@ const scoreRound = (opponent, self) => {
3637
return scoreShape(self) + scoreOutcome(opponent, self)
3738
}
3839

40+
const strategizeRound = (opponent, outcome) => {
41+
const scoreOutcome = 3 * strategyCodes.indexOf(outcome)
42+
const scoreShape = (shape) => {
43+
return opponentCodes.indexOf(shape) + 1
44+
}
45+
46+
const findPlay = (opponent, outcome) => {
47+
const offset = strategyCodes.indexOf(outcome) - 1
48+
let target = opponentCodes.indexOf(opponent) + offset
49+
if (target >= opponentCodes.length) { target = 0 }
50+
return opponentCodes[target]
51+
}
52+
console.debug(scoreShape(findPlay(opponent, outcome)), scoreOutcome)
53+
return scoreShape(findPlay(opponent, outcome)) + scoreOutcome
54+
}
55+
3956
/**
4057
* Tallies the results of all rounds in a match
4158
* @param {*} guide
@@ -47,7 +64,15 @@ const scoreMatch = (guide) => {
4764
}).reduce((sum, value) => sum + value, 0)
4865
}
4966

67+
const strategizeMatch = (guide) => {
68+
return guide.map((match) => {
69+
return strategizeRound(...match)
70+
}).reduce((sum, value) => sum + value, 0)
71+
}
72+
5073
module.exports = {
5174
scoreMatch,
52-
scoreRound
75+
scoreRound,
76+
strategizeMatch,
77+
strategizeRound
5378
}

2022/day-02/rochambeau.test.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
3-
const { scoreMatch, scoreRound } = require('./rochambeau')
3+
const { scoreMatch, scoreRound, strategizeMatch, strategizeRound } = require('./rochambeau')
44

5-
describe('--- Day 2: Rock Paper Scissors ---', () => {
5+
describe.only('--- Day 2: Rock Paper Scissors ---', () => {
66
describe('Part 1', () => {
77
describe('scoreRound', () => {
88
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 ---', () => {
2626
})
2727
})
2828
})
29+
describe('Part 2', () => {
30+
describe('strategizeRound()', () => {
31+
it('calculates the score of a round based on what the opponent played and the outcome you should achieve', () => {
32+
expect(strategizeRound('A', 'Y')).to.equal(4)
33+
expect(strategizeRound('B', 'X')).to.equal(1)
34+
expect(strategizeRound('C', 'Z')).to.equal(7)
35+
})
36+
})
37+
describe('strategizeMatch', () => {
38+
it('calculates the total score of a strategized match', () => {
39+
expect(
40+
strategizeMatch([
41+
['A', 'Y'],
42+
['B', 'X'],
43+
['C', 'Z']
44+
])
45+
).to.equal(12)
46+
})
47+
})
48+
})
2949
})

0 commit comments

Comments
 (0)