npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

traintracks

v1.0.0

Published

The Saturday Times Train Tracks Puzzle solver

Downloads

4

Readme

traintracks

A solver for the Train Tracks puzzle written in JavaScript

The Puzzle

In the Saturday Times (in the UK), there is a puzzle called Train Tracks.

It's an 8x8 grid of squares. Each row and column has a number which tells you how many pieces go in that row or column.

There is also a marker for A and B the start and end of the track. There are possibly one or more other pieces already on the grid.

Your job is to draw the track using only staight or 90 degree corner piece from A to B so that the correct number of pieces is present in each row and column of the grid.

Here's an example puzzle:

Train Tracks Puzzle

Encoding Puzzles

We encode the puzzle as follows: <A><B>-<cols>-<rows>-<pieces>.

The puzzle above is 54-14134544-54234341-48EW.53NE

A and B are the number 1-8 indicating how far along the grid (from the bottom-left) the start (how far up) and finish (how far right). In this example A is 5 and B is 4

cols is a list of 8 numbers 1-8 indicating the constraint on the column. numbers read left-to-right (along the top of the puzzle as pictured).

rows is the same for the rows, this time top to bottom, making it easier to follow from the real puzzle.

pieces are the list of initial pieces (not counting the start and finish) separated by a dot . and are <rol><col><type>where type is the way the piece points. The valid types are NS (vertical) EW (horizontal) NE L-shaped, NW backwards-L, SE upside-L, SW 180degree rotated L.

This is a lot easier to type in than it sounds. But if this was used on a website, then a more intuitive UI could create the puzzle and generate the encoding.

The Solver

Anyway once we have the encoding we can call the traintracks tool:

$ traintracks 54-14134544-54234341-48EW.53NE
Puzzle: 54-14343245-14134544-48EW.53NE
    1 4 1 3 4 5 4 4            1 4 1 3 4 5 4 4
   ┌─┬─┬─┬─┬─┬─┬─┬─┐          ┌─┬─┬─┬─┬─┬─┬─┬─┐
 8 │ │ │ │━│ │ │ │ │ 5      8 │ │┏│━│━│━│┓│ │ │ 5
   ├─┼─┼─┼─┼─┼─┼─┼─┤          ├─┼─┼─┼─┼─┼─┼─┼─┤
 7 │ │ │ │ │ │ │ │ │ 4      7 │ │┃│ │ │ │┗│━│┓│ 4
   ├─┼─┼─┼─┼─┼─┼─┼─┤          ├─┼─┼─┼─┼─┼─┼─┼─┤
 6 │ │ │ │ │ │ │ │ │ 2      6 │ │┃│ │ │ │ │ │┃│ 2
   ├─┼─┼─┼─┼─┼─┼─┼─┤          ├─┼─┼─┼─┼─┼─┼─┼─┤
 A │━│ │ │ │ │ │ │ │ 3      A │━│┛│ │ │ │ │ │┃│ 3
   ├─┼─┼─┼─┼─┼─┼─┼─┤          ├─┼─┼─┼─┼─┼─┼─┼─┤
 4 │ │ │ │ │ │ │ │ │ 4      4 │ │ │ │ │┏│━│━│┛│ 4
   ├─┼─┼─┼─┼─┼─┼─┼─┤          ├─┼─┼─┼─┼─┼─┼─┼─┤
 3 │ │ │ │ │┗│ │ │ │ 3      3 │ │ │ │ │┗│━│┓│ │ 3
   ├─┼─┼─┼─┼─┼─┼─┼─┤          ├─┼─┼─┼─┼─┼─┼─┼─┤
 2 │ │ │ │ │ │ │ │ │ 4      2 │ │ │ │┏│━│━│┛│ │ 4
   ├─┼─┼─┼─┼─┼─┼─┼─┤          ├─┼─┼─┼─┼─┼─┼─┼─┤
 1 │ │ │ │┃│ │ │ │ │ 1      1 │ │ │ │┃│ │ │ │ │ 1
   └─┴─┴─┴─┴─┴─┴─┴─┘          └─┴─┴─┴─┴─┴─┴─┴─┘
    1 2 3 B 5 6 7 8            1 2 3 B 5 6 7 8
Solved: true - Steps: 323

And you can watch the algoritm work by adding --animate. See the asciinema video:

asciicast

Motivation

I saw this AR Sudoku Solver. There has been prior art in the field, but this was the first I saw. Anyway, I like this little puzzle and wondered if a) I could do the same, and b) make it fully web-based.

So the roadmap is:

  • [x] Create solver
  • [ ] Make it AR
  • [ ] Profit ???