Initial Commit
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="2023-01" type="BunRunConfiguration">
|
||||
<option name="program" value="2023/01/run.ts" />
|
||||
<option name="workingDirectory" value="$PROJECT_DIR$" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
@@ -0,0 +1,22 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import { solveFirst, solveSecond } from './index.ts';
|
||||
|
||||
describe('2023-01', () => {
|
||||
it('first', () => {
|
||||
const testInput = `1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet`;
|
||||
expect(solveFirst(testInput)).toBe(142);
|
||||
});
|
||||
it('second', () => {
|
||||
const testInput = `two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen`;
|
||||
expect(solveSecond(testInput)).toBe(281);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { sumArray } from '../../utils/array.ts';
|
||||
|
||||
export function solveFirst(input: string): number {
|
||||
const regex = /^.*?(\d).*(\d).*?$|^.*(\d).*$/gm;
|
||||
const match = input.matchAll(regex);
|
||||
|
||||
if (!match) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const numbers = Array.from(match, (m) =>
|
||||
Number(m[3] ? `${m[3]}${m[3]}` : `${m[1]}${m[2]}`)
|
||||
);
|
||||
return numbers.reduce((sum, number) => sum + number, 0);
|
||||
}
|
||||
|
||||
export function solveSecond(input: string): number {
|
||||
const lookup: Record<string, string> = {
|
||||
one: '1',
|
||||
two: '2',
|
||||
three: '3',
|
||||
four: '4',
|
||||
five: '5',
|
||||
six: '6',
|
||||
seven: '7',
|
||||
eight: '8',
|
||||
nine: '9',
|
||||
'1': '1',
|
||||
'2': '2',
|
||||
'3': '3',
|
||||
'4': '4',
|
||||
'5': '5',
|
||||
'6': '6',
|
||||
'7': '7',
|
||||
'8': '8',
|
||||
'9': '9',
|
||||
'0': '0'
|
||||
} as const;
|
||||
|
||||
const regex =
|
||||
/^.*?(one|two|three|four|five|six|seven|eight|nine|\d).*(one|two|three|four|five|six|seven|eight|nine|\d).*?$|^.*(one|two|three|four|five|six|seven|eight|nine|\d).*$/gm;
|
||||
const match = input.matchAll(regex);
|
||||
|
||||
if (!match) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const numbers = Array.from(match, (m) =>
|
||||
Number(
|
||||
m[3] ? `${lookup[m[3]]}${lookup[m[3]]}` : `${lookup[m[1]]}${lookup[m[2]]}`
|
||||
)
|
||||
);
|
||||
|
||||
return sumArray(numbers);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { join } from 'path';
|
||||
import { solveFirst, solveSecond } from './index.ts';
|
||||
|
||||
const input = await Bun.file(join(__dirname, 'input.txt')).text();
|
||||
|
||||
const firstAnswer = solveFirst(input);
|
||||
|
||||
console.log(firstAnswer);
|
||||
|
||||
const secondAnswer = solveSecond(input);
|
||||
|
||||
console.log(secondAnswer);
|
||||
Reference in New Issue
Block a user