Unable to load image

Advent of Code 2022: Day 8

Post solutions here :marseysad: :marseyill:

25
Jump in the discussion.

No email address required.

Almost got both of them done as 1-liners, I could do it if I remembered how to use Haskell.

var part1 = ((gridString) => {
    return gridString
        .split('\n')
        .map(line => line.split('').map(str => parseInt(str)))
        .map((row, rowIndex, grid) => row
            .map((height, colIndex) => {
                var column = grid.map(row => row[colIndex]);
                return row.slice(0, colIndex).every(v => height > v) 
                    || row.slice(colIndex + 1).reverse().every(v => height > v) 
                    || column.slice(0, rowIndex).every(v => height > v)
                    || column.slice(rowIndex + 1).reverse().every(v => height > v);
            }))
        .flatMap(row => row.filter(cell => cell))
        .reduce((a, v) => a + v, 0);
})(input);

var part2 = ((gridString) => {
    function takeWhile(array, predicate) {
        for (var i = 0, result = []; i < array.length; i++) {
            if (result.push(array[i]), !predicate(array[i])) break;
        }
        return result;
    }
    return gridString
        .split('\n')
        .map(line => line.split('').map(str => parseInt(str)))
        .map((row, rowIndex, grid) => row
            .map((height, colIndex) => {
                var column = grid.map(row => row[colIndex]);
                return takeWhile(row.slice(0, colIndex).reverse(), (v => height > v)).length
                    * takeWhile(row.slice(colIndex + 1), (v => height > v)).length
                    * takeWhile(column.slice(0, rowIndex).reverse(), v => height > v).length
                    * takeWhile(column.slice(rowIndex + 1), v => height > v).length;
            }))
        .flatMap(row => row)
        .sort((a, b) => b - a)[0];
})(input);

console.log(part1, part2);
Jump in the discussion.

No email address required.

now what's the big O of this mess?

Jump in the discussion.

No email address required.

About as big as your mom.

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.