advent of code 2023 day 11: test galaxy expanding separately, fix rest

This commit is contained in:
Wouter Groeneveld 2023-12-11 17:43:40 +01:00
parent 9a7148770e
commit 68c0fbd901
2 changed files with 80 additions and 31 deletions

View File

@ -1,41 +1,49 @@
module.exports.solve = function(input) {
const expandGalaxyInDepth = (map) => {
[...Array(map.length).keys()].forEach(rownr => {
if(new Set(map[rownr]).size === 1 && map[rownr][0] === '.') {
map.splice(rownr, 0, '.'.repeat(map[0].length).split(''))
}
const expandGalaxyInDepth = (map) => {
[...Array(map.length).keys()]
.filter(rownr => new Set(map[rownr]).size === 1 && map[rownr][0] === '.')
.forEach((rownr, i) => {
// Don't do this in a forEach() over the rows! What a mistaka to-a maka...
map.splice(rownr + i, 0, '.'.repeat(map[rownr].length).split(''))
})
return map
}
const expandGalaxyInBreadth = (map) => {
const rownrs = [...Array(map.length).keys()];
[...Array(map[0].length).keys()]
.filter(colnr => new Set(rownrs.map(rownr => map[rownr][colnr])).size === 1 && map[0][colnr] === '.')
.forEach(colnr => {
rownrs.forEach(rownr => {
map[rownr].splice(colnr, 0, '.') // There's something not quite right about this?
})
return map
}
const expandGalaxyInBreadth = (map) => {
const rownrs = [...Array(map.length).keys()];
[...Array(map[0].length).keys()]
.filter(colnr => new Set(rownrs.map(rownr => map[rownr][colnr])).size === 1 && map[0][colnr] === '.')
.reverse()
.forEach(colnr => {
rownrs.forEach(rownr => {
map[rownr].splice(colnr, 0, '.')
})
return map
}
const discoverGalaxy = (map) => {
const galaxies = []
// Trying to create these loops functionally here seems to work against me in JS
for(let i = 0; i < map.length; i++) {
for(let j = 0; j < map[i].length; j++) {
if(map[i][j] === '#') {
galaxies.push([i, j])
}
})
return map
}
const expandGalaxy = (map) => expandGalaxyInDepth(expandGalaxyInBreadth(map))
const discoverGalaxy = (map) => {
const galaxies = []
// Trying to create these loops functionally here seems to work against me in JS
for(let i = 0; i < map.length; i++) {
for(let j = 0; j < map[i].length; j++) {
if(map[i][j] === '#') {
galaxies.push([i, j])
}
}
return galaxies
}
return galaxies
}
const map = expandGalaxyInBreadth(expandGalaxyInDepth(input.split('\n').map(row => row.split(''))))
const solve = (input) => {
const map = expandGalaxy(input.split('\n').map(row => row.split('')))
const planets = discoverGalaxy(map)
return planets
// Cutting corners: difference in coords = minimum distance. Won't always work?
.flatMap((v, i) => planets.slice(i+1).map(w => Math.abs(v[0] - w[0]) + Math.abs(v[1] - w[1])))
.reduce((a, b) => a + b)
}
module.exports = {
expandGalaxy,
solve
}

View File

@ -1,9 +1,50 @@
const test = require('node:test')
const assert = require('node:assert').strict;
const { readFileSync } = require('fs')
const { solve } = require('./impl.js')
const { expandGalaxy, solve } = require('./impl.js')
const toMap = (input) => input.split('\n').map(row => row.split(''))
const fromMap = (map) => {
let str = ""
for(let i = 0; i < map.length; i++) {
for(let j = 0; j < map[i].length; j++) {
str += map[i][j]
}
str += '\n'
}
return str
}
test('Expanding the galaxy works as expected', (t) => {
const map = `...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....`
const expanded = `....#........
.........#...
#............
.............
.............
........#....
.#...........
............#
.............
.............
.........#...
#....#.......
`
const actual = fromMap(expandGalaxy(toMap(map)))
assert.equal(expanded, actual)
})
/*
test('The single shortest path in an isolated example is 9', (t) => {
const input = `...x......
.......x..
@ -18,7 +59,7 @@ x...#.....`
assert.equal(9, solve(input))
})
*/
test('The sum of the shortest paths between galaxies in the assignment is 374', (t) => {
const input = `...#......