Skip to content

Commit

Permalink
Add part 2 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
lundgren2 committed Dec 6, 2023
1 parent ee88c56 commit 97dd348
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/2023/day01/part2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const digitMap = {
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
} as const;

const regex = /(?=(\d|one|two|three|four|five|six|seven|eight|nine))/g;

export function part2(input: string) {
return input.split('\n').reduce((acc, line) => {
const matches = [...line.matchAll(regex)].map(d => d[1]);
const value = [matches[0], matches.at(-1)]
.map(d => digitMap[d as keyof typeof digitMap] || d)
.join('');

return acc + Number(value);
}, 0);
}
20 changes: 19 additions & 1 deletion src/2023/day01/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {part1} from './part1';
import input from './input.txt';
import {part1} from './part1';
import {part2} from './part2';

describe('Advent of Code 2023 - Day 1', () => {
describe('part 1', () => {
Expand All @@ -15,4 +16,21 @@ treb7uchet`;
expect(part1(input)).toBe(54331);
});
});

describe('part 2', () => {
test('should output 281', () => {
const testInput = `two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen`;
expect(part2(testInput)).toBe(281);
});

test('should output correct from input', () => {
expect(part2(input)).toBe(54518);
});
});
});

0 comments on commit 97dd348

Please sign in to comment.