Samuel Shuert | 274a4d6 | 2023-12-01 15:04:55 -0500 | [diff] [blame] | 1 | import { readFileSync } from 'fs' |
| 2 | |
| 3 | |
| 4 | const input = readFileSync('./input.txt').toString().split('\n'); |
| 5 | |
| 6 | const firstLast = (str: string) => { |
| 7 | const newStr = str |
| 8 | .replaceAll('one', 'o1e') |
| 9 | .replaceAll('two', 't2o') |
| 10 | .replaceAll('three', 't3e') |
| 11 | .replaceAll('four', 'f4r') |
| 12 | .replaceAll('five', 'f5e') |
| 13 | .replaceAll('six', 's6x') |
| 14 | .replaceAll('seven', 's7n') |
| 15 | .replaceAll('eight', 'e8t') |
| 16 | .replaceAll('nine', 'n9e') |
Samuel Shuert | 274a4d6 | 2023-12-01 15:04:55 -0500 | [diff] [blame] | 17 | const matches = newStr.match(/[\d]/g) |
| 18 | return matches ? [matches[0], matches[matches.length-1]].join('') : null |
| 19 | } |
| 20 | |
| 21 | let int: number = 0; |
| 22 | |
| 23 | for(const line of input) { |
| 24 | int += parseInt(firstLast(line) ?? "0") |
| 25 | } |
| 26 | console.log(int) |