advent-of-code

Entries to advent of code, multiple years
git clone git://git.finwo.net/misc/advent-of-code
Log | Files | Refs

commit e77d3288cff330a6b2cd1c658c18460819ed4231
parent d522cbdc3ea411ff6ed51dc2cc5859eeef79d577
Author: finwo <finwo@pm.me>
Date:   Fri, 23 Dec 2022 17:57:48 +0100

2022/23 solution

Diffstat:
A2022/day-23/index.js | 158+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day-23/sample | 7+++++++
A2022/day-23/text | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 245 insertions(+), 0 deletions(-)

diff --git a/2022/day-23/index.js b/2022/day-23/index.js @@ -0,0 +1,158 @@ +const fs = require('fs'); + +const lines = fs.readFileSync('input', 'utf-8') +// const lines = fs.readFileSync('sample', 'utf-8') + .split('\n') + +const elves = []; +for(let y=0; y<lines.length; y++) { + const line = lines[y]; + for(let x=0; x<line.length; x++) { + if (line.charAt(x) == '#') { + elves.push({ + x, + y, + }); + } + } +} + +function adjacent(grid, origin) { + const neighbours = []; + for(let y=origin.y-1; y<=origin.y+1; y++) { + if (!grid[y]) continue; + for(let x=origin.x-1; x<=origin.x+1; x++) { + if (y == origin.y && x == origin.x) continue; + if (grid[y][x]) neighbours.push(grid[y][x]); + } + } + return neighbours; +} + +let step1 = 0; +const moves = ['N','S','W','E']; + +const test = { + N: origin => subject => subject.y < origin.y, + S: origin => subject => subject.y > origin.y, + W: origin => subject => subject.x < origin.x, + E: origin => subject => subject.x > origin.x, +}; + +function drawmap(elves) { + let output = '\n'; + let minx = Infinity; + let miny = Infinity; + let maxx = -Infinity; + let maxy = -Infinity; + for(const elf of elves) { + if (elf.x < minx) minx = elf.x; + if (elf.y < miny) miny = elf.y; + if (elf.x > maxx) maxx = elf.x; + if (elf.y > maxy) maxy = elf.y; + } + for(let y=miny; y<=maxy; y++) { + for(let x=minx; x<=maxx; x++) { + if (elves.find(elf => elf.x == x && elf.y == y)) { + output += '#'; + } else { + output += ' '; + } + } + output += '\n'; + } + process.stdout.write(output); +} + +const dirdiff = { + N: [ 0, -1], + S: [ 0, 1], + W: [-1, 0], + E: [ 1, 0], +}; + +// drawmap(elves); + +const grid = []; +for(const elf of elves) { + grid[elf.y] = grid[elf.y] || []; + grid[elf.y][elf.x] = elf; +} + +(async () => { + let proposals = []; + let rounds = 0; + do { + proposals = []; + + // stage 1, make proposals + for(const elf of elves) { + // Only make a proposal if neighbours are found + const neighbours = adjacent(grid, elf); + if (!neighbours.length) continue; + for(const direction of moves) { + if (neighbours.find(test[direction](elf))) continue; + proposals.push({ + elf, + direction, + x: elf.x + dirdiff[direction][0], + y: elf.y + dirdiff[direction][1], + }); + break; + } + } + + // stage 2, count proposals per location + let positions = []; + for(const proposal of proposals) { + if (!positions[proposal.y]) positions[proposal.y] = []; + positions[proposal.y][proposal.x] = (positions[proposal.y][proposal.x]||0) + 1; + } + + // stage 3, move the elves + for(const proposal of proposals) { + if (positions[proposal.y][proposal.x] > 1) continue; + grid[proposal.elf.y][proposal.elf.x] = false; + grid[proposal.y] = grid[proposal.y] || []; + grid[proposal.y][proposal.x] = proposal.elf; + proposal.elf.x = proposal.x; + proposal.elf.y = proposal.y; + } + + // stage 4, moves + moves.push(moves.shift()); + + // Round counter + rounds++; + + // if (rounds % 1e1 == 0) { + // drawmap(elves); + // console.log(proposals.length); + // } + + if (rounds == 10) { + // Find area to check + let minx = Infinity; + let miny = Infinity; + let maxx = -Infinity; + let maxy = -Infinity; + for(const elf of elves) { + if (elf.x < minx) minx = elf.x; + if (elf.y < miny) miny = elf.y; + if (elf.x > maxx) maxx = elf.x; + if (elf.y > maxy) maxy = elf.y; + } + + // Calculate empty area + const width = maxx - minx + 1; + const height = maxy - miny + 1; + const area = (width * height) - elves.length; + console.log({ area }); + } + + } while(proposals.length); + + + console.log({ elves: elves.length, rounds }); + +})(); diff --git a/2022/day-23/sample b/2022/day-23/sample @@ -0,0 +1,7 @@ +....#.. +..###.# +#...#.# +.#...## +#.###.. +##.#.## +.#..#.. diff --git a/2022/day-23/text b/2022/day-23/text @@ -0,0 +1,80 @@ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################