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