Merge branch '2021-09'
This commit is contained in:
commit
f97dbcbcfe
54
2021/09/part1.mjs
Normal file
54
2021/09/part1.mjs
Normal file
@ -0,0 +1,54 @@
|
||||
import fs from "fs-extra";
|
||||
import fp from "lodash/fp.js";
|
||||
import _ from "lodash";
|
||||
|
||||
export const processInput = (filename) => fs.readFile(filename,'utf8').then(
|
||||
content => content.split("\n").filter(x=>x).map(
|
||||
lines => lines.split('').map(x => parseInt(x))
|
||||
)
|
||||
);
|
||||
|
||||
export const genNeighbours = (maxX,maxY) => (x,y) =>
|
||||
[ [-1,0], [1,0], [0,1], [0,-1] ].map(
|
||||
delta => _.zip(delta,[x,y]).map(_.sum)
|
||||
).filter( ([x,y]) => {
|
||||
if(x<0 || y<0 ) return false;
|
||||
if( x>= maxX || y>= maxY ) return false;
|
||||
return true;
|
||||
} );
|
||||
|
||||
export const genCoords = grid => ([x,y]) => grid[x][y];
|
||||
|
||||
export function solution(grid) {
|
||||
const seen = grid.map( () => [] );
|
||||
let risk = 0;
|
||||
|
||||
const neighbours = genNeighbours(grid.length,grid[0].length);
|
||||
const coords = genCoords(grid);
|
||||
|
||||
for( const x of _.range(grid.length) ) {
|
||||
for( const y of _.range(grid[x].length) ) {
|
||||
if( seen[x][y] ) continue;
|
||||
|
||||
let lowest = true;
|
||||
for( const n of neighbours(x,y) ) {
|
||||
if( coords(n) > grid[x][y] ) {
|
||||
seen[n[0]][n[1]] = true;
|
||||
} else if ( coords(n) == grid[x][y] ) {
|
||||
seen[n[0]][n[1]] = true;
|
||||
lowest = false;
|
||||
} else {
|
||||
lowest = false;
|
||||
}
|
||||
}
|
||||
|
||||
if(lowest) {
|
||||
risk += 1 + grid[x][y];
|
||||
}
|
||||
seen[x][y] = true;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return risk;
|
||||
}
|
44
2021/09/part2.mjs
Normal file
44
2021/09/part2.mjs
Normal file
@ -0,0 +1,44 @@
|
||||
import fs from "fs-extra";
|
||||
import fp from "lodash/fp.js";
|
||||
import _ from "lodash";
|
||||
|
||||
import * as p1 from './part1.mjs';
|
||||
|
||||
function getBasinSize( [x,y], grid, seen ) {
|
||||
if( seen[x][y] ) return 0;
|
||||
|
||||
seen[x][y] = true;
|
||||
|
||||
if( grid[x][y] == 9 ) return 0;
|
||||
let size = 1;
|
||||
|
||||
const neighbours = p1.genNeighbours(grid.length,grid[0].length);
|
||||
const coords = p1.genCoords(grid);
|
||||
|
||||
neighbours(x,y).map( n => getBasinSize(n,grid,seen) ).forEach(
|
||||
x => size+=x
|
||||
);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
export function solution(grid) {
|
||||
const seen = grid.map( () => [] );
|
||||
let basinSizes = [];
|
||||
|
||||
|
||||
for( const x of _.range(grid.length) ) {
|
||||
for( const y of _.range(grid[x].length) ) {
|
||||
if( seen[x][y] ) continue;
|
||||
|
||||
if( grid[x][y] == 9 ) {
|
||||
seen[x][y] = true;
|
||||
continue;
|
||||
}
|
||||
basinSizes.push( getBasinSize([x,y],grid,seen));
|
||||
}
|
||||
}
|
||||
|
||||
basinSizes.sort( (a,b) => a < b ? 1 : -1 );
|
||||
return basinSizes.splice(0,3).reduce( (a,b) => a*b ) ;
|
||||
}
|
5
2021/09/sample
Normal file
5
2021/09/sample
Normal file
@ -0,0 +1,5 @@
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
23
2021/09/test.mjs
Normal file
23
2021/09/test.mjs
Normal file
@ -0,0 +1,23 @@
|
||||
// https://adventofcode.com/2021/day/09
|
||||
|
||||
import tap from "tap";
|
||||
import fs from "fs-extra";
|
||||
|
||||
import * as p1 from "./part1.mjs";
|
||||
import * as p2 from "./part2.mjs";
|
||||
|
||||
const sample = p1.processInput("sample");
|
||||
const input = p1.processInput("input");
|
||||
|
||||
tap.test("part1", async (t) => {
|
||||
t.match(p1.genNeighbours(10,10)(0,0), [
|
||||
[1,0],[0,1]
|
||||
]);
|
||||
t.equal(p1.solution(await sample), 15);
|
||||
t.equal(p1.solution(await input), 550);
|
||||
});
|
||||
|
||||
tap.test("part2", async (t) => {
|
||||
t.equal(p2.solution(await sample), 1134);
|
||||
t.equal(p2.solution(await input), 1100682);
|
||||
});
|
Loading…
Reference in New Issue
Block a user