-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.js
59 lines (44 loc) · 1.1 KB
/
2.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
import { parseData } from "../helpers";
const data = parseData("day-2/input.txt");
const scores = {
A: 1, // rock (beats scissors)
B: 2, // paper (beats rock)
C: 3, // scissors (beats paper)
X: 1,
Y: 2,
Z: 3,
};
function puzzle1(input) {
let myScore = 0;
for (const game of input) {
const player1 = scores[game[0]];
const player2 = scores[game[2]];
let gameScore = player2;
if (player2 - (player1 % 3) === 1) {
gameScore += 6;
}
if (player2 === player1) {
gameScore += 3;
}
myScore += gameScore;
}
return myScore;
}
const RPC = ["A", "B", "C"];
const wldOffsets = {
X: { offset: 2, bonus: 0 },
Y: { offset: 0, bonus: 3 },
Z: { offset: 1, bonus: 6 },
};
function puzzle2(input) {
let myScore = 0;
for (const game of input) {
const player1 = game[0];
const player2data = wldOffsets[game[2]];
const place = (RPC.indexOf(player1) + player2data.offset) % RPC.length;
myScore += scores[RPC[place]] + player2data.bonus;
}
return myScore;
}
console.log(`Puzzle 1: ${puzzle1(data)}`);
console.log(`Puzzle 2: ${puzzle2(data)}`);