-
-
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
9 changed files
with
96 additions
and
13 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 2024", | ||
"message": "2/25", | ||
"message": "3/25", | ||
"color": "orange" | ||
} |
Submodule resources
updated
from f55aff to b630d0
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
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,12 @@ | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json' assert { type: 'json' }; | ||
|
||
export const p1 = (input: string): number => {}; | ||
const mulRegex = /mul\((\d+),(\d+)\)/g; | ||
|
||
await task(p1, packageJson.aoc); // 0 ~0ms | ||
export const p1 = (input: string): number => { | ||
const mulMatches = [...input.matchAll(mulRegex)]; | ||
|
||
return mulMatches.map((m) => parseInt(m[1] ?? '1', 10) * parseInt(m[2] ?? '1', 10)).sum(); | ||
}; | ||
|
||
await task(p1, packageJson.aoc); // 179834255 ~0.09ms |
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,71 @@ | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json' assert { type: 'json' }; | ||
|
||
const instructionRegex = /(mul|don't|do)\((\d+)?,?(\d+)?\)/g; | ||
|
||
interface DoInstruction { | ||
operator: Operator.DO; | ||
} | ||
|
||
interface DontInstruction { | ||
operator: Operator.DONT; | ||
} | ||
|
||
interface MulInstruction { | ||
operator: Operator.MUL; | ||
operandA: number; | ||
operandB: number; | ||
} | ||
|
||
type Instruction = DoInstruction | DontInstruction | MulInstruction; | ||
|
||
enum Operator { | ||
MUL = 'mul', | ||
DO = 'do', | ||
DONT = "don't", | ||
} | ||
|
||
const parseInstruction = (match: RegExpExecArray): Instruction => { | ||
if (match[1] === Operator.MUL) { | ||
return { | ||
operator: match[1], | ||
operandA: parseInt(match[2] ?? '1', 10), | ||
operandB: parseInt(match[3] ?? '1', 10), | ||
} as MulInstruction; | ||
} else if (match[1] === Operator.DO) { | ||
return { | ||
operator: match[1], | ||
} as DoInstruction; | ||
} else if (match[1] === Operator.DONT) { | ||
return { | ||
operator: match[1], | ||
} as DontInstruction; | ||
} else { | ||
throw new Error('unsupported instruction: ' + match[0]); | ||
} | ||
}; | ||
|
||
export const p2 = (input: string): number => { | ||
return 0; | ||
const instructions = [...input.matchAll(instructionRegex)].map(parseInstruction); | ||
|
||
let enabled = true; | ||
let sum = 0; | ||
for (const i of instructions) { | ||
switch (i.operator) { | ||
case Operator.DO: | ||
enabled = true; | ||
break; | ||
case Operator.DONT: | ||
enabled = false; | ||
break; | ||
case Operator.MUL: | ||
if (enabled) { | ||
sum += i.operandA * i.operandB; | ||
} | ||
break; | ||
} | ||
} | ||
return sum; | ||
}; | ||
|
||
await task(p2, packageJson.aoc); // 364 ~0.90ms | ||
await task(p2, packageJson.aoc); // 80570939 ~0.11ms |
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