Skip to content

Latest commit

 

History

History
144 lines (123 loc) · 4.8 KB

File metadata and controls

144 lines (123 loc) · 4.8 KB

Task 1: Finding the Size of the Largest Circuit

To solve the problem of finding the size of the largest circuit formed by connecting junction boxes based on their 3D coordinates, we can follow these steps: So, we need to implement a solution that reads the junction box coordinates, calculates distances, and connects them accordingly.

  1. Parse Input: Read the list of junction box coordinates from the input file and store them in a suitable data structure (e.g., an array of objects).
  2. Calculate Distances: Create a function to calculate the Euclidean distance between two junction boxes using their coordinates.
  3. Connect Junction Boxes: Use a union-find data structure to efficiently manage and connect junction boxes based on the shortest distances.
  4. Count Circuit Sizes: After all connections are made, count the size of each circuit and determine the largest one.
  5. Output Result: Print the size of the largest circuit.
class UnionFind {
    constructor(size) {
        this.parent = Array.from({ length: size }, (_, i) => i);
        this.rank = Array(size).fill(0);
        this.size = Array(size).fill(1);
    }

    find(x) {
        if (this.parent[x] !== x) {
            this.parent[x] = this.find(this.parent[x]);
        }
        return this.parent[x];
    }

    union(x, y) {
        const rootX = this.find(x);
        const rootY = this.find(y);

        if (rootX !== rootY) {
            if (this.rank[rootX] > this.rank[rootY]) {
                this.parent[rootY] = rootX;
                this.size[rootX] += this.size[rootY];
            } else if (this.rank[rootX] < this.rank[rootY]) {
                this.parent[rootX] = rootY;
                this.size[rootY] += this.size[rootX];
            } else {
                this.parent[rootY] = rootX;
                this.size[rootX] += this.size[rootY];
                this.rank[rootX]++;
            }
        }
    }

    getSize(x) {
        return this.size[this.find(x)];
    }
}

const edges = [];
for (let i = 0; i < input.length; i++) {
    for (let j = i + 1; j < input.length; j++) {
        edges.push([distance(input[i], input[j]), i, j]);
    }
}

edges.sort((a, b) => a[0] - b[0]);

const uf = new UnionFind(input.length);
for (let i = 0; i < 1000; i++) {
    const [_, a, b] = edges[i];
    uf.union(a, b);
}

const sizes = {};
for (let i = 0; i < input.length; i++) {
    const root = uf.find(i);
    sizes[root] = (sizes[root] || 0) + 1;
}

const largestSizes = Object.values(sizes).sort((a, b) => b - a).slice(0, 3);
const result = largestSizes.reduce((acc, size) => acc * size, 1);

Task 2: Multiplying the X Coordinates of the Last Connected Junction Boxes

To find the product of the X coordinates of the last two junction boxes that need to be connected to form a single circuit, we can extend the previous solution with the following steps:

  1. Track Connections: While connecting junction boxes, keep track of the last two junction boxes that were connected.
  2. Output Result: After all junction boxes are connected, multiply the X coordinates of the last two connected junction boxes and print the result.

It's same as above with an addition to track the last connected junction boxes.

class UnionFind {
    constructor(size) {
        this.parent = Array.from({ length: size }, (_, i) => i);
        this.rank = Array(size).fill(0);
        this.size = Array(size).fill(1);
    }

    find(x) {
        if (this.parent[x] !== x) {
            this.parent[x] = this.find(this.parent[x]);
        }
        return this.parent[x];
    }

    union(x, y) {
        const rootX = this.find(x);
        const rootY = this.find(y);

        if (rootX !== rootY) {
            if (this.rank[rootX] > this.rank[rootY]) {
                this.parent[rootY] = rootX;
                this.size[rootX] += this.size[rootY];
            } else if (this.rank[rootX] < this.rank[rootY]) {
                this.parent[rootX] = rootY;
                this.size[rootY] += this.size[rootX];   
            } else {
                this.parent[rootY] = rootX;
                this.size[rootX] += this.size[rootY];
                this.rank[rootX]++;
            }
            return true;
        }
        return false;
    }

    getSize(x) {
        return this.size[this.find(x)];
    }
}

const edges = [];
for (let i = 0; i < input.length; i++) {
    for (let j = i + 1; j < input.length; j++) {
        edges.push([distance(input[i], input[j]), i, j]);
    }
}

edges.sort((a, b) => a[0] - b[0]);

const uf = new UnionFind(input.length);
let lastConnection = null;
for (let i = 0; i < edges.length; i++) {
    const [_, a, b] = edges[i];
    if (uf.union(a, b)) {
        lastConnection = [a, b];
        if (uf.getSize(a) === input.length) {
            break;
        }
    }
}

const result = input[lastConnection[0]][0] * input[lastConnection[1]][0];