-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.ts
58 lines (40 loc) · 1.3 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
import { answer, question, section } from "../utils.ts";
export default function solution(input: string) {
// Part 1
question("How many characters need to be processed before the first start-of-packet marker is detected?");
function findMarker(input: string, size: number) {
const marker: Array<string> = [];
for (let i = 0; i < input.length; i++) {
const char = input[i];
const indexOfChar = marker.indexOf(char);
if (indexOfChar >= 0) {
marker.splice(0, indexOfChar + 1);
}
marker.push(char);
if (marker.length == size) {
return i + 1;
}
}
}
answer(findMarker(input, 4));
question("How many characters need to be processed before the first start-of-message marker is detected?");
answer(findMarker(input, 14));
}
const example1 = `mjqjpqmgbljsphdztnvjfqwrcgsmlb`;
const example2 = `bvwbjplbgvbhsrlpgdmjqwftvncz`;
const example3 = `nppdvjthqldpwncqszvftbrmjlhg`;
const example4 = `nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg`;
const example5 = `zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw`;
const input = await Deno.readTextFile("./input.txt");
section("Example 1");
solution(example1);
section("Example 2");
solution(example2);
section("Example 3");
solution(example3);
section("Example 4");
solution(example4);
section("Example 5");
solution(example5);
section("Input");
solution(input);