This code systematically explores the solution space for the puzzle "Das Haus vom Nikolaus". It finds various things:
- Solutions: Paths that use each (undirected) connection exactly once.
- Failures: Paths ending in a vertex where you cannot continue (because all connections of the vertex have been used already), but there are still unused connections elsewhere.
- "Early Failures": Paths where the last step was a mistake. That is, the path cannot be completed to a solution but without the last step it could have been (by continuing another way).
Usually one is only allowed to cross the middle of the house (the X in the sketch near the beginning of the code) in a straight line from b to e or from c to d or in the opposite direction. But some people consider the crossing as an actual vertex where you may start or end the path or change the direction. The program handles both variants.
Here are the numbers of paths of each kind found by the program for each variant of the puzzle:
solutions | failures | early failures | |
---|---|---|---|
without vertex X | 88 | 266 | 23 |
with vertex X | 240 | 1008 | 78 |
Notes on the code:
- Yes, my string-based data structures are quite simplistic. They work in this simple application but not for larger graphs. But I think the idea of the algorithm becomes clearer without elaborate graph data structures.
- Of course the algorithm could be optimized if we did not care about failures. Solutions are only possible if there are at most two vertices with an odd number of connections. And if there are two such vertices, solutions must begin at one of them and end with the other.
Running the code:
- Invoke the code using one of the following command lines, depending on which JS/TS runner you have available:
node --watch haus-vom-nikolaus.ts deno run --watch haus-vom-nikolaus.ts bun --watch haus-vom-nikolaus.ts
- If you're not playing with the code and only want to run it once, omit the option
--watch
. - Direct execution of (some subset of) TypeScript in node works for v23.7.0. Older versions (probably before v23) require the command-line option
--experimental-strip-types
, which was introduced in nodejs V22.6.0. With an even older version of nodejs you have to convert the TypeScript code to JavaScript, using the TypeScript compiler or babel or esbuild or... (or just remove the type annotations manually).