-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzle01.ts
80 lines (67 loc) · 2.1 KB
/
puzzle01.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
import { readFile } from 'node:fs/promises';
import path from 'node:path';
const ZERO_CODE = '0'.charCodeAt(0);
export async function solvePuzzle01() {
const content = await readFile(path.join('./input/puzzle01.txt'), {encoding: 'utf8'});
let sum = 0;
for (const line of content.split('\n')) {
const firstToken = findFirst(line, /[0-9]/);
const lastToken = findFirst(reverseString(line), /[0-9]/);
if (firstToken && lastToken) {
sum += (
(firstToken.charCodeAt(0) - ZERO_CODE) * 10 +
(lastToken.charCodeAt(0) - ZERO_CODE)
);
}
}
console.log(`Puzzle 01: ${sum}`);
}
export async function solvePuzzle01Advanced() {
const content = await readFile(path.join('./input/puzzle01.txt'), {encoding: 'utf8'});
const digits = [
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
];
const forwardRegex = new RegExp(`(?:[0-9]|${digits.join('|')})`);
const backwardRegex = new RegExp(`(?:[0-9]|${digits.map(reverseString).join('|')})`);
let sum = 0;
for (const line of content.split('\n')) {
let firstToken = findFirst(line, forwardRegex);
let lastToken = findFirst(reverseString(line), backwardRegex);
if (firstToken && lastToken) {
const firstDigit = tokenToDigit(firstToken, digits);
const lastDigit = tokenToDigit(reverseString(lastToken), digits);
sum += firstDigit * 10 + lastDigit;
}
}
console.log(`Puzzle 01 (advanced): ${sum}`);
}
function findFirst(line: string, regex: RegExp): string | undefined {
const match = regex.exec(line);
return match ? match[0] : undefined;
}
function reverseString(str: string): string {
return Array.from(str).reverse().join('');
}
function tokenToDigit(token: string, digits: readonly string[]): number {
if (/^[0-9]$/.test(token)) {
return token.charCodeAt(0) - ZERO_CODE;
} else {
const index = digits.indexOf(token);
if (index < 0) {
throw new Error(`Failed to find token among digits: ${token}`);
}
return index + 1;
}
}
(async function main() {
await solvePuzzle01();
await solvePuzzle01Advanced();
})();