-
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
4 changed files
with
67 additions
and
3 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,33 @@ | ||
export async function taskOne(input: string[]): Promise<void> { | ||
let sum = 0 | ||
for (const i of input) { | ||
const p = i.split(': ') | ||
const nums = p[1].split(' ').map(Number) | ||
const goal = Number(p[0]) | ||
if (t(goal, nums, 1, nums[0])) sum += goal | ||
} | ||
console.log(sum) | ||
|
||
function t(goal: number, nums: number[], i: number, cur: number): boolean { | ||
if (i >= nums.length) return cur == goal | ||
if (cur > goal) return false | ||
return t(goal, nums, i+1, cur + nums[i]) || t(goal, nums, i+1, cur * nums[i]) | ||
} | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
let sum = 0 | ||
for (const i of input) { | ||
const p = i.split(': ') | ||
const nums = p[1].split(' ').map(Number) | ||
const goal = Number(p[0]) | ||
if (t(goal, nums, 1, nums[0])) sum += goal | ||
} | ||
console.log(sum) | ||
|
||
function t(goal: number, nums: number[], i: number, cur: number): boolean { | ||
if (i >= nums.length) return cur == goal | ||
if (cur > goal) return false | ||
return t(goal, nums, i+1, cur + nums[i]) || t(goal, nums, i+1, cur * nums[i]) || t(goal, nums, i+1, Number(cur.toString() + nums[i].toString())) | ||
} | ||
} |
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