···11+/* each line originally contained a specific calibration value that the Elves now need to recover. On each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.
22+33+For example:
44+1abc2
55+pqr3stu8vwx
66+a1b2c3d4e5f
77+treb7uchet
88+99+In this example, the calibration values of these four lines are 12, 38, 15, and 77. Adding these together produces 142.
1010+*/
1111+const input = await Bun.file('1.input').text();
1212+const splitInput = input.split('\n');
1313+let counter = 0;
1414+for (let line of splitInput) {
1515+ line = line.replaceAll(/[a-z]/g, '');
1616+ const firstDigit = line.at(0);
1717+ const lastDigit = line.at(-1);
1818+ const lineValue = parseInt(`${firstDigit}${lastDigit}`, 10);
1919+ counter += lineValue;
2020+}
2121+2222+console.log(counter);
+49
1part2.ts
···11+/* Your calculation isn't quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid "digits".
22+33+Equipped with this new information, you now need to find the real first and last digit on each line. For example:
44+55+two1nine
66+eightwothree
77+abcone2threexyz
88+xtwone3four
99+4nineeightseven2
1010+zoneight234
1111+7pqrstsixteen
1212+1313+In this example, the calibration values are 29, 83, 13, 24, 42, 14, and 76. Adding these together produces 281.
1414+1515+What is the sum of all of the calibration values? */
1616+1717+const digitMap = {
1818+ one: '1',
1919+ two: '2',
2020+ three: '3',
2121+ four: '4',
2222+ five: '5',
2323+ six: '6',
2424+ seven: '7',
2525+ eight: '8',
2626+ nine: '9',
2727+};
2828+2929+const allNumbersRegex = `(${Object.keys(digitMap).join('|')}|${Object.values(digitMap).join('|')})`;
3030+const input = await Bun.file('1.input').text();
3131+// const input = await Bun.file('1part2test.input').text();
3232+const splitInput = input.split('\n');
3333+let counter = 0;
3434+for (let line of splitInput) {
3535+ // positive lookahead wizardry needed to capture things like "twone"
3636+ // https://stackoverflow.com/a/33903830
3737+ const regex = new RegExp(`(?=${allNumbersRegex})`, 'g');
3838+ const matches = Array.from(line.matchAll(regex), (x) => x[1]);
3939+ let firstDigit = matches.at(0);
4040+ let lastDigit = matches.at(-1);
4141+ for (const [key, value] of Object.entries(digitMap)) {
4242+ firstDigit = firstDigit!.replace(key, value);
4343+ lastDigit = lastDigit!.replace(key, value);
4444+ }
4545+ const lineValue = parseInt(`${firstDigit}${lastDigit}`, 10);
4646+ counter += lineValue;
4747+}
4848+4949+console.log(counter);
···11+# Advent of Code 2023
22+33+To install dependencies:
44+55+```bash
66+bun install
77+```
88+99+To run:
1010+1111+```bash
1212+bun run index.ts
1313+```
1414+1515+This project was created using `bun init` in bun v1.0.14. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.