-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
165 lines (133 loc) Β· 3.74 KB
/
index.ts
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
153
154
155
156
157
158
159
160
161
162
163
164
165
type Board = string[][];
enum SeatStatuses {
EMPTY = "L",
OCCUPIED = "#",
FLOOR = ".",
}
type SeatPosition = [number, number];
type Vector = [number, number];
const ADJACENT_VECTORS: Vector[] = [
[-1, -1],
[-1, 0],
[-1, +1],
[0, +1],
[+1, +1],
[+1, 0],
[+1, -1],
[0, -1],
];
const safeGetSeat = (board: Board, seatPosition: SeatPosition) => {
try {
const [row, column] = seatPosition;
return board[row][column];
} catch (err) {
return null;
}
};
const countOccupiedSeats = (
board: Board,
seatPosition: SeatPosition,
checkVectors: Vector[]
): number => {
let count = 0;
const [row, column] = seatPosition;
for (let [v1, v2] of checkVectors) {
const seatStatus = safeGetSeat(board, [row + v1, column + v2]);
if (seatStatus === SeatStatuses.OCCUPIED) {
count += 1;
}
}
return count;
};
export const part1 = (input: string) => {
let currentBoard: Board = input.split("\n").map((row) => row.split(""));
let nextBoard: Board = [];
let hadChanges = true;
while (hadChanges) {
hadChanges = false;
for (let row = 0; row < currentBoard.length; row++) {
const nextRow = [];
for (let column = 0; column < currentBoard[0].length; column++) {
const occupiedSeats = countOccupiedSeats(
currentBoard,
[row, column],
ADJACENT_VECTORS
);
const currentStatus = currentBoard[row][column];
if (occupiedSeats === 0 && currentStatus === SeatStatuses.EMPTY) {
hadChanges = true;
nextRow.push(SeatStatuses.OCCUPIED);
} else if (
occupiedSeats >= 4 &&
currentStatus === SeatStatuses.OCCUPIED
) {
hadChanges = true;
nextRow.push(SeatStatuses.EMPTY);
} else {
nextRow.push(currentStatus);
}
}
nextBoard.push(nextRow);
}
currentBoard = nextBoard;
nextBoard = [];
}
return currentBoard
.flat()
.reduce((acc, seat) => (seat === SeatStatuses.OCCUPIED ? acc + 1 : acc), 0);
};
const countVisibleOccupiedSeats = (
board: Board,
seatPosition: SeatPosition,
checkVectors: Vector[]
) => {
let count = 0;
const [row, column] = seatPosition;
for (let [v1, v2] of checkVectors) {
const checkingPosition: Vector = [row + v1, column + v2];
const seatStatus = safeGetSeat(board, checkingPosition);
if (seatStatus === SeatStatuses.OCCUPIED) {
count += 1;
} else if (seatStatus === SeatStatuses.FLOOR) {
count += countVisibleOccupiedSeats(board, checkingPosition, [[v1, v2]]);
}
}
return count;
};
export const part2 = (input: string) => {
let currentBoard: Board = input.split("\n").map((row) => row.split(""));
let nextBoard: Board = [];
let hadChanges = true;
while (hadChanges) {
hadChanges = false;
for (let row = 0; row < currentBoard.length; row++) {
const nextRow = [];
for (let column = 0; column < currentBoard[0].length; column++) {
const occupiedSeats = countVisibleOccupiedSeats(
currentBoard,
[row, column],
ADJACENT_VECTORS
);
const currentStatus = currentBoard[row][column];
if (occupiedSeats === 0 && currentStatus === SeatStatuses.EMPTY) {
hadChanges = true;
nextRow.push(SeatStatuses.OCCUPIED);
} else if (
occupiedSeats >= 5 &&
currentStatus === SeatStatuses.OCCUPIED
) {
hadChanges = true;
nextRow.push(SeatStatuses.EMPTY);
} else {
nextRow.push(currentStatus);
}
}
nextBoard.push(nextRow);
}
currentBoard = nextBoard;
nextBoard = [];
}
return currentBoard
.flat()
.reduce((acc, seat) => (seat === SeatStatuses.OCCUPIED ? acc + 1 : acc), 0);
};