-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path09.js
102 lines (96 loc) · 2.5 KB
/
09.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use strict';
import {deepCopy} from './utils.js';
/**
* @param {string} d
*/
export const part1 = async d => {
const data = d.split('\n').map(e => e.split('').map(e => parseInt(e, 10)));
const width = data[0].length;
const height = data.length;
let lows = 0;
const lowPoints = [];
for (let y = 0; y < height; y++) {
const yPrev = (y == 0) ? false : y - 1;
const yNext = (y + 1 == height) ? false : y + 1;
for (let x = 0; x < width; x++) {
const xPrev = (x == 0) ? false : x - 1;
const xNext = (x + 1 == height) ? false : x + 1;
if (
(xPrev !== false && data[y][xPrev] <= data[y][x]) ||
(xNext !== false && data[y][xNext] <= data[y][x]) ||
(yPrev !== false && data[yPrev][x] <= data[y][x]) ||
(yNext !== false && data[yNext][x] <= data[y][x])
) {
continue;
}
lows += data[y][x] + 1;
lowPoints.push([x + 1, y + 1].join(','));
}
}
return lows;
};
/**
* @param {string} d
*/
export const part2 = async d => {
/**
* @type string[][]
*/
const data = d.replaceAll('9', ' ').split('\n').map(e => e.split(''));
const width = data[0].length;
const height = data.length;
const fillValues = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,./;\'[]\\-=`~!@#$%^&*()_+{}|:"<>?'.split('');
// Fill in with (visible) unicode characters I can't type
for (let uni = 0x00a1; uni < 0x00AD; uni++) {
fillValues.push(String.fromCodePoint(uni));
}
for (let uni = 0x00ae; uni < 0x0180; uni++) {
fillValues.push(String.fromCodePoint(uni));
}
/**
* @type string
*/
const fillValuesIndex = deepCopy(fillValues).join('');
const fillCount = fillValues.map(() => 0);
/**
*
* @param {string[][]} data
* @param {number} x
* @param {number} y
* @param {string} fill
*/
const floodFill = (data, x, y, fill) => {
// It's a wall! Ignore it
if (data[y][x] == ' ') {
return;
}
// Already filled! Ignore it (how did this happen?)
if (data[y][x] == fill) {
return;
}
data[y][x] = fill;
fillCount[fillValuesIndex.indexOf(fill)]++;
if (y > 0) {
floodFill(data, x, y - 1, fill);
}
if (y + 1 < height) {
floodFill(data, x, y + 1, fill);
}
if (x > 0) {
floodFill(data, x - 1, y, fill);
}
if (x + 1 < width) {
floodFill(data, x + 1, y, fill);
}
};
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
if (data[y][x] == ' ') continue;
if (/[0-8]/.test(data[y][x])) {
floodFill(data, x, y, fillValues.shift());
}
}
}
fillCount.sort((a, b) => b - a);
return fillCount[0] * fillCount[1] * fillCount[2];
};