-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
92 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
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,54 @@ | ||
export async function taskOne(input: string[]): Promise<void> { | ||
const allowed = input[0].split(', ') | ||
const allowedMemo = new Set<string>() | ||
const disallowedMemo = new Set<string>() | ||
let s = 0 | ||
for (let i = 2; i < input.length; i++) { | ||
if (check(input[i])) s++ | ||
} | ||
console.log(s) | ||
|
||
|
||
function check(towel: string) { | ||
if (towel == '') return true | ||
if (allowedMemo.has(towel)) return true | ||
if (disallowedMemo.has(towel)) return false | ||
|
||
for (const t of allowed) { | ||
if (towel.startsWith(t)) { | ||
if (check(towel.substring(t.length))) { | ||
allowedMemo.add(towel) | ||
return true | ||
} | ||
} | ||
} | ||
disallowedMemo.add(towel) | ||
return false | ||
} | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
const allowed = input[0].split(', ') | ||
const memo = new Map<string, number>() | ||
let s = 0 | ||
for (let i = 2; i < input.length; i++) { | ||
s += check(input[i]) | ||
} | ||
console.log(s) | ||
|
||
|
||
function check(towel: string): number { | ||
if (towel == '') return 1 | ||
if (memo.has(towel)) return memo.get(towel)! | ||
|
||
let s = 0 | ||
for (const t of allowed) { | ||
if (towel.startsWith(t)) { | ||
let c = check(towel.substring(t.length)) | ||
s += c | ||
} | ||
} | ||
memo.set(towel, s) | ||
return s | ||
} | ||
} |
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