-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
153 lines (137 loc) · 3.09 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
153
import run from "aocrunner";
type PlayerAValue = "A" | "B" | "C";
type PlayerBValue = "X" | "Y" | "Z";
enum Actions {
Rock = 1,
Paper = 2,
Scissors = 3,
}
const actionDict = {
A: Actions.Rock,
B: Actions.Paper,
C: Actions.Scissors,
X: Actions.Rock,
Y: Actions.Paper,
Z: Actions.Scissors,
};
function roundResult(playerAAction: Actions, playerBAction: Actions): number {
if (playerAAction === playerBAction) {
return 3;
}
switch (playerAAction) {
case Actions.Rock:
switch (playerBAction) {
case Actions.Paper:
return 6;
case Actions.Scissors:
return 0;
}
case Actions.Paper:
switch (playerBAction) {
case Actions.Scissors:
return 6;
case Actions.Rock:
return 0;
}
case Actions.Scissors:
switch (playerBAction) {
case Actions.Rock:
return 6;
case Actions.Paper:
return 0;
}
default:
throw new Error("Invalid action");
}
}
function roundScore(playerAAction: Actions, playerBAction: Actions): number {
return roundResult(playerAAction, playerBAction) + playerBAction;
}
function getRound2Action(
playerAAction: Actions,
playerB: PlayerBValue,
): Actions {
switch (playerAAction) {
case Actions.Rock:
switch (playerB) {
// lose
case "X":
return Actions.Scissors;
// tie
case "Y":
return Actions.Rock;
// win
case "Z":
return Actions.Paper;
}
case Actions.Paper:
switch (playerB) {
// lose
case "X":
return Actions.Rock;
// tie
case "Y":
return Actions.Paper;
// win
case "Z":
return Actions.Scissors;
}
case Actions.Scissors:
switch (playerB) {
// lose
case "X":
return Actions.Paper;
// tie
case "Y":
return Actions.Scissors;
// win
case "Z":
return Actions.Rock;
}
default:
throw new Error("Invalid action");
}
}
const parseInput = (rawInput: string) => rawInput.split("\n");
const part1 = (rawInput: string) => {
const input = parseInput(rawInput);
return input.reduce((total, round) => {
const [playerA, playerB] = round.split(" ") as [PlayerAValue, PlayerBValue];
return total + roundScore(actionDict[playerA], actionDict[playerB]);
}, 0);
};
const part2 = (rawInput: string) => {
const input = parseInput(rawInput);
return input.reduce((total, round) => {
const [playerA, playerB] = round.split(" ") as [PlayerAValue, PlayerBValue];
const playerAAction = actionDict[playerA];
const playerBAction = getRound2Action(playerAAction, playerB);
return total + roundScore(playerAAction, playerBAction);
}, 0);
};
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,
});