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

unblock-me-solver

v1.0.5

Published

A package that solves the given unblock-me puzzle case

Downloads

5

Readme

Unblock me solver

This is a solver for the classic game Unblock me

Interface

The package exposes a solve function which takes in a board state and returns the optimal solution as an array of Move objects.

Object structure:

Block: { id: number, x: number, y: number, width: number, height: number }

Move: { blockId: number, amount: number, dir: 'x' | 'y' }

Solver interface

Function: solve

| Param | Type | Description | | -------------- | -------------------------- | -------------------------------------------- | | initialBlocks | Block[] | The configuration of the blocks on the board | | maxHeight | number default 6 | The height of the board | | maxWidth | number default 6 | The width of the board | | maxWidth | boolean default false | A flag to determine whether blocks can only move in the direction of their larger size or in both directions | | goalY | number default 2 | The row where the goal is | | Returns | Move[] | A list the moves for the optimal solution |

Implementation

The solver is implemented using a Breadth-first-search (BFS) algorithm. For each board state, all legal moves are calculated and added to the search queue. The queue is expanded in a FIFO-manner, hence BFS. Duplicate states are detected by comparing the stringified representations of each board. The search stops when the goal state is reached. The list of moves needed for each state is stored alongside the state in the queue. This allows us to trace back the optimal solution once the goal state is reached.

Usage

For instance, given the following board state:

..22.3
.....3
1145..
..45..
6677..
....88

The following code will return the optimal solution:

import { solve } from 'unblock-me-solver';

const blocks = [
    { id: 1, x: 0, y: 2, width: 2, height: 1 },
    { id: 2, x: 2, y: 0, width: 2, height: 1 },
    { id: 3, x: 5, y: 0, width: 1, height: 2 },
    { id: 4, x: 2, y: 2, width: 1, height: 2 },
    { id: 5, x: 3, y: 2, width: 1, height: 2 },
    { id: 6, x: 0, y: 4, width: 2, height: 1 },
    { id: 7, x: 2, y: 4, width: 2, height: 1 },
    { id: 8, x: 4, y: 5, width: 2, height: 1 },
];

const solution = solve(blocks);
console.log(solution);
Output:
[
  { id: 2, amount: -2, dir: 'x' },
  { id: 4, amount: -2, dir: 'y' },
  { id: 5, amount: -2, dir: 'y' },
  { id: 1, amount: 4, dir: 'x' }
]

Notes

In the original game, the board is always 6 x 6. However, the solver can handle boards of any size. The default board size is 6 x 6, but this can be overridden by passing the maxHeight and maxWidth parameters to the solve function. Additionally, blocks can only move in the direction of their larger size by default. This can be overridden by passing the bidirectional parameter to the solve function. The goal row is 2 by default, but this can be overridden by passing the goalY parameter to the solve function.