-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.js
66 lines (56 loc) · 1.19 KB
/
part1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const { getInputString } = require('../utils/getInputString.js');
const file = getInputString(2, false);
const startingScores = {
X: 1,
Y: 2,
Z: 3
}
const outcomes = {
WIN: 6,
DRAW: 3
}
const elfHands = {
ROCK: "A",
PAPER: "B",
SCISSORS: "C"
};
const myHands = {
ROCK: "X",
PAPER: "Y",
SCISSORS: "Z"
}
const getScore = (elf, me) => {
let score = startingScores[me];
switch(elf) {
case elfHands.ROCK:
if(me === myHands.ROCK) {
score += outcomes.DRAW
} else if(me === myHands.PAPER) {
score += outcomes.WIN
}
break;
case elfHands.PAPER:
if(me === myHands.PAPER) {
score += outcomes.DRAW
} else if(me === myHands.SCISSORS) {
score += outcomes.WIN
}
break;
case elfHands.SCISSORS:
if(me === myHands.SCISSORS) {
score += outcomes.DRAW
} else if(me === myHands.ROCK) {
score += outcomes.WIN
}
break;
}
return score;
}
const rounds = file.split('\n');
const totalScore = rounds.reduce((acc, round) => {
const elfPlay = round.split(' ')[0]
const myPlay = round.split(' ')[1]
acc += getScore(elfPlay, myPlay)
return acc
}, 0);
console.log(totalScore)