-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
108 lines (84 loc) · 2.56 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
type Seat = [number, number];
type SeatRange = {
row: [number, number];
column: [number, number];
};
enum BoardingPassIndications {
F = "F",
B = "B",
R = "R",
L = "L",
}
const PLANE_ROWS = 128;
const PLANE_COLUMNS = 8;
const getSeatFromRange = ({ row, column }: SeatRange) => {
const seat: Seat = [0, 0];
if (row[0] === row[1]) {
seat[0] = row[0];
} else {
throw new Error(`Rows ${row[0]} and ${row[1]} are different`);
}
if (column[0] === column[1]) {
seat[1] = column[0];
} else {
throw new Error(`Columns ${column[0]} and ${column[1]} are different`);
}
return seat;
};
export const decodeBoardingPass = (passCode: string): Seat => {
const seatIndications = passCode.split("") as BoardingPassIndications[];
const initialValue: SeatRange = {
row: [0, PLANE_ROWS - 1],
column: [0, PLANE_COLUMNS - 1],
};
const { row, column } = seatIndications.reduce(
({ row, column }, indication): SeatRange => {
const [minRow, maxRow] = row;
const [minColumn, maxColumn] = column;
const middleRow = Math.round((maxRow - minRow) / 2);
const middleColumn = Math.round((maxColumn - minColumn) / 2);
if (indication === BoardingPassIndications.F) {
return { column, row: [minRow, maxRow - middleRow] };
}
if (indication === BoardingPassIndications.B) {
return { column, row: [minRow + middleRow, maxRow] };
}
if (indication === BoardingPassIndications.L) {
return { row, column: [minColumn, maxColumn - middleColumn] };
}
if (indication === BoardingPassIndications.R) {
return { row, column: [minColumn + middleColumn, maxColumn] };
}
return { row, column };
},
initialValue
);
return getSeatFromRange({ row, column });
};
export const calculateSeatId = ([row, column]: Seat) =>
row * PLANE_COLUMNS + column;
const findGreaterSeatId = (acc: number, seatId: number) =>
acc < seatId ? seatId : acc;
export const part1 = (input: string): number =>
input
.split("\n")
.map(decodeBoardingPass)
.map(calculateSeatId)
.reduce(findGreaterSeatId, -Infinity);
export const part2 = (input: string): number => {
const allSeatIds = input
.split("\n")
.map(decodeBoardingPass)
.map(calculateSeatId);
const greaterSeatId = allSeatIds.reduce(findGreaterSeatId, -Infinity);
for (
let seatPointer = PLANE_COLUMNS;
seatPointer <= greaterSeatId;
seatPointer++
) {
if (!allSeatIds.includes(seatPointer)) {
return seatPointer;
}
}
throw new Error("All seats are filled");
};