Skip to content

Commit 4e67a03

Browse files
feat(2020-day-11): advance seating configurations
1 parent 52d921f commit 4e67a03

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

2020/day-11/seating.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
const parse = (data) => {
3+
return data.split('\n').map((row) => {
4+
return row.split('')
5+
})
6+
}
7+
8+
const format = (seatMap) => {
9+
return seatMap.map((row) => row.join('')).join('\n')
10+
}
11+
12+
const occupiedNearby = ({ x, y, seatMap }) => {
13+
let temp = ''
14+
15+
for (let row = y - 1; row <= y + 1; row++) {
16+
for (let col = x - 1; col <= x + 1; col++) {
17+
try {
18+
temp += seatMap[row][col] || '-'
19+
} catch {
20+
temp += '-'
21+
}
22+
}
23+
temp += '\n'
24+
}
25+
const occupied = (temp.match(/#/g) || []).length
26+
27+
// console.debug(temp)
28+
// console.debug(`------${occupied}------------`)
29+
return occupied
30+
}
31+
32+
const advance = (seatMap) => {
33+
return seatMap.map((row, y) => {
34+
return row.map((col, x) => {
35+
return update({ x, y, seatMap })
36+
})
37+
})
38+
}
39+
40+
const update = ({ x, y, seatMap }) => {
41+
let next = seatMap[y][x]
42+
switch (seatMap[y][x]) {
43+
case '.':
44+
// Floor (.) never changes; seats don't move, and nobody sits on the floor.
45+
next = seatMap[y][x]
46+
break
47+
case 'L':
48+
// If a seat is empty (L) and there are no occupied seats adjacent to it, the seat becomes occupied.
49+
if (occupiedNearby({ x, y, seatMap }) === 0) {
50+
next = '#'
51+
}
52+
break
53+
case '#':
54+
// If a seat is occupied (#) and four or more seats adjacent to it are also occupied, the seat becomes empty.
55+
// We subtract 1 so we don't count the target seat
56+
if (occupiedNearby({ x, y, seatMap }) - 1 >= 4) {
57+
next = 'L'
58+
}
59+
break
60+
default:
61+
// Otherwise, the seat's state does not change.
62+
next = seatMap[y][x]
63+
}
64+
65+
return next
66+
}
67+
68+
module.exports = {
69+
format,
70+
parse,
71+
advance
72+
}

2020/day-11/seating.test.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { format, parse, advance } = require('./seating')
4+
5+
const testData = [
6+
`L.LL.LL.LL
7+
LLLLLLL.LL
8+
L.L.L..L..
9+
LLLL.LL.LL
10+
L.LL.LL.LL
11+
L.LLLLL.LL
12+
..L.L.....
13+
LLLLLLLLLL
14+
L.LLLLLL.L
15+
L.LLLLL.LL`
16+
]
17+
testData.push(
18+
`#.##.##.##
19+
#######.##
20+
#.#.#..#..
21+
####.##.##
22+
#.##.##.##
23+
#.#####.##
24+
..#.#.....
25+
##########
26+
#.######.#
27+
#.#####.##`
28+
)
29+
testData.push(
30+
`#.LL.L#.##
31+
#LLLLLL.L#
32+
L.L.L..L..
33+
#LLL.LL.L#
34+
#.LL.LL.LL
35+
#.LLLL#.##
36+
..L.L.....
37+
#LLLLLLLL#
38+
#.LLLLLL.L
39+
#.#LLLL.##`
40+
)
41+
testData.push(
42+
`#.##.L#.##
43+
#L###LL.L#
44+
L.#.#..#..
45+
#L##.##.L#
46+
#.##.LL.LL
47+
#.###L#.##
48+
..#.#.....
49+
#L######L#
50+
#.LL###L.L
51+
#.#L###.##`
52+
)
53+
testData.push(
54+
`#.#L.L#.##
55+
#LLL#LL.L#
56+
L.L.L..#..
57+
#LLL.##.L#
58+
#.LL.LL.LL
59+
#.LL#L#.##
60+
..L.L.....
61+
#L#LLLL#L#
62+
#.LLLLLL.L
63+
#.#L#L#.##`
64+
)
65+
testData.push(
66+
`#.#L.L#.##
67+
#LLL#LL.L#
68+
L.#.L..#..
69+
#L##.##.L#
70+
#.#L.LL.LL
71+
#.#L#L#.##
72+
..L.L.....
73+
#L#L##L#L#
74+
#.LLLLLL.L
75+
#.#L#L#.##`
76+
)
77+
78+
describe('--- Day 11: Seating System ---', () => {
79+
describe('Part 1', () => {
80+
describe('advance()', () => {
81+
it('advances the seating state', () => {
82+
const results = testData.map((data) => {
83+
return format(
84+
advance(
85+
parse(data)
86+
)
87+
)
88+
})
89+
90+
for (let x = 1; x < testData.length; x++) {
91+
console.debug('Step', x)
92+
expect(results[x - 1]).to.equal(testData[x])
93+
}
94+
const finalOccupancy = (results[results.length - 1].match(/#/g) || []).length
95+
expect(finalOccupancy).to.equal(37)
96+
})
97+
})
98+
})
99+
})

0 commit comments

Comments
 (0)