52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
const numberTable: Record<string, string> = {
|
|
"one": "1",
|
|
"two": "2",
|
|
"three": "3",
|
|
"four": "4",
|
|
"five": "5",
|
|
"six": "6",
|
|
"seven": "7",
|
|
"eight": "8",
|
|
"nine": "9"
|
|
}
|
|
const matchBackTable: Record<string, number> = {
|
|
// `eight` is started with spell e. `oneight` is one, eight.
|
|
"one": 1,
|
|
"two": 1,
|
|
"three": 1,
|
|
"four": 0,
|
|
"five": 1,
|
|
}
|
|
function findFirstNumber(line: string) {
|
|
for (let i = 0; i < line.length; i++) {
|
|
const front = line.slice(i);
|
|
|
|
}
|
|
}
|
|
|
|
function transformLine(line: string) {
|
|
// find first number
|
|
const re = /[0-9]+/;
|
|
const firstNumber = line.match(re)?.[0];
|
|
console.log(line);
|
|
console.log(firstNumber);
|
|
return parseInt(firstNumber || "");
|
|
|
|
const numbersExp = line.matchAll(re);
|
|
const numbers = [...numbersExp].map(n => {
|
|
if (n[0] in numberTable) {
|
|
return numberTable[n[0]];
|
|
}
|
|
return n[0];
|
|
});
|
|
console.log(line)
|
|
console.log(numbers);
|
|
return parseInt(numbers[0] + numbers[numbers.length - 1]);
|
|
}
|
|
|
|
transformLine("eightwothree")
|
|
|
|
// const input = await Deno.readTextFile("input.txt");
|
|
// const lines = input.split("\n");
|
|
// const numbers = lines.map(transformLine);
|
|
// console.log(numbers.reduce((a, b) => a + b));
|