-
Notifications
You must be signed in to change notification settings - Fork 0
/
b.ts
54 lines (44 loc) · 1.29 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
import { assertEquals } from "https://deno.land/std@0.167.0/testing/asserts.ts";
const scoreLetter = (letter: string) => {
const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
return alphabet.indexOf(letter) + 1;
};
function solution(input: string) {
const lines = input.split("\n");
const [chunked] = lines.reduce<[string[][], string[]]>(
([acc, chunk], line) => {
chunk.push(line);
if (chunk.length === 3) {
acc.push(chunk);
return [acc, []];
}
return [acc, chunk];
},
[[], []],
);
const result = chunked
.map((chunk) => {
const [first, second, third] = chunk
.map((line) => line.split(""))
.map((line) => new Set(line));
for (const s of first) {
if (second.has(s) && third.has(s)) {
return s;
}
}
})
.reduce((acc, s) => acc + scoreLetter(s!), 0);
return result;
}
Deno.test("example", () => {
const input = Deno.readTextFileSync("./03/example.txt");
const actual = solution(input);
const expected = 70;
assertEquals(actual, expected);
});
Deno.test("puzzle input", { ignore: false }, () => {
const input = Deno.readTextFileSync("./03/input.txt");
const actual = solution(input);
const expected = 2683;
assertEquals(actual, expected);
});