-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (100 loc) · 1.68 KB
/
index.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
import run from "aocrunner";
const parseInput = (rawInput) => rawInput
.replace(/A|X/gi,0)
.replace(/B|Y/gi,1)
.replace(/C|Z/gi,2)
.split(/\n/)
.map(e=>e.split(/ /).map(e=>e*1));
const part1 = (rawInput) => {
const input = parseInput(rawInput);
let score = 0;
for (const round of input) {
score += round[1] + 1;
if (
// win
round[0] === 0 && round[1] === 1 ||
round[0] === 1 && round[1] === 2 ||
round[0] === 2 && round[1] === 0
) {
score += 6;
} else if (
// lose
round[0] === 1 && round[1] === 0 ||
round[0] === 2 && round[1] === 1 ||
round[0] === 0 && round[1] === 2
) {
} else {
// draw
score += 3;
}
}
return score;
};
const part2Rules = {
0: { // lose
1: 0,
2: 1,
0: 2,
},
1: { // draw
0: 0,
1: 1,
2: 2,
},
2: { // win
0: 1,
1: 2,
2: 0,
},
};
const part2 = (rawInput) => {
const input = parseInput(rawInput);
let score = 0;
let outcome;
let play;
for (const round of input) {
outcome = round[1];
play = part2Rules[outcome][round[0]];
score += play + 1;
if (outcome === 2) {
// win
score += 6;
} else if (outcome === 0) {
// lose
} else {
// draw
score += 3;
}
}
return score;
};
run({
part1: {
tests: [
{
input: `
A Y
B X
C Z
`,
expected: 15,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `
A Y
B X
C Z
`,
expected: 12,
},
],
solution: part2,
},
trimTestInputs: true,
// onlyTests: true,
});