-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08_1.js
82 lines (74 loc) · 2.01 KB
/
day08_1.js
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
console.log('###########');
import { readLines } from 'https://deno.land/[email protected]/io/bufio.ts';
import { range } from 'https://deno.land/x/[email protected]/range.mjs';
import { slidingWindows } from 'https://deno.land/[email protected]/collections/mod.ts';
import count from 'https://deno.land/x/[email protected]/src/collection/count.ts';
const input = await Deno.readTextFile('./resources/input08.txt');
// const input = `30373
// 25512
// 65332
// 33549
// 35390`;
const map = input
.trim()
.split('\n')
.map((s) =>
s
.trim()
.split('')
.map((i) => parseInt(i, 10)),
);
console.log({ map });
const visible = {};
let maxScore = 1;
for (let r = 0; r < map.length; r++) {
for (let c = 0; c < map.length; c++) {
console.log({ r, c }, map[r][c]);
const scores = [0, 0, 0, 0];
let isV = true;
for (let x = r - 1, y = c; x >= 0; x--) {
scores[0]++;
if (map[x][y] >= map[r][c]) {
isV = false;
break;
}
}
console.log('1', isV);
if (isV) visible[`${r}-${c}`] = true;
isV = true;
for (let x = r, y = c - 1; y >= 0; y--) {
scores[1]++;
if (map[x][y] >= map[r][c]) {
isV = false;
break;
}
}
console.log('3', isV);
if (isV) visible[`${r}-${c}`] = true;
isV = true;
for (let x = r, y = c + 1; y < map[0].length; y++) {
// console.log({ x, y, r, c }, map[x][y], map[r][c]);
scores[2]++;
if (map[x][y] >= map[r][c]) {
isV = false;
break;
}
}
console.log('4', isV);
isV = true;
for (let x = r + 1, y = c; x < map.length; x++) {
scores[3]++;
if (map[x][y] >= map[r][c]) {
isV = false;
break;
}
}
console.log('2', isV);
if (isV) visible[`${r}-${c}`] = true;
const score = scores.reduce((acc, c) => acc * c, 1);
console.log({ scores }, score);
if (score > maxScore) maxScore = score;
if (isV) visible[`${r}-${c}`] = true;
}
}
console.log({ visible }, Object.keys(visible).length, maxScore);