-
Notifications
You must be signed in to change notification settings - Fork 0
/
b.ts
75 lines (64 loc) · 1.44 KB
/
b.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
import { assertEquals } from "https://deno.land/std@0.167.0/testing/asserts.ts";
type Shape = "A" | "B" | "C";
type Round = "X" | "Y" | "Z";
const scoreShape = (shape: Shape) => {
switch (shape) {
case "A":
return 1;
case "B":
return 2;
case "C":
return 3;
}
};
const scoreRound = (round: Round) => {
switch (round) {
case "X":
return 0;
case "Y":
return 3;
case "Z":
return 6;
}
};
const game = (a: Shape, b: Round) => {
const rules = {
A: {
X: "C" as const,
Y: "A" as const,
Z: "B" as const,
},
B: {
X: "A" as const,
Y: "B" as const,
Z: "C" as const,
},
C: {
X: "B" as const,
Y: "C" as const,
Z: "A" as const,
},
};
const shapeB = rules[a][b];
return scoreRound(b) + scoreShape(shapeB);
};
function solution(input: string) {
const lines = input.split("\n");
const result = lines.reduce((acc, line) => {
const [a, b] = line.split(" ") as [Shape, Round];
return acc + game(a, b);
}, 0);
return result;
}
Deno.test("example", () => {
const input = Deno.readTextFileSync("./02/example.txt");
const actual = solution(input);
const expected = 12;
assertEquals(actual, expected);
});
Deno.test("puzzle input", { ignore: false }, () => {
const input = Deno.readTextFileSync("./02/input.txt");
const actual = solution(input);
const expected = 11186;
assertEquals(actual, expected);
});