-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.js
executable file
·61 lines (57 loc) · 1.61 KB
/
day02.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
#!/usr/bin/env node
// https://adventofcode.com/2022/day/2
const input = `A Y
B X
C Z`;
// Part 1 - Score tournament, given your plays
const score = {
// rock = 1 point, paper = 2 points, scissors = 3 points
// win = 6 points, draw = 3 points, loss = 0 points
'A': { // they play rock
'X': 1 + 3, // rock, draw
'Y': 2 + 6, // paper, win
'Z': 3 + 0 // scissors, loss
},
'B': { // they play paper
'X': 1 + 0, // rock, loss
'Y': 2 + 3, // paper, draw
'Z': 3 + 6 // scissors, win
},
'C': { // they play scissors
'X': 1 + 6, // rock, win
'Y': 2 + 0, // paper, loss
'Z': 3 + 3, // scissors, draw
}
};
let sum = 0;
for (const line of input.split("\n")) {
[them, us] = line.split(" ");
sum += score[them][us];
}
console.log(sum);
// Part 2 - Score tournament, given desired results
const resultScore = {
// rock = 1 point, paper = 2 points, scissors = 3 points
// win = 6 points, draw = 3 points, loss = 0 points
'A': { // they play rock
'X': 3 + 0, // scissors, loss
'Y': 1 + 3, // rock, draw
'Z': 2 + 6 // paper, win
},
'B': { // they play paper
'X': 1 + 0, // rock, loss
'Y': 2 + 3, // paper, draw
'Z': 3 + 6 // scissors, win
},
'C': { // they play scissors
'X': 2 + 0, // paper, loss
'Y': 3 + 3, // scissors, draw
'Z': 1 + 6 // rock, win
}
};
let resultSum = 0;
for (const line of input.split("\n")) {
[them, result] = line.split(" ");
resultSum += resultScore[them][result];
}
console.log(resultSum);