Last active
December 11, 2020 14:56
-
-
Save cranderveldt/79b4094c90bc6a74bcd39f5e2d523dbb to your computer and use it in GitHub Desktop.
Advent of Code 2020 Day 11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puzzleInput = `` | |
// Common | |
const parseInput = (input) => { | |
return input.split(/\n/).map(x => x.split('')) | |
} | |
const considerSurroundings = (rows, rowIndex, colIndex, comparator, maxDistance, threshold) => { | |
const range = [-1, 0, 1] | |
return range.reduce((acc1, yOffset) => ( | |
acc1 + range.reduce((acc2, xOffset) => { | |
if (yOffset === 0 && xOffset === 0) { return acc2 } | |
let seat | |
for (let distance = 1; distance < maxDistance; distance++) { | |
const rowOffset = rowIndex + (yOffset * distance) | |
const colOffset = colIndex + (xOffset * distance) | |
seat = rows[rowOffset] && rows[rowOffset][colOffset] | |
if (seat !== '.') { break } | |
} | |
return acc2 + +comparator(seat) | |
}, 0) | |
), 0) >= threshold | |
} | |
const shouldGetUp = (rows, rowIndex, colIndex, maxDistance, threshold) => { | |
return considerSurroundings(rows, rowIndex, colIndex, (seat => seat === '#'), maxDistance, threshold) ? 'L' : '#' | |
} | |
const shouldSitDown = (rows, rowIndex, colIndex, maxDistance) => { | |
return considerSurroundings(rows, rowIndex, colIndex, (seat => seat !== '#'), maxDistance, 8) ? '#' : 'L' | |
} | |
const countOccupiedSeats = rows => { | |
return (stringifyRows(rows).match(/\#/gi) || []).length | |
} | |
const stringifyRows = rows => { | |
return rows.map(y => y.join('')).join('\n') | |
} | |
const atLeastOneSeatChange = (rows1, rows2) => { | |
return stringifyRows(rows1) !== stringifyRows(rows2) | |
} | |
const reachEqualibrium = (rows, changedSeats, fillFn) => { | |
if (!changedSeats) { return countOccupiedSeats(rows) } | |
const newRows = rows.map(fillFn) | |
return reachEqualibrium(newRows, atLeastOneSeatChange(rows, newRows), fillFn) | |
} | |
// Part 1 | |
const fillSeatsPartOne = (row, rowIndex, rows) => { | |
return row.map((col, colIndex) => { | |
switch (col) { | |
case 'L': return shouldSitDown(rows, rowIndex, colIndex, 2) | |
case '#': return shouldGetUp(rows, rowIndex, colIndex, 2, 4) | |
default: return col | |
} | |
}) | |
} | |
const partOne = (input) => { | |
const rows = parseInput(input) | |
const occupied = reachEqualibrium(rows, true, fillSeatsPartOne) | |
console.log(occupied) | |
} | |
partOne(puzzleInput) | |
// Part 2 | |
const fillSeatsPartTwo = (row, rowIndex, rows) => { | |
return row.map((col, colIndex) => { | |
switch (col) { | |
case 'L': return shouldSitDown(rows, rowIndex, colIndex, rows.length) | |
case '#': return shouldGetUp(rows, rowIndex, colIndex, rows.length, 5) | |
default: return col | |
} | |
}) | |
} | |
const partTwo = (input) => { | |
const rows = parseInput(input) | |
const occupied = reachEqualibrium(rows, true, fillSeatsPartTwo) | |
console.log(occupied) | |
} | |
partTwo(puzzleInput) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment