-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.js
114 lines (105 loc) · 3.07 KB
/
day2.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import {readFileForDay, splitBlocks, splitLines} from './helper.js';
let input = await readFileForDay(2);
const mappings = {
Rock: 'A',
Paper: 'B',
Scissors: 'C',
PlayerRock: 'X',
PlayerPaper: 'Y',
PlayerScissors: 'Z',
Lose: 'X',
Draw: 'Y',
Win: 'Z',
};
function doFirstPuz() {
const scores = {
A: 1,
B: 2,
C: 3,
X: 1,
Y: 2,
Z: 3,
win: 6,
draw: 3,
loss: 0
}
let totalScore = 0;
let result = splitLines(input).map((row) => {
return row.split(' ');
}).map((round) => {
// Add score for what was chosen (rock/paper/scissors)
totalScore += scores[round[1]];
// Determine points for winner
let playerWins = false;
let draw = scores[round[0]] === scores[round[1]];
if (!draw) {
switch (round[0]) {
case mappings.Rock:
playerWins = round[1] === mappings.PlayerPaper;
break;
case mappings.Paper:
playerWins = round[1] === mappings.PlayerScissors;
break;
case mappings.Scissors:
playerWins = round[1] === mappings.PlayerRock;
break;
default:
console.error(`Unknown input "${round[0]}"`);
break;
}
}
if (playerWins) {
totalScore += scores.win;
} else if (draw) {
totalScore += scores.draw;
} else {
totalScore += scores.loss;
}
});
console.log(totalScore);
}
doFirstPuz(input);
function matchFix() {
const scores = {
A: 1,
B: 2,
C: 3,
X: 0, // Lose
Y: 3, // Draw
Z: 6, // Win
}
let totalScore = 0;
let result = splitLines(input).map((row) => {
return row.split(' ');
}).map((round) => {
switch (round[1]) {
case mappings.Lose:
if (round[0] === mappings.Rock) {
totalScore += scores[mappings.Scissors];
} else if (round[0] === mappings.Paper) {
totalScore += scores[mappings.Rock];
} else if (round[0] === mappings.Scissors) {
totalScore += scores[mappings.Paper];
}
break;
case mappings.Draw:
totalScore += scores[round[0]];
break;
case mappings.Win:
if (round[0] === mappings.Rock) {
totalScore += scores[mappings.Paper];
} else if (round[0] === mappings.Paper) {
totalScore += scores[mappings.Scissors];
} else if (round[0] === mappings.Scissors) {
totalScore += scores[mappings.Rock];
}
break;
default:
break;
}
// Add score for result (win/lose/draw)
totalScore += scores[round[1]];
});
console.log(totalScore);
}
matchFix(input);