@@ -33,15 +33,22 @@ const occupiedNearby = ({ x, y, seatMap }) => {
33
33
return occupied
34
34
}
35
35
36
- const advance = ( seatMap ) => {
36
+ const advance = ( seatMap , rules ) => {
37
37
return seatMap . map ( ( row , y ) => {
38
38
return row . map ( ( col , x ) => {
39
- return update ( { x, y, seatMap } )
39
+ return update ( { x, y, seatMap, rules } )
40
40
} )
41
41
} )
42
42
}
43
43
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
+
45
52
let next = seatMap [ y ] [ x ]
46
53
switch ( seatMap [ y ] [ x ] ) {
47
54
case '.' :
@@ -50,14 +57,14 @@ const update = ({ x, y, seatMap }) => {
50
57
break
51
58
case 'L' :
52
59
// 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 ) {
54
61
next = '#'
55
62
}
56
63
break
57
64
case '#' :
58
65
// If a seat is occupied (#) and four or more seats adjacent to it are also occupied, the seat becomes empty.
59
66
// 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 ) {
61
68
next = 'L'
62
69
}
63
70
break
0 commit comments