-
Notifications
You must be signed in to change notification settings - Fork 0
/
b.ts
77 lines (64 loc) · 1.58 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
76
77
import { assertEquals } from "https://deno.land/std@0.167.0/testing/asserts.ts";
function solution(input: string) {
const check = (
matrix: number[][],
y: number,
x: number,
dx: number,
dy: number,
) => {
const target = matrix[y][x];
let s = 0;
while (true) {
if (
y === matrix.length - 1 ||
x === matrix[y].length - 1 ||
y === 0 ||
x === 0
) {
break;
}
if (target <= matrix[y + dy][x + dx]) {
s++;
break;
} else {
s++;
}
x += dx;
y += dy;
}
return s;
};
const lines = input.split("\n").map((line) =>
line.split("").map((n) => Number(n))
);
const scenicScores: number[] = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
for (let j = 0; j < line.length; j++) {
if (
j === 0 || j === line.length - 1 || i === 0 || i === lines.length - 1
) {
continue;
}
const score = check(lines, i, j, -1, 0) *
check(lines, i, j, 0, -1) *
check(lines, i, j, 1, 0) *
check(lines, i, j, 0, 1);
scenicScores.push(score);
}
}
return Math.max(...scenicScores);
}
Deno.test("example", () => {
const input = Deno.readTextFileSync("./08/example.txt");
const actual = solution(input);
const expected = 8;
assertEquals(actual, expected);
});
Deno.test("puzzle input", { ignore: false }, () => {
const input = Deno.readTextFileSync("./08/input.txt");
const actual = solution(input);
const expected = 211680;
assertEquals(actual, expected);
});