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 🙏

© 2026 – Pkg Stats / Ryan Hefner

castles

v2.0.0

Published

A competitive logic puzzle useful for teaching

Readme

Castles

A competitive logic puzzle useful for teaching.

| Branch | Status | | ------------- |:------------- | | Master | Build Status Coverage Status NSP Status | | All | Build Status |

Overview

This project is inspired by an entry of FiveThirtyEight.com's weekly Riddler puzzle column:

In a distant, war-torn land, there are 10 castles. There are two warlords: you and your archenemy. Each castle has its own strategic value for a would-be conqueror. Specifically, the castles are worth 1, 2, 3, ..., 9, and 10 victory points. You and your enemy each have 100 soldiers to distribute, any way you like, to fight at any of the 10 castles. Whoever sends more soldiers to a given castle conquers that castle and wins its victory points. If you each send the same number of troops, you split the points. You don't know what distribution of forces your enemy has chosen until the battles begin. Whoever wins the most points wins the war.

For more info, see the original post here and the results of the Riddler Nation battle royale here.

Usage

Install the Package

npm install --save castles

BattlePlans

A BattlePlan is an allocation of armies, plus some additional metadata. Two BattlePlans can fight according to the rules above:

const castles = require('castles')

// Create a BattlePlan that sends all armies to Castle #10
const allToCastleTen = new castles.BattlePlan({
  allocations: [0, 0, 0, 0, 0, 0, 0, 0, 0, 100],
  name: 'All to Castle Ten',
  author: 'Samples'
})

// Create a BattlePlan that sends all armies to Castle #1
const allToCastleOne = new castles.BattlePlan({
  allocations: [100, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  name: 'All to Castle One',
  author: 'Samples'
})

// Have BattlePlans fight each other
t.is(allToCastleTen.fight(allToCastleOne), 'win')
t.is(allToCastleOne.fight(allToCastleTen), 'lose')
t.is(allToCastleTen.fight(allToCastleTen), 'tie')

Armies

An Army has a BattlePlan, plus a running record:

const castles = require('castles')

// Create an Army that sends all armies to Castle #10
// Note that we're using the published sample battle plans for brevity
const allToCastleTen = new castles.Army(castles.sampleBattlePlans.allToCastleTen)

// Create a BattlePlan that sends all armies to Castle #1
const allToCastleOne = new castles.Army(castles.sampleBattlePlans.allToCastleOne)

// Have the two Armies fight each other.
// Instead of returning an immediate result, they keep a running record.
allToCastleTen.fight(allToCastleOne)

// The `.record` property supplies a human-readable win-loss-tie record,
// useful for debugging and testing.
t.is(allToCastleTen.record, '1-0-0')
t.is(allToCastleOne.record, '0-1-0')

// The individual .wins, .losses, and .ties records are also accessible.
t.is(allToCastleTen.wins, 1)
t.is(allToCastleTen.losses, 0)
t.is(allToCastleTen.ties, 0)

// An army is given one point for each win and half a point for each tie
t.is(allToCastleTen.score, 1)

War

Tying it all together, we can have each Army fight each other Army exactly once with the .war(...) function:

const castles = require('castles')

// All sample battle plans are available
const allBattlePlans = castles.sampleBattlePlans.all
const allArmies = allBattlePlans.map(battlePlan => new castles.Army(battlePlan))

// .war(...) has each Army fight each other exactly once and
// returns the armies sorted from best to worst
const sortedArmies = castles.war(allArmies)
const winner = sortedArmies[0]

t.is(winner.battlePlan.name, 'All to Castle Ten')
t.is(winner.record, '2-0-0')
t.is(winner.score, 2)