Skip to content

Commit 4bf36e7

Browse files
feat(2020-day-11): support advancing with different rulesets
1 parent e9c58f3 commit 4bf36e7

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

2020/day-11/seating.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,22 @@ const occupiedNearby = ({ x, y, seatMap }) => {
3333
return occupied
3434
}
3535

36-
const advance = (seatMap) => {
36+
const advance = (seatMap, rules) => {
3737
return seatMap.map((row, y) => {
3838
return row.map((col, x) => {
39-
return update({ x, y, seatMap })
39+
return update({ x, y, seatMap, rules })
4040
})
4141
})
4242
}
4343

44-
const update = ({ x, y, seatMap }) => {
44+
const update = ({ x, y, seatMap, rules }) => {
45+
let leaveThreshold = 4
46+
let processor = occupiedNearby
47+
if (rules === 'visibility') {
48+
leaveThreshold = 5
49+
processor = occupiedLineOfSite
50+
}
51+
4552
let next = seatMap[y][x]
4653
switch (seatMap[y][x]) {
4754
case '.':
@@ -50,14 +57,14 @@ const update = ({ x, y, seatMap }) => {
5057
break
5158
case 'L':
5259
// If a seat is empty (L) and there are no occupied seats adjacent to it, the seat becomes occupied.
53-
if (occupiedNearby({ x, y, seatMap }) === 0) {
60+
if (processor({ x, y, seatMap }) === 0) {
5461
next = '#'
5562
}
5663
break
5764
case '#':
5865
// If a seat is occupied (#) and four or more seats adjacent to it are also occupied, the seat becomes empty.
5966
// We subtract 1 so we don't count the target seat
60-
if (occupiedNearby({ x, y, seatMap }) - 1 >= 4) {
67+
if (processor({ x, y, seatMap }) - 1 >= leaveThreshold) {
6168
next = 'L'
6269
}
6370
break

0 commit comments

Comments
 (0)