mirror of
https://github.com/Aviortheking/advent-of-code.git
synced 2025-08-10 04:01:59 +00:00
feat: Add the four first days of 2023
Signed-off-by: Avior <github@avior.me>
This commit is contained in:
1000
2023/day-1/input.txt
Normal file
1000
2023/day-1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
10
2023/day-1/part-1.ts
Normal file
10
2023/day-1/part-1.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
// note: this runs using bun!
|
||||
const file = Bun.file('./input.txt')
|
||||
const text = await file.text()
|
||||
const res = text.split('\n').filter((it) => !!it).map((it) => {
|
||||
const nums = it.replace(/[a-z]/g, '')
|
||||
const first = nums[0]
|
||||
const last = nums[nums.length - 1]
|
||||
return parseInt(first + last)
|
||||
}).reduce((p, c) => p + c, 0)
|
||||
console.log(res)
|
43
2023/day-1/part-2.ts
Normal file
43
2023/day-1/part-2.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
// note: this runs using bun!
|
||||
const file = Bun.file('./input.txt')
|
||||
const text = await file.text()
|
||||
|
||||
// hack by https://twitter.com/championswimmer/status/1730667285415948767
|
||||
const letters = {
|
||||
'one': 'o1ne',
|
||||
'two': 't2wo',
|
||||
'three': 'th3ree',
|
||||
'four': 'fo4ur',
|
||||
'five': 'fi5ve',
|
||||
'six': 's6ix',
|
||||
'seven': 'se7ven',
|
||||
'eight': 'ei8ght',
|
||||
'nine': 'ni9ne'
|
||||
} as const
|
||||
|
||||
const res = text.split('\n').filter((it) => !!it).map((it) => {
|
||||
let nextNumber = -1
|
||||
let nextLetter: keyof typeof letters | null = null
|
||||
do {
|
||||
nextNumber = -1
|
||||
for (const text in letters) {
|
||||
const idx = it.indexOf(text)
|
||||
if (nextNumber === -1 || (idx > -1 && idx < nextNumber)) {
|
||||
nextNumber = idx
|
||||
nextLetter = text as 'one'
|
||||
}
|
||||
}
|
||||
if (nextNumber > -1 && nextLetter) {
|
||||
it = it.replace(nextLetter, letters[nextLetter])
|
||||
}
|
||||
} while (nextNumber > -1)
|
||||
|
||||
const nums = it.replace(/[a-z]/g, '')
|
||||
const first = nums[0]
|
||||
const last = nums[nums.length - 1]
|
||||
return parseInt(first + last)
|
||||
}).reduce((p, c) => p + c, 0)
|
||||
console.log(res)
|
||||
|
||||
// too low:
|
||||
// too high: 53896
|
Reference in New Issue
Block a user