Solve 2023-25 (terribly)
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="2023-25" type="BunRunConfiguration">
|
||||||
|
<option name="program" value="2023/25/run.ts" />
|
||||||
|
<option name="workingDirectory" value="$PROJECT_DIR$" />
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import { describe, expect, it } from 'bun:test';
|
||||||
|
import { solveFirst, solveSecond } from './index.ts';
|
||||||
|
|
||||||
|
describe('2023-25', () => {
|
||||||
|
it('first', () => {
|
||||||
|
const testInput = `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`;
|
||||||
|
expect(solveFirst(testInput)).toBe(54);
|
||||||
|
});
|
||||||
|
it('second', () => {
|
||||||
|
// TODO: add second input
|
||||||
|
const testInput = ``;
|
||||||
|
// TODO: add second solution
|
||||||
|
expect(solveSecond(testInput)).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
import { multBy } from '../../utils/array.ts';
|
||||||
|
import { randomInt } from '../../utils/random.ts';
|
||||||
|
|
||||||
|
function trySolve(
|
||||||
|
nodes: { node: string; weight: number }[],
|
||||||
|
edges: [string, string][]
|
||||||
|
) {
|
||||||
|
while (nodes.length > 2) {
|
||||||
|
const randEdgeIdx = randomInt(0, edges.length);
|
||||||
|
const [u, v] = edges.splice(randEdgeIdx, 1)[0];
|
||||||
|
|
||||||
|
const vertexU = nodes.find((n) => n.node === u)!;
|
||||||
|
const vertexVIdx = nodes.findIndex((n) => n.node === v);
|
||||||
|
const vertexV = nodes.splice(vertexVIdx, 1)[0];
|
||||||
|
|
||||||
|
const oldNodes = [u, v];
|
||||||
|
const mergedName = `(${u}:${v})`;
|
||||||
|
|
||||||
|
const newEdges = edges
|
||||||
|
.map(
|
||||||
|
(e) =>
|
||||||
|
[
|
||||||
|
oldNodes.includes(e[0]) ? mergedName : e[0],
|
||||||
|
oldNodes.includes(e[1]) ? mergedName : e[1]
|
||||||
|
] as [string, string]
|
||||||
|
)
|
||||||
|
.filter((e) => e[0] !== e[1]);
|
||||||
|
edges.length = 0;
|
||||||
|
edges.push(...newEdges);
|
||||||
|
|
||||||
|
vertexU.node = mergedName;
|
||||||
|
vertexU.weight += vertexV.weight;
|
||||||
|
}
|
||||||
|
const edgeLength = edges.length;
|
||||||
|
|
||||||
|
const mult = multBy(nodes, (n) => n.weight);
|
||||||
|
|
||||||
|
return { edgeLength, mult };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function solveFirst(input: string): number {
|
||||||
|
const regex = /^(\w*): (.*)$/gm;
|
||||||
|
const match = input.matchAll(regex);
|
||||||
|
|
||||||
|
const { edges, nodesSet } = Array.from(
|
||||||
|
match,
|
||||||
|
(m) => [m[1], m[2].split(' ')] as [string, string[]]
|
||||||
|
).reduce(
|
||||||
|
({ edges, nodesSet }, line) => {
|
||||||
|
nodesSet.add(line[0]);
|
||||||
|
line[1].forEach((n) => {
|
||||||
|
nodesSet.add(n);
|
||||||
|
edges.push([line[0], n]);
|
||||||
|
});
|
||||||
|
|
||||||
|
return { edges, nodesSet };
|
||||||
|
},
|
||||||
|
{
|
||||||
|
edges: [] as [string, string][],
|
||||||
|
nodesSet: new Set<string>()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
let edgeLength, mult;
|
||||||
|
let i = 0;
|
||||||
|
do {
|
||||||
|
i++;
|
||||||
|
const result = trySolve(
|
||||||
|
Array.from(nodesSet, (n) => ({ node: n, weight: 1 })),
|
||||||
|
[...edges]
|
||||||
|
);
|
||||||
|
edgeLength = result.edgeLength;
|
||||||
|
mult = result.mult;
|
||||||
|
} while (edgeLength > 3);
|
||||||
|
|
||||||
|
console.log(`That took ${i} tries`);
|
||||||
|
console.log(mult);
|
||||||
|
return mult;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function solveSecond(input: string): number {
|
||||||
|
// TODO: add code
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { join } from 'path';
|
||||||
|
import { solveFirst, solveSecond } from './index.ts';
|
||||||
|
|
||||||
|
const input = await Bun.file(join(__dirname, 'input.txt')).text();
|
||||||
|
|
||||||
|
const firstAnswer = solveFirst(input);
|
||||||
|
|
||||||
|
console.log(firstAnswer);
|
||||||
|
|
||||||
|
const secondAnswer = solveSecond(input);
|
||||||
|
|
||||||
|
console.log(secondAnswer);
|
||||||
Reference in New Issue
Block a user