-
Notifications
You must be signed in to change notification settings - Fork 0
/
part-1.js
97 lines (78 loc) · 2.55 KB
/
part-1.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
/*
Seating rules:
- If a seat is empty and there are no occupied seats adjacent to it, the seat
becomes occupied.
- If a seat is occupied and four or more seats adjacent to it are also occupied,
the seat becomes empty.
- Otherwise, the seat's state does not change.
Simulate your seating area by applying the seating rules repeatedly until no
seats change state. How many seats end up occupied?
*/
import readFile from '../read-file.js';
const seats = [];
await readFile(new URL('./input.txt', import.meta.url), (line) => {
seats.push(
line
.split('')
.map((char) => ({ type: char === 'L' ? 'empty seat' : 'floor' }))
);
});
const getSeat = (rowIndex, columnIndex) => {
const row = seats[rowIndex];
if (row) return row[columnIndex];
return undefined;
};
const getAdjacentSeats = (rowIndex, columnIndex) => {
const adjacentSeatIndexes = [
[rowIndex - 1, columnIndex - 1],
[rowIndex - 1, columnIndex],
[rowIndex - 1, columnIndex + 1],
[rowIndex, columnIndex - 1],
[rowIndex, columnIndex + 1],
[rowIndex + 1, columnIndex - 1],
[rowIndex + 1, columnIndex],
[rowIndex + 1, columnIndex + 1],
];
return adjacentSeatIndexes.map((index) => getSeat(...index));
};
const markRule = (rowIndex, columnIndex) => {
const seat = getSeat(rowIndex, columnIndex);
if (seat.type === 'floor') return null;
const adjacentSeats = getAdjacentSeats(rowIndex, columnIndex);
const occupiedAdjacentSeats = adjacentSeats.filter(
(seat_) => seat_ && seat_.type === 'occupied seat'
);
if (seat.type === 'empty seat' && occupiedAdjacentSeats.length === 0) {
seat.nextType = 'occupied seat';
return seat;
}
if (seat.type === 'occupied seat' && occupiedAdjacentSeats.length >= 4) {
seat.nextType = 'empty seat';
return seat;
}
return null;
};
const applyRule = (seat) => {
/* eslint-disable no-param-reassign */
seat.type = seat.nextType;
delete seat.nextType;
};
const runAllRules = () => {
const changedSeats = [];
for (let rowIndex = 0; rowIndex < seats.length; rowIndex += 1) {
for (let columnIndex = 0; columnIndex < seats[0].length; columnIndex += 1) {
const ruleResult = markRule(rowIndex, columnIndex);
if (ruleResult) changedSeats.push(ruleResult);
}
}
changedSeats.forEach(applyRule);
return changedSeats.length;
};
// eslint-disable-next-line no-constant-condition
while (true) {
const changedSeatsCount = runAllRules();
if (changedSeatsCount === 0) break;
}
const result = seats.flat().filter((seat) => seat.type === 'occupied seat')
.length;
console.log(result);