/
day11.js
152 lines (122 loc) · 4.08 KB
/
day11.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const { dirxml } = require('console');
const fs = require('fs');
const lines = fs.readFileSync('day11.txt', {encoding: 'utf-8'}).split('\n').filter(x => x);
class Seating {
constructor(lines) {
this.height = lines.length;
this.width = lines[0].length;
this.seats = lines;
}
nextState() {
let hasChanged = false;
const updatedSeats = [];
this.seats.forEach((line, y) => {
let updated = '';
[...line].forEach((seat, x) => {
let occupied = 0;
for (let i = -1; i <= 1; i++) {
for(let j = -1; j <= 1; j++) {
if((i != 0 || j != 0)
&& y+i >= 0
&& y+i < this.height
&& x+j >= 0
&& x+j < this.width
&& this.seats[y+i][x+j] === '#') {
occupied++;
}
}
}
if(seat == 'L' && occupied === 0) {
// If a seat is empty (L) and there are no occupied seats adjacent to it,
// the seat becomes occupied.
updated += '#';
hasChanged = true;
} else if(seat === '#' && occupied >= 4) {
// If a seat is occupied (#) and four or more seats adjacent to it are also occupied,
// the seat becomes empty.
updated += 'L';
hasChanged = true;
} else {
// Otherwise, the seat's state does not change.
updated += seat;
}
})
updatedSeats.push(updated);
})
this.seats = updatedSeats;
return hasChanged;
}
nextState2() {
let hasChanged = false;
const updatedSeats = [];
this.seats.forEach((line, y) => {
let updated = '';
[...line].forEach((seat, x) => {
let occupied = 0;
const directions = [
{x: 1, y:0}, {x: -1, y:0},
{x: 1, y:1}, {x: -1, y:-1},
{x: 1, y:-1}, {x: -1, y:1},
{x: 0, y:1}, {x: 0, y:-1}
];
directions.forEach(({x: dx, y: dy}) => {
let posX = x+dx;
let posY = y+dy;
while(posX >= 0 && posY >= 0 && posX < this.width && posY < this.height) {
if(this.seats[posY][posX] === '#') {
occupied++;
break;
}
if(this.seats[posY][posX] === 'L') {
break;
}
posX += dx;
posY += dy;
}
})
if(seat == 'L' && occupied === 0) {
updated += '#';
hasChanged = true;
} else if(seat === '#' && occupied >= 5) {
updated += 'L';
hasChanged = true;
} else {
updated += seat;
}
})
updatedSeats.push(updated);
})
this.seats = updatedSeats;
return hasChanged;
}
getOccupiedSeats() {
let occupied = 0;
this.seats.forEach((line) => {
[...line].forEach((seat) => {
if(seat === '#') {
occupied++;
}
})
})
return occupied;
}
display() {
this.seats.forEach(line => {
console.log(line);
});
}
}
const s = new Seating(lines);
// s.display();
while(s.nextState()) {
//do nothing
}
// s.display();
console.log(s.getOccupiedSeats());
const s2 = new Seating(lines);
while(s2.nextState2()) {
//do nothing
// s2.display();
// console.log('---')
}
console.log(s2.getOccupiedSeats());