-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
135 lines (125 loc) · 2.49 KB
/
index.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import run from "aocrunner";
const parseInput = (rawInput: string) => rawInput;
const part1 = (rawInput: string) => {
const input = parseInput(rawInput);
const lines = input.split("\n");
let total = 0;
for (let index in lines) {
// console.log(index)
const parts = lines[index].split(" ");
const score = calculateScore1(parts[0], parts[1]);
total += score;
// console.log(`${parts} - ${score} - ${total}`);
// console.log(total)
}
return total;
};
const part2 = (rawInput: string) => {
const input = parseInput(rawInput);
const lines = input.split("\n");
let total = 0;
for (let index in lines) {
// console.log(index)
const parts = lines[index].split(" ");
const score = calculateScore2(parts[0], parts[1]);
total += score;
// console.log(`${parts} - ${score} - ${total}`);
// console.log(total)
}
return total;
};
const calculateScore1 = (opponent: string, me: string): number => {
let score = 0;
switch (opponent) {
case "A":
switch (me) {
case "X": score += 4; break;
case "Y": score += 8; break;
case "Z": score += 3; break;
}
break;
case "B":
switch (me) {
case "X": score += 1; break;
case "Y": score += 5; break;
case "Z": score += 9; break;
}
break;
case "C":
switch (me) {
case "X": score += 7; break;
case "Y": score += 2; break;
case "Z": score += 6; break;
}
break;
}
return score;
}
const calculateScore2 = (opponent: string, me: string): number => {
let score = 0;
switch (opponent) {
case "A":
switch (me) {
case "X": score += 3; break;
case "Y": score += 4; break;
case "Z": score += 8; break;
}
break;
case "B":
switch (me) {
case "X": score += 1; break;
case "Y": score += 5; break;
case "Z": score += 9; break;
}
break;
case "C":
switch (me) {
case "X": score += 2; break;
case "Y": score += 6; break;
case "Z": score += 7; break;
}
break;
}
return score;
}
run({
part1: {
tests: [
{
input: `
A Y
B X
C Z`,
expected: 15,
},
{
input: `
A X
A Y
A Z
B X
B Y
B Z
C X
C Y
C Z`,
expected: 45,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `
A Y
B X
C Z`,
expected: 12,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});