-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
122 lines (117 loc) · 3.04 KB
/
index.ts
File metadata and controls
122 lines (117 loc) · 3.04 KB
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
/**
* Builds a number using the given digits.
* If the digits array is empty, returns 0.
* If the digits array has only one element, returns a number formed by repeating that element twice.
* Otherwise, returns a number formed by concatenating the first and last digits of the array.
* @param digits - An array of digits.
* @returns The built number.
*/
const buildNumber = (digits: number[]): number => {
if (digits.length === 0) {
return 0;
}
if (digits.length === 1) {
return parseInt(digits[0].toString() + digits[0].toString(), 10);
} else {
return parseInt(
digits[0].toString() + digits[digits.length - 1].toString(),
10
);
}
};
/**
* Converts a string containing words representing numbers to a string containing the corresponding numeric digits.
*
* @param line - The input string to convert.
* @returns The converted string with numeric digits.
*/
const convertStringsToNumbers = (line: string): string => {
let newLine = "";
for (let j = 0; j < line.length; j++) {
if (line[j] === "o" && line[j + 1] === "n" && line[j + 2] === "e") {
newLine += "1";
}
if (line[j] === "t" && line[j + 1] === "w" && line[j + 2] === "o") {
newLine += "2";
}
if (
line[j] === "t" &&
line[j + 1] === "h" &&
line[j + 2] === "r" &&
line[j + 3] === "e" &&
line[j + 4] === "e"
) {
newLine += "3";
}
if (
line[j] === "f" &&
line[j + 1] === "o" &&
line[j + 2] === "u" &&
line[j + 3] === "r"
) {
newLine += "4";
}
if (
line[j] === "f" &&
line[j + 1] === "i" &&
line[j + 2] === "v" &&
line[j + 3] === "e"
) {
newLine += "5";
}
if (line[j] === "s" && line[j + 1] === "i" && line[j + 2] === "x") {
newLine += "6";
}
if (
line[j] === "s" &&
line[j + 1] === "e" &&
line[j + 2] === "v" &&
line[j + 3] === "e" &&
line[j + 4] === "n"
) {
newLine += "7";
}
if (
line[j] === "e" &&
line[j + 1] === "i" &&
line[j + 2] === "g" &&
line[j + 3] === "h" &&
line[j + 4] === "t"
) {
newLine += "8";
}
if (
line[j] === "n" &&
line[j + 1] === "i" &&
line[j + 2] === "n" &&
line[j + 3] === "e"
) {
newLine += "9";
}
if (
line[j] === "z" &&
line[j + 1] === "e" &&
line[j + 2] === "r" &&
line[j + 3] === "o"
) {
newLine += "0";
}
if (isFinite(parseInt(line[j], 10))) {
newLine += line[j];
}
}
return newLine;
};
// get the input data
const data = await Bun.file("ettore.txt").text();
const lines = data.split("\n");
// parse the input data
const convertedLines = lines.map((line) => convertStringsToNumbers(line));
// console.log(convertedLines);
const numbers = convertedLines.map((line) =>
Array.from(line).map((c) => parseInt(c, 10))
);
const filteredNumbers = numbers.map((line) => line.filter((n) => !isNaN(n)));
// build the result
const result = filteredNumbers.map((line) => buildNumber(line));
console.log(result);