From 373d037c624b39d46715520f95860e55892e06b6 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Wed, 15 Dec 2021 10:34:28 -0500 Subject: [PATCH 1/2] 2021-15 --- 2021/15/part1.mjs | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 2021/15/part2.mjs | 28 ++++++++++++++++++++++++++ 2021/15/sample | 10 ++++++++++ 2021/15/test.mjs | 20 +++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 2021/15/part1.mjs create mode 100644 2021/15/part2.mjs create mode 100644 2021/15/sample create mode 100644 2021/15/test.mjs diff --git a/2021/15/part1.mjs b/2021/15/part1.mjs new file mode 100644 index 0000000..105a981 --- /dev/null +++ b/2021/15/part1.mjs @@ -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) ); +} diff --git a/2021/15/part2.mjs b/2021/15/part2.mjs new file mode 100644 index 0000000..3d04249 --- /dev/null +++ b/2021/15/part2.mjs @@ -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); +} diff --git a/2021/15/sample b/2021/15/sample new file mode 100644 index 0000000..ab80887 --- /dev/null +++ b/2021/15/sample @@ -0,0 +1,10 @@ +1163751742 +1381373672 +2136511328 +3694931569 +7463417111 +1319128137 +1359912421 +3125421639 +1293138521 +2311944581 diff --git a/2021/15/test.mjs b/2021/15/test.mjs new file mode 100644 index 0000000..357d92c --- /dev/null +++ b/2021/15/test.mjs @@ -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); +}); From f10edefcb410f48d3d5eb4090b0de042e3a4b4e1 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Wed, 15 Dec 2021 10:35:12 -0500 Subject: [PATCH 2/2] refine the template --- 2021/_templates/day/new/part1.mjs.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2021/_templates/day/new/part1.mjs.t b/2021/_templates/day/new/part1.mjs.t index 7260703..7e6ee2f 100644 --- a/2021/_templates/day/new/part1.mjs.t +++ b/2021/_templates/day/new/part1.mjs.t @@ -5,7 +5,7 @@ import fs from "fs-extra"; import fp from "lodash/fp.js"; import _ from "lodash"; -export const processInput = (input) => input; +export const processInput = (input) => fs.readFile(input,'utf8'); export function solution(input) { }