-
-
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.
- Loading branch information
Showing
11 changed files
with
109 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"schemaVersion": 1, | ||
"label": "Advent of TypeScript 2023", | ||
"message": "12/25", | ||
"message": "13/25", | ||
"color": "orange" | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7 |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { hash } from './hash.js'; | ||
|
||
describe('hash', () => { | ||
it('should, return for the string hash', () => { | ||
expect(hash('HASH')).toEqual(52); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const hash = (s: string): number => { | ||
let value = 0; | ||
for (let i = 0; i < s.length; i++) { | ||
value += s.codePointAt(i) ?? 0; | ||
value *= 17; | ||
value %= 256; | ||
} | ||
return value; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { hash } from './hash.js'; | ||
|
||
interface SetInstruction { | ||
label: string; | ||
box: number; | ||
operator: '='; | ||
focalLength: number; | ||
} | ||
|
||
interface RemoveInstruction { | ||
label: string; | ||
box: number; | ||
operator: '-'; | ||
} | ||
|
||
type Instruction = SetInstruction | RemoveInstruction; | ||
|
||
const instructionRegex = /(.*)([=-])(.*)/; | ||
|
||
export const parseInstruction = (instruction: string): Instruction => { | ||
const match = instructionRegex.exec(instruction); | ||
if (!match) { | ||
throw new Error('instruction not matching regex: ' + instruction); | ||
} | ||
const [, label, operator, value] = match; | ||
if (operator === '=' && value !== undefined && label !== undefined) { | ||
return { | ||
focalLength: Number.parseInt(value, 10), | ||
label, | ||
operator, | ||
box: hash(label), | ||
}; | ||
} else if (operator === '-' && label !== undefined) { | ||
return { | ||
label, | ||
operator, | ||
box: hash(label), | ||
}; | ||
} else { | ||
throw new Error('invalid instruction: ' + instruction); | ||
} | ||
}; |
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,8 +1,7 @@ | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json'; | ||
import { hash } from './internal/hash.js'; | ||
|
||
export const p1 = (_input: string): number => { | ||
return 0; | ||
}; | ||
export const p1 = (input: string): number => input.lines(false).first().split(',').map(hash).sum(); | ||
|
||
await task(p1, packageJson.aoc); // 0 ~0ms | ||
await task(p1, packageJson.aoc); // 495972 ~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,8 +1,42 @@ | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json'; | ||
import { parseInstruction } from './internal/parse.js'; | ||
|
||
export const p2 = (_input: string): number => { | ||
return 0; | ||
interface Lens { | ||
label: string; | ||
focalLength: number; | ||
} | ||
|
||
export const p2 = (input: string): number => { | ||
const sequence = input.lines(false).first().split(',').map(parseInstruction); | ||
const boxMap = new Map<number, Lens[]>(); | ||
|
||
for (const instruction of sequence) { | ||
if (instruction.operator === '-') { | ||
const lenses = boxMap.get(instruction.box); | ||
const lenseIndex = lenses?.findIndex((lens) => lens.label === instruction.label) ?? -1; | ||
if (lenses !== undefined && lenseIndex >= 0) { | ||
lenses.splice(lenseIndex, 1); | ||
if (lenses.length === 0) { | ||
boxMap.delete(instruction.box); | ||
} | ||
} | ||
} else { | ||
const lenses = boxMap.getOrAdd(instruction.box, (_key) => []); | ||
const existingLens = lenses.find((lens) => lens.label === instruction.label); | ||
if (existingLens) { | ||
existingLens.focalLength = instruction.focalLength; | ||
} else { | ||
lenses.push({ focalLength: instruction.focalLength, label: instruction.label }); | ||
} | ||
} | ||
} | ||
|
||
return [...boxMap.entries()] | ||
.flatMap(([box, lenses]) => | ||
lenses.map((lens, lenseIndex) => (box + 1) * (lenseIndex + 1) * lens.focalLength), | ||
) | ||
.sum(); | ||
}; | ||
|
||
await task(p2, packageJson.aoc); // 0 ~0ms | ||
await task(p2, packageJson.aoc); // 245223 ~0.65ms |
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