2021-15
This commit is contained in:
parent
3540dc29de
commit
373d037c62
50
2021/15/part1.mjs
Normal file
50
2021/15/part1.mjs
Normal file
@ -0,0 +1,50 @@
|
||||
import fs from "fs-extra";
|
||||
import fp from "lodash/fp.js";
|
||||
import _ from "lodash";
|
||||
import Heap from 'heap';
|
||||
|
||||
export const processInput = (input) => fs.readFile(input,'utf8').then(
|
||||
content => content.split("\n").filter(x=>x).map(
|
||||
line => line.split('').map( x => parseInt(x) )
|
||||
)
|
||||
);
|
||||
|
||||
export const genNeighbours = (maxX,maxY) => ([x,y]) =>
|
||||
[
|
||||
[1,0], [0,1],[-1,0], [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 function solution(grid) {
|
||||
|
||||
const lowest = grid.map( () => grid[0].map(()=>9E9) );
|
||||
|
||||
const neighbors = genNeighbours(grid.length, grid[0].length);
|
||||
|
||||
const scouts = new Heap((a,b) => a.risk - b.risk);
|
||||
scouts.push( {pos: [0,0], risk: 0 } );
|
||||
|
||||
let destinationRisk = () => _.last(_.last(lowest)) || 9E9;
|
||||
|
||||
while(scouts.size()) {
|
||||
const {pos,risk} = scouts.pop();
|
||||
|
||||
for( const [x,y] of neighbors(pos) ) {
|
||||
|
||||
if( lowest[x][y] <= risk + grid[x][y] ) continue;
|
||||
|
||||
if( destinationRisk() <= risk + grid[x][y] ) continue;
|
||||
|
||||
lowest[x][y] = risk + grid[x][y];
|
||||
|
||||
scouts.push({pos:[x,y], risk: lowest[x][y]} );
|
||||
}
|
||||
}
|
||||
|
||||
return _.last( _.last(lowest) );
|
||||
}
|
28
2021/15/part2.mjs
Normal file
28
2021/15/part2.mjs
Normal file
@ -0,0 +1,28 @@
|
||||
import fs from "fs-extra";
|
||||
import fp from "lodash/fp.js";
|
||||
import _ from "lodash";
|
||||
|
||||
import * as p1 from './part1.mjs';
|
||||
|
||||
export function solution(grid) {
|
||||
|
||||
const massive = Array.from({
|
||||
length: 5 * grid.length }, ( ) => []
|
||||
);
|
||||
|
||||
const wrap = x => x <= 9 ? x : (1+x % 10);
|
||||
|
||||
for ( const magX of _.range(0,5) ) {
|
||||
for ( const magY of _.range(0,5) ) {
|
||||
for ( const x of _.range(grid.length) ) {
|
||||
for( const y of _.range(grid[0].length) ) {
|
||||
massive[x + grid[0].length * magX][
|
||||
y + grid.length * magY
|
||||
] = wrap( grid[x][y] + magX + magY )
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return p1.solution(massive);
|
||||
}
|
10
2021/15/sample
Normal file
10
2021/15/sample
Normal file
@ -0,0 +1,10 @@
|
||||
1163751742
|
||||
1381373672
|
||||
2136511328
|
||||
3694931569
|
||||
7463417111
|
||||
1319128137
|
||||
1359912421
|
||||
3125421639
|
||||
1293138521
|
||||
2311944581
|
20
2021/15/test.mjs
Normal file
20
2021/15/test.mjs
Normal file
@ -0,0 +1,20 @@
|
||||
// https://adventofcode.com/2021/day/15
|
||||
|
||||
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.equal(p1.solution(await sample), 40);
|
||||
t.equal(p1.solution(await input), 456);
|
||||
});
|
||||
|
||||
tap.test("part2", async (t) => {
|
||||
t.equal(p2.solution(await sample), 315);
|
||||
t.equal(p2.solution(await input), 2831);
|
||||
});
|
Loading…
Reference in New Issue
Block a user