Skip to content

Commit 6bf5071

Browse files
feat(2020-day-11): iterate seating with visibility rules
for part 2
1 parent 4bf36e7 commit 6bf5071

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

2020/day-11/seating.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,35 @@ const format = (seatMap) => {
1010
}
1111

1212
const occupiedLineOfSite = ({ x, y, seatMap }) => {
13+
let occupied = 0
14+
const look = ({ lookX, lookY, dirX, dirY }) => {
15+
if (lookY < 0 || lookY >= seatMap.length) {
16+
// exceeded rows space
17+
return 'x'
18+
}
19+
if (lookX < 0 || lookX >= seatMap[0].length) {
20+
// exceeded column space
21+
return 'x'
22+
}
23+
// Find the first seat in the direction, recursively
24+
if (seatMap[lookY][lookX] !== '.') {
25+
return seatMap[lookY][lookX]
26+
}
27+
28+
// Recursively look in the next seat in this direction
29+
return look({ lookX: lookX + dirX, lookY: lookY + dirY, dirX, dirY })
30+
}
1331

32+
// 8 compass point directions
33+
for (let dirX = -1; dirX <= 1; dirX++) {
34+
for (let dirY = -1; dirY <= 1; dirY++) {
35+
if (look({ lookX: x + dirX, lookY: y + dirY, dirX, dirY }) === '#') {
36+
occupied++
37+
}
38+
}
39+
}
40+
41+
return occupied
1442
}
1543

1644
const occupiedNearby = ({ x, y, seatMap }) => {
@@ -41,7 +69,7 @@ const advance = (seatMap, rules) => {
4169
})
4270
}
4371

44-
const update = ({ x, y, seatMap, rules }) => {
72+
const update = ({ x, y, seatMap, rules = 'proximity' }) => {
4573
let leaveThreshold = 4
4674
let processor = occupiedNearby
4775
if (rules === 'visibility') {

2020/day-11/seating.test.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,19 @@ L.#.L..#..
7575
#.#L#L#.##`
7676
)
7777

78-
const testDataPart2 = testData.slice(0, 2)
78+
const testDataPart2 = [testData[0]]
79+
testDataPart2.push(
80+
`#.##.##.##
81+
#######.##
82+
#.#.#..#..
83+
####.##.##
84+
#.##.##.##
85+
#.#####.##
86+
..#.#.....
87+
##########
88+
#.######.#
89+
#.#####.##`
90+
)
7991
testDataPart2.push(
8092
`#.LL.LL.L#
8193
#LLLLLL.LL
@@ -171,14 +183,14 @@ describe('--- Day 11: Seating System ---', () => {
171183
.........
172184
#........
173185
...#.....`
174-
expect(occupiedLineOfSite({ x: 1, y: 1, seatMap: data })).to.equal(8)
186+
expect(occupiedLineOfSite({ x: 3, y: 4, seatMap: parse(data) })).to.equal(8)
175187
})
176188
it('cannot see occupied seats past an available seat', () => {
177189
const data =
178190
`.............
179191
.L.L.#.#.#.#.
180192
.............`
181-
expect(occupiedLineOfSite({ x: 3, y: 4, seatMap: data })).to.equal(0)
193+
expect(occupiedLineOfSite({ x: 1, y: 1, seatMap: parse(data) })).to.equal(0)
182194
})
183195
it('can look in all compass directions', () => {
184196
const data =
@@ -189,21 +201,22 @@ describe('--- Day 11: Seating System ---', () => {
189201
##...##
190202
#.#.#.#
191203
.##.##.`
192-
expect(occupiedLineOfSite({ x: 3, y: 3, seatMap: data })).to.equal(0)
204+
expect(occupiedLineOfSite({ x: 3, y: 3, seatMap: parse(data) })).to.equal(0)
193205
})
194206
})
195207
describe('advance()', () => {
196208
it('accepts visibility rules instead of proximity', () => {
197209
const results = testDataPart2.map((data) => {
198210
return format(
199211
advance(
200-
parse(data), 'visible'
212+
parse(data), 'visibility'
201213
)
202214
)
203215
})
204216

205217
for (let x = 1; x < testDataPart2.length; x++) {
206218
console.debug('Step', x)
219+
console.debug(results[x - 1])
207220
expect(results[x - 1]).to.equal(testDataPart2[x])
208221
}
209222
const finalOccupancy = (results[results.length - 1].match(/#/g) || []).length

0 commit comments

Comments
 (0)