Fetta: Splitting Packages Fairly with a DFS Algorithm and Next.js 16

Giovanni Mugelli
Giovanni Mugelli

Fetta solves a deceptively simple problem: how do you divide a set of fixed-size packages among several people as fairly as possible? Built with Next.js 16, Tailwind CSS v4, and a custom depth-first search solver.

Cover Image for Fetta: Splitting Packages Fairly with a DFS Algorithm and Next.js 16

Every time a group of friends orders something in bulk — olive oil from a Tuscan cooperative, coffee beans, cured meats — the same conversation happens: how do we split the packages fairly? You cannot cut a 5 kg bag in half. The quantities are fixed, the proportions are always messy, and spreadsheets feel like overkill.

That friction is what led to Fetta — Italian for slice, as in a fair share of something larger.

The Problem Is Harder Than It Looks

At first glance, this is a straightforward division problem. In practice, it belongs to a family of combinatorial optimization problems closely related to the bin-packing and subset-sum problems, both of which are NP-complete. The packages are indivisible. Each recipient has a target proportion (not a fixed weight). The total allocation must use only whole packages. The goal is to minimise the deviation from the ideal proportions across all recipients simultaneously.

A naive greedy approach — assign the largest available package to whoever is furthest below their target — fails quickly when the remaining packages create an irreconcilable imbalance. What you need is a solver that can look ahead.

A DFS Solver with a Greedy Fallback

Fetta's core algorithm is a depth-first search with backtracking, modelled as a constraint satisfaction problem. For each package, the solver tries assigning it to each recipient in turn, prunes branches that exceed a recipient's proportional budget, and records the allocation that minimises total absolute error when all packages are exhausted.

For large inputs where the DFS search space becomes intractable, Fetta switches to a greedy-with-swaps fallback: assign packages greedily, then perform pairwise swaps between recipients until no swap improves the total error. The strategy used is reported transparently in the results summary.

Both algorithms run entirely in the browser — no server, no API, no data leaves the device.

The Stack

Building Fetta was an opportunity to work with the very latest tooling:

  • Next.js 16 with the App Router and full static generation (output: export)
  • TypeScript throughout, with strict mode enabled
  • Tailwind CSS v4 — the new engine is noticeably faster and the @theme configuration syntax is a pleasure to work with
  • shadcn/ui built on @base-ui/react for accessible, headless UI primitives
  • react-hook-form with Zod schemas for type-safe form validation
  • next-themes for dark / light / system preference support

The form layer deserves special mention. The input model — packages (weight × quantity pairs) and recipients (name + relative proportion) — is dynamic: rows are added and removed at runtime. Getting react-hook-form's field array API to cooperate with Zod's discriminated union validation took some patience, but the result is a form that validates correctly at every step without a single any cast.

Internationalisation from Day One

Fetta supports Italian and English out of the box, switchable at runtime without a page reload. The translation architecture mirrors the approach used on this portfolio site: a typed context that reads the user's language preference from localStorage, defaulting to English on the server to avoid hydration mismatches.

The Italian locale is the default for local use; the English locale makes the app useful to an international audience discovering it via GitHub or Vercel.

Deployment

The app is deployed on Vercel and available at fetta.vercel.app. Because the entire solver runs client-side and history is persisted in localStorage, the deployment has zero environment variables and zero server infrastructure. It is a pure static application that could be hosted on any CDN.

The source code is available on GitHub under the MIT licence.

Reflections

Fetta is a small app with a specific purpose, but it forced me to think carefully about algorithm design in a constrained environment, about progressive disclosure in form UX (showing the strategy badge only when the fallback fires), and about the ergonomics of a tool people will use repeatedly — which is why the history panel persists the last 20 calculations with one-click restore.

Sometimes the most satisfying software is the kind that solves one problem completely.