Skip to content

Commit

Permalink
2024D23: part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
martapanc committed Dec 23, 2024
1 parent d232933 commit 0f3914d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
4 changes: 2 additions & 2 deletions 2024/src/2024/day23/day23.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('2024 Day 23', () => {
});

test('Part 2', async () => {
expect(await part2('testInput1')).toEqual(31);
expect(await part2('input')).toEqual(29379307);
expect(await part2('testInput1')).toEqual("co,de,ka,ta");
expect(await part2('input')).toEqual("bg,bl,ch,fn,fv,gd,jn,kk,lk,pv,rr,tb,vw");
});
});
63 changes: 59 additions & 4 deletions 2024/src/2024/day23/day23.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,71 @@ export async function part1(inputFile: string) {
}

export async function part2(inputFile: string) {
return await day23(inputFile);
return await day23(inputFile, findLargestParty);
}

async function day23(inputFile: string, calcFn?: (lines: string[]) => number) {
async function day23(inputFile: string, calcFn?: (lines: string[]) => number | string) {
const inputPath = path.join(__dirname, inputFile);
const lines = await readInputLineByLine(inputPath);

return calcFn?.(lines);
}

function findComputerTriplets(lines: string[]) {
const adjacencyList: { [key: string]: Set<string>} = {};
const adjacencyList = getAdjacencyList(lines);

return getTrianglesStartingWithT(adjacencyList);
}

// Bron–Kerbosch Algorithm:
// It operates on three sets:
// - R (Current Clique): Nodes included in the current clique.
// - P (Candidates): Nodes that can be added to the current clique.
// - X (Excluded): Nodes already considered and excluded from the current clique.
// At each step:
// - If P and X are empty, R is a maximal clique.
// - A pivot is selected to reduce the search space and optimize performance.
// Recursive calls refine these sets to explore possible cliques.

function findLargestParty(lines: string[]) {
const adjacencyList = getAdjacencyList(lines);

const findLargestParties = (adjacencyList: { [key: string]: Set<string> }) => {
const parties: string[][] = [];

const bronKerbosch = (r: Set<string>, p: Set<string>, x: Set<string>) => {
if (p.size === 0 && x.size === 0) {
parties.push([...r]);
return;
}

const pivot = p.size > 0 ? [...p][0] : null;
const pivotNeighbors = pivot ? adjacencyList[pivot] : new Set();

for (const node of [...p].filter((n) => !pivotNeighbors.has(n))) {
bronKerbosch(
new Set([...r, node]),
new Set([...p].filter((n) => adjacencyList[node].has(n))),
new Set([...x].filter((n) => adjacencyList[node].has(n))),
);
p.delete(node);
x.add(node);
}
}

bronKerbosch(new Set(), new Set(Object.keys(adjacencyList)), new Set());
return parties;
}

const findLargestParty = (parties: string[][]) => {
return parties.reduce((max, party) => (party.length > max.length ? party : max), []);
}

return findLargestParty(findLargestParties(adjacencyList)).sort().join(",");
}

function getAdjacencyList(lines: string[]) {
const adjacencyList: { [key: string]: Set<string> } = {};
for (const connection of lines) {
const [a, b] = connection.split("-");
if (!adjacencyList[a])
Expand All @@ -28,7 +81,10 @@ function findComputerTriplets(lines: string[]) {
adjacencyList[a].add(b);
adjacencyList[b].add(a);
}
return adjacencyList;
}

function getTrianglesStartingWithT(adjacencyList: { [p: string]: Set<string> }) {
const triangles: Set<string> = new Set();
let trianglesStartingWithT = 0;
for (const [node, neighbors] of Object.entries(adjacencyList)) {
Expand All @@ -46,7 +102,6 @@ function findComputerTriplets(lines: string[]) {
}
}
}

[...triangles].forEach((triangle: string) => {
const split = triangle.split(",")
if (split.some(s => s.startsWith("t"))) {
Expand Down

0 comments on commit 0f3914d

Please sign in to comment.