-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.js
95 lines (82 loc) · 1.87 KB
/
part2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const { getInputString } = require('../utils/getInputString.js');
const file = getInputString(2, false);
const hands = {
ROCK: "A",
PAPER: "B",
SCISSORS: "C",
};
const outcomes = {
LOSE: "X",
DRAW: "Y",
WIN: "Z",
};
const outcomeScores = {
WIN: 6,
DRAW: 3,
};
const startingScores = {
[hands.ROCK]: 1,
[hands.PAPER]: 2,
[hands.SCISSORS]: 3,
};
const choosePlay = (elf, outcome) => {
switch (outcome) {
case outcomes.LOSE:
switch (elf) {
case hands.ROCK:
return hands.SCISSORS;
case hands.PAPER:
return hands.ROCK;
case hands.SCISSORS:
return hands.PAPER;
}
case outcomes.WIN:
switch (elf) {
case hands.ROCK:
return hands.PAPER;
case hands.PAPER:
return hands.SCISSORS;
case hands.SCISSORS:
return hands.ROCK;
}
case outcomes.DRAW:
return elf;
}
return score;
};
const getScore = (elf, me) => {
let score = startingScores[me];
switch (elf) {
case hands.ROCK:
if (me === hands.ROCK) {
score += outcomeScores.DRAW;
} else if (me === hands.PAPER) {
score += outcomeScores.WIN;
}
break;
case hands.PAPER:
if (me === hands.PAPER) {
score += outcomeScores.DRAW;
} else if (me === hands.SCISSORS) {
score += outcomeScores.WIN;
}
break;
case hands.SCISSORS:
if (me === hands.SCISSORS) {
score += outcomeScores.DRAW;
} else if (me === hands.ROCK) {
score += outcomeScores.WIN;
}
break;
}
return score;
};
const rounds = file.split("\n");
const totalScore = rounds.reduce((acc, round) => {
const elfPlay = round.split(" ")[0];
const outcome = round.split(" ")[1];
const myPlay = choosePlay(elfPlay, outcome);
acc += getScore(elfPlay, myPlay);
return acc;
}, 0);
console.log(totalScore);