Skip to content

Commit

Permalink
solved day25
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 25, 2023
1 parent 4ac2080 commit 03fc5d5
Show file tree
Hide file tree
Showing 5 changed files with 1,380 additions and 0 deletions.
93 changes: 93 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"@datastructures-js/priority-queue": "^6.3.0",
"@graph-algorithm/minimum-cut": "^2.0.0",
"@shahata/inquirer-timeout-confirm-prompt": "^0.1.1",
"chart.js": "^4.4.0",
"combinatorial-generators": "^1.1.2",
Expand Down
37 changes: 37 additions & 0 deletions src/2023/day25.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { mincut } from '@graph-algorithm/minimum-cut';

function graphSize(graph, component, ignored, visited = new Set()) {
let result = 1;
visited.add(component);
graph[component].forEach(c => {
if (!visited.has(c) && !ignored[component]?.includes(c)) {
result += graphSize(graph, c, ignored, visited);
}
});
return result;
}

function toGraph(edges) {
const graph = {};
edges.forEach(([a, b]) => {
graph[a] = (graph[a] || []).concat(b);
graph[b] = (graph[b] || []).concat(a);
});
return graph;
}

export function part1(input) {
const edges = [];
input.split('\n').forEach(line => {
let [component, connections] = line.split(': ');
connections = connections.split(' ');
connections.forEach(connection => edges.push([component, connection]));
});
const graph = toGraph(edges);
const ignored = toGraph([...mincut(edges)]);
const size = graphSize(graph, Object.keys(graph)[0], {});
const x = graphSize(graph, Object.keys(graph)[0], ignored);
return x * (size - x);
}

export const part2 = () => undefined;
40 changes: 40 additions & 0 deletions src/2023/day25.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { part1, part2 } from './day25.js';
import readInput from '../utils/read-input.js';

const input = readInput(import.meta.url);

describe('day25 2023', () => {
describe('part1', () => {
it('should work for part 1 examples', () => {
expect(
part1(
[
'jqt: rhn xhk nvd',
'rsh: frs pzl lsr',
'xhk: hfx',
'cmg: qnr nvd lhk bvb',
'rhn: xhk bvb hfx',
'bvb: xhk hfx',
'pzl: lsr hfx nvd',
'qnr: nvd',
'ntq: jqt hfx bvb xhk',
'nvd: lhk',
'lsr: lhk',
'rzs: qnr cmg lsr rsh',
'frs: qnr lhk lsr',
].join('\n'),
),
).toEqual(54);
});

it('should work for part 1 input', () => {
expect(part1(input)).toEqual(562978);
});
});

describe('part2', () => {
it('should work for part 2 input', () => {
expect(part2()).toEqual(undefined);
});
});
});
Loading

0 comments on commit 03fc5d5

Please sign in to comment.