-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cleaned up the 2023 day 1 task
- Loading branch information
Showing
7 changed files
with
93 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,33 @@ | ||
import { NEWLINE, task } from '@alexaegis/advent-of-code-lib'; | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json'; | ||
|
||
export const p1 = (input: string): number => { | ||
return input | ||
.split(NEWLINE) | ||
.map((group) => { | ||
const f = group | ||
.chars() | ||
.find((l) => /\d/.test(l)) | ||
?.toInt() | ||
.toString(); | ||
const l = group | ||
.chars() | ||
.reverse() | ||
.find((l) => /\d/.test(l)) | ||
?.toInt() | ||
.toString(); | ||
return f && l ? (f + l).toInt() : 0; | ||
.lines(false) | ||
.map((line) => { | ||
let firstDigit: string | undefined; | ||
let lastDigit: string | undefined; | ||
for (let i = 0; i < line.length; i++) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const maybeFirstDigit = line[i]!; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const maybeLastDigit = line[line.length - i - 1]!; | ||
|
||
if (firstDigit === undefined && /\d/.test(maybeFirstDigit)) { | ||
firstDigit = maybeFirstDigit; | ||
} | ||
|
||
if (lastDigit === undefined && /\d/.test(maybeLastDigit)) { | ||
lastDigit = maybeLastDigit; | ||
} | ||
|
||
if (firstDigit !== undefined && lastDigit !== undefined) { | ||
break; | ||
} | ||
} | ||
|
||
return ((firstDigit ?? '') + (lastDigit ?? '')).toInt(); | ||
}) | ||
.sum(); | ||
}; | ||
await task(p1, packageJson.aoc); // 54644 ~?ms | ||
await task(p1, packageJson.aoc); // 54644 ~0.19ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,65 @@ | ||
import { NEWLINE, task } from '@alexaegis/advent-of-code-lib'; | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json'; | ||
|
||
const numbers = { | ||
one: '1', | ||
'1': '1', | ||
two: '2', | ||
'2': '2', | ||
three: '3', | ||
'3': '3', | ||
four: '4', | ||
'4': '4', | ||
five: '5', | ||
'5': '5', | ||
six: '6', | ||
'6': '6', | ||
seven: '7', | ||
'7': '7', | ||
eight: '8', | ||
'8': '8', | ||
nine: '9', | ||
'9': '9', | ||
} as const; | ||
|
||
const numbersAsKeys = Object.keys(numbers) as (keyof typeof numbers)[]; | ||
|
||
export const p2 = (input: string): number => { | ||
return input | ||
.split(NEWLINE) | ||
.lines() | ||
.map((line) => { | ||
let bufferForFirst = ''; | ||
for (const l of line.chars()) { | ||
bufferForFirst += l; | ||
bufferForFirst = bufferForFirst | ||
.replaceAll('one', '1') | ||
.replaceAll('two', '2') | ||
.replaceAll('three', '3') | ||
.replaceAll('four', '4') | ||
.replaceAll('five', '5') | ||
.replaceAll('six', '6') | ||
.replaceAll('seven', '7') | ||
.replaceAll('eight', '8') | ||
let firstDigit: string | undefined; | ||
let lastDigit: string | undefined; | ||
for (let i = 0; i < line.length; i++) { | ||
if (firstDigit === undefined) { | ||
const maybeFirstDigit = line.slice(i, i + 5); | ||
const firstTextNumber = numbersAsKeys.find((key) => | ||
maybeFirstDigit.startsWith(key), | ||
); | ||
if (firstTextNumber) { | ||
firstDigit = numbers[firstTextNumber]; | ||
} | ||
} | ||
|
||
.replaceAll('nine', '9'); | ||
} | ||
if (lastDigit === undefined) { | ||
const maybeDigit = line.slice( | ||
Math.max(line.length - i - 5, 0), | ||
line.length - i, | ||
); | ||
|
||
let bufferForLast = ''; | ||
for (const l of line.chars().reverse()) { | ||
bufferForLast = l + bufferForLast; | ||
bufferForLast = bufferForLast | ||
.replaceAll('one', '1') | ||
.replaceAll('two', '2') | ||
.replaceAll('three', '3') | ||
.replaceAll('four', '4') | ||
.replaceAll('five', '5') | ||
.replaceAll('six', '6') | ||
.replaceAll('seven', '7') | ||
.replaceAll('eight', '8') | ||
const textNumber = numbersAsKeys.find((key) => maybeDigit.endsWith(key)); | ||
if (textNumber) { | ||
lastDigit = numbers[textNumber]; | ||
} | ||
} | ||
|
||
.replaceAll('nine', '9'); | ||
if (firstDigit !== undefined && lastDigit !== undefined) { | ||
break; | ||
} | ||
} | ||
|
||
const f = bufferForFirst | ||
.chars() | ||
.find((l) => /\d/.test(l)) | ||
?.toInt() | ||
.toString(); | ||
|
||
const l = bufferForLast | ||
.chars() | ||
.reverse() | ||
.find((l) => /\d/.test(l)) | ||
?.toInt() | ||
.toString(); | ||
|
||
return f && l ? (f + l).toInt() : 0; | ||
return ((firstDigit ?? '') + (lastDigit ?? '')).toInt(); | ||
}) | ||
.sum(); | ||
}; | ||
|
||
await task(p2, packageJson.aoc); // 53348 ~?ms | ||
await task(p2, packageJson.aoc); // 53348 ~1.4ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters