part 1
This commit is contained in:
parent
f4bc47bc23
commit
ff4203a58f
87
2022/08/part1.js
Normal file
87
2022/08/part1.js
Normal file
@ -0,0 +1,87 @@
|
||||
import * as R from "remeda";
|
||||
import { readFile } from "../05/part1.js";
|
||||
import Victor from '@a-robu/victor';
|
||||
|
||||
const V = (...args) => new Victor(...args);
|
||||
|
||||
const readInput = (...args) =>
|
||||
readFile(...args)
|
||||
.split("\n")
|
||||
.filter((x) => x)
|
||||
.map( l => l.split('').map( x => parseInt(x) ) );
|
||||
|
||||
export function generateVisibilityGrid(forest) {
|
||||
const size = forest.length;
|
||||
const visibility = Array(size).fill(null).map(
|
||||
() => Array(size).fill(false)
|
||||
);
|
||||
|
||||
// top
|
||||
const positions = R.range(size)(0).map( x => ({
|
||||
height: -1,
|
||||
position: V(x,-1),
|
||||
inc: V(0,1),
|
||||
}) );
|
||||
|
||||
// bottom
|
||||
positions.push(
|
||||
...R.range(size)(0).map( x => ({
|
||||
height: -1,
|
||||
position: V(x,size),
|
||||
inc: V(0,-1),
|
||||
}) )
|
||||
);
|
||||
|
||||
positions.push(
|
||||
...R.range(size)(0).map( x => ({
|
||||
height: -1,
|
||||
position: V(-1,x),
|
||||
inc: V(1,0),
|
||||
}) )
|
||||
);
|
||||
positions.push(
|
||||
...R.range(size)(0).map( x => ({
|
||||
height: -1,
|
||||
position: V(size,x),
|
||||
inc: V(-1,0),
|
||||
}) )
|
||||
);
|
||||
|
||||
const outOfBound = (pos) => {
|
||||
if( Math.min( pos.x, pos.y ) < 0 ) return true;
|
||||
if( Math.max( pos.x, pos.y ) >= size ) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
while( positions.length ) {
|
||||
const pos = positions.shift();
|
||||
pos.position.add( pos.inc );
|
||||
|
||||
if( outOfBound( pos.position ) ) continue;
|
||||
|
||||
if( forest[pos.position.x][pos.position.y] > pos.height ) {
|
||||
visibility[pos.position.x][pos.position.y] = true;
|
||||
pos.height = forest[pos.position.x][pos.position.y];
|
||||
}
|
||||
|
||||
positions.push(pos);
|
||||
}
|
||||
|
||||
return visibility;
|
||||
}
|
||||
|
||||
export const puzzleInput = readInput(import.meta.url, "input");
|
||||
export const sample = readInput(import.meta.url, "sample");
|
||||
|
||||
function printMap( forest ) {
|
||||
forest.forEach( line => console.log(line.join(' ')) );
|
||||
return forest;
|
||||
}
|
||||
|
||||
export default R.createPipe(
|
||||
printMap,
|
||||
generateVisibilityGrid,
|
||||
printMap,
|
||||
R.flatten,
|
||||
R.countBy(R.identity)
|
||||
);
|
4
2022/08/part2.js
Normal file
4
2022/08/part2.js
Normal file
@ -0,0 +1,4 @@
|
||||
import * as R from "remeda";
|
||||
|
||||
|
||||
export default () => {};
|
5
2022/08/sample
Normal file
5
2022/08/sample
Normal file
@ -0,0 +1,5 @@
|
||||
30373
|
||||
25512
|
||||
65332
|
||||
33549
|
||||
35390
|
21
2022/08/test.js
Normal file
21
2022/08/test.js
Normal file
@ -0,0 +1,21 @@
|
||||
import { test, expect, describe } from "vitest";
|
||||
|
||||
import { expectSolution } from "../01/main.js";
|
||||
import part1, { sample, puzzleInput, generateVisibilityGrid } from "./part1.js";
|
||||
import part2 from "./part2.js";
|
||||
|
||||
describe("part 1", () => {
|
||||
test( "sample", () => {
|
||||
expect( part1(sample) ).toBe(21);
|
||||
|
||||
});
|
||||
test("solution", () => {
|
||||
expectSolution(part1(puzzleInput)).toEqual(1703);
|
||||
});
|
||||
});
|
||||
|
||||
describe("part 2", () => {
|
||||
test.todo("solution", () => {
|
||||
expectSolution(part2(puzzleInput)).toEqual("TODO");
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user