-
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
158 additions
and
101 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
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,40 @@ | ||
export async function taskOne(input: string[]): Promise<void> { | ||
const points = input.map(i=>i.split(',').map(Number)) | ||
const graph: number[][] = Array.from({length:input.length}, () => []) | ||
|
||
for (let i = 0; i < points.length; i++) { | ||
for (let j = i+1; j < points.length; j++) { | ||
let dist = [ | ||
points[i][0]-points[j][0], | ||
points[i][1]-points[j][1], | ||
points[i][2]-points[j][2], | ||
points[i][3]-points[j][3], | ||
].map(Math.abs).reduce((a,b)=>a+b,0) | ||
if (dist <= 3) { | ||
graph[i].push(j) | ||
graph[j].push(i) | ||
} | ||
} | ||
} | ||
|
||
const visited = new Set<number> | ||
let groups = 0 | ||
for (let i = 0; i < graph.length; i++) { | ||
if (visited.has(i)) continue | ||
groups++ | ||
const stack = [i] | ||
while(stack.length > 0) { | ||
const q = stack.pop()! | ||
if (visited.has(q)) continue | ||
visited.add(q) | ||
for (const n of graph[q]) { | ||
stack.push(n) | ||
} | ||
} | ||
} | ||
console.log(groups) | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
console.log("Unimplemented"); | ||
} |
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,4 +1,4 @@ | ||
# 2018 | ||
![](https://img.shields.io/badge/stars%20⭐-46-yellow) | ||
![](https://img.shields.io/badge/stars%20⭐-50-yellow) | ||
|
||
Puzzles not solved during event. |
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