-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
152 lines (122 loc) · 2.88 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import run from "aocrunner";
const parseInput = (rawInput: string) => rawInput;
enum Moves {
SCISSORS = "C",
ROCK = "A",
PAPER = "B"
}
const winningEncryptedAssociations = new Map<string, string>([
["Z", Moves.PAPER],
["X", Moves.SCISSORS],
["Y", Moves.ROCK],
]);
const winningAssociations = new Map<string,string> ([
[Moves.PAPER, Moves.ROCK],
[Moves.ROCK, Moves.SCISSORS],
[Moves.SCISSORS, Moves.PAPER]
])
const losingAssociations = new Map<string,string> ([
[Moves.ROCK, Moves.PAPER],
[Moves.SCISSORS, Moves.ROCK],
[Moves.PAPER, Moves.SCISSORS]
])
const encryptedMoveScore = new Map<string, number>([
["Z", 3],
["X", 1],
["Y", 2],
]);
const encryptedMove = new Map<string, string>([
["Z", Moves.SCISSORS],
["X", Moves.ROCK],
["Y", Moves.PAPER],
]);
enum ResultScores {
WON = 6,
DRAW = 3,
LOST = 0
}
const moveScore = new Map<string, number>([
["C", 3],
["A", 1],
["B", 2],
]);
const part1 = (rawInput: string) => {
const input = parseInput(rawInput);
const rounds = input.split("\n");
let totalScore: number = 0;
rounds.forEach(round => {
const [opponentMove, myMove] = round.split(" ");
let scoreRound = 0;
// Determine result
if(encryptedMove.get(myMove) === opponentMove) {
scoreRound += ResultScores.DRAW;
}
if(winningEncryptedAssociations.get(myMove) === opponentMove) {
scoreRound += ResultScores.WON;
}
// Add move Score
scoreRound += Number(encryptedMoveScore.get(myMove));
totalScore += scoreRound;
})
return totalScore.toString();
};
const part2 = (rawInput: string) => {
const howItNeedToEndScore = new Map<string, number>([
["X" ,ResultScores.LOST],
["Y" , ResultScores.DRAW],
["Z" ,ResultScores.WON]
]
);
const input = parseInput(rawInput);
const rounds = input.split("\n");
let totalScore: number = 0;
rounds.forEach(round => {
let roundScore = 0;
const [opponentMove, howItNeedToEnd] = round.split(" ");
roundScore += howItNeedToEndScore.get(howItNeedToEnd)!;
// Define my move
let myMove = "";
if(roundScore === ResultScores.DRAW) {
myMove = opponentMove;
}
if(roundScore === ResultScores.WON) {
myMove = losingAssociations.get(opponentMove)!;
}
if(roundScore === ResultScores.LOST) {
myMove = winningAssociations.get(opponentMove)!;
}
roundScore += moveScore.has(myMove) ? moveScore.get(myMove)! : 0;
totalScore += roundScore;
})
return totalScore.toString();
};
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: false,
});