-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.ts
127 lines (106 loc) · 2.79 KB
/
solution.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
import { answer, question, section } from "../utils.ts";
export default function solution(input: string) {
// Part 1
question("What would your total score be if everything goes exactly according to your strategy guide?");
enum Shape {
Rock,
Paper,
Scissors,
}
enum Outcome {
Win,
Lose,
Draw,
}
const OPONENT_SHAPE = {
A: Shape.Rock,
B: Shape.Paper,
C: Shape.Scissors,
}
const MY_SHAPE = {
X: Shape.Rock,
Y: Shape.Paper,
Z: Shape.Scissors,
}
const SHAPE_SCORE: Record<Shape, number> = {
[Shape.Rock]: 1,
[Shape.Paper]: 2,
[Shape.Scissors]: 3,
}
const OUTCOME_SCORE: Record<Outcome, number> = {
[Outcome.Draw]: 3,
[Outcome.Win]: 6,
[Outcome.Lose]: 0,
}
const SHAPE_OUTCOME: Record<Shape, Record<Shape, Outcome>> = {
[Shape.Rock]: {
[Shape.Rock]: Outcome.Draw,
[Shape.Paper]: Outcome.Win,
[Shape.Scissors]: Outcome.Lose,
},
[Shape.Paper]: {
[Shape.Rock]: Outcome.Lose,
[Shape.Paper]: Outcome.Draw,
[Shape.Scissors]: Outcome.Win,
},
[Shape.Scissors]: {
[Shape.Rock]: Outcome.Win,
[Shape.Paper]: Outcome.Lose,
[Shape.Scissors]: Outcome.Draw,
},
}
type Round = [keyof typeof OPONENT_SHAPE, keyof typeof MY_SHAPE];
const rounds = input.split("\n")
.map(round => round.split(" ") as Round);
const score = rounds.reduce((score, [oponent, my]) => {
const oponentShape = OPONENT_SHAPE[oponent];
const myShape = MY_SHAPE[my];
const outcome = SHAPE_OUTCOME[oponentShape][myShape];
const outcomeScore = OUTCOME_SCORE[outcome];
const shapeScore = SHAPE_SCORE[myShape];
return score + outcomeScore + shapeScore;
}, 0);
answer(score);
// Part 2
question("Following the Elf's instructions for the second column, what would your total score be if everything goes exactly according to your strategy guide?")
const STRATEGY_OUTCOME = {
X: Outcome.Lose,
Y: Outcome.Draw,
Z: Outcome.Win,
}
const OUTCOME_SHAPE = {
[Shape.Rock]: {
[Outcome.Draw]: Shape.Rock,
[Outcome.Win]: Shape.Paper,
[Outcome.Lose]: Shape.Scissors,
},
[Shape.Paper]: {
[Outcome.Draw]: Shape.Paper,
[Outcome.Win]: Shape.Scissors,
[Outcome.Lose]: Shape.Rock,
},
[Shape.Scissors]: {
[Outcome.Draw]: Shape.Scissors,
[Outcome.Win]: Shape.Rock,
[Outcome.Lose]: Shape.Paper,
},
}
const strategyScore = rounds.reduce((score, [oponent, strategy]) => {
const oponentShape = OPONENT_SHAPE[oponent];
const outcome = STRATEGY_OUTCOME[strategy];
const myShape = OUTCOME_SHAPE[oponentShape][outcome];
const outcomeScore = OUTCOME_SCORE[outcome];
const shapeScore = SHAPE_SCORE[myShape];
return score + outcomeScore + shapeScore;
}, 0);
answer(strategyScore);
}
const example =
`A Y
B X
C Z`;
const input = await Deno.readTextFile("./input.txt");
section("Example");
solution(example);
section("Input");
solution(input);