One Way or Another: A Sliding Puzzle, Haskell, and Half an Hour with Claude
A physical sliding puzzle, a one-page plan, and a Claude Code session in Haskell. About 24 minutes from the first prompt to the last commit, with working, tested code at the end, and I was busy with something else for much of it.
(I used AI assistance writing this post — why)
The puzzle
One Way or Another is a physical puzzle designed by Alexander Magyarics and sold by Greg Puzzles. The shop describes it as a tray packing puzzle “given somewhere to go”, which is a good summary.

One Way or Another, with the pieces in a jammed position.
The tray is a grid four cells high and five wide. There are five pieces: a 2×2 square, an L (which can be flipped over), a T, a short three-cell L, and a 1×1 disc. Solving it has two phases:
- Placement. Put all the pieces in the tray, rotated or flipped however you like, with the disc in a corner.
- Solving. Slide the pieces around until the disc reaches the opposite corner.
The catch is that most placements can’t be solved. Finding the right placement is a large part of the puzzle.
Before starting, I wrote all of this up by hand as PLAN.md. Apart from the picture above, that was
the only input to the session.
The session
The whole thing took five prompts. Times are local.
- 08:42, repo setup. I asked for a fresh git repo, using my personal email, with an initial
commit of
PLAN.md. Claude pointed out that the picture wasn’t committed, so I asked it to add it and amend the commit. - 08:45, analysis. I asked it to analyze the puzzle and suggest an approach, and said that if it called for a program, I wanted it in Haskell, using “stack or what the current best practice is”. Claude looked at the picture and checked which tools I had installed: ghcup, GHC 9.6.7 and cabal 3.12, but no stack. It recommended ghcup and cabal over stack as current practice, and noted that the GHC I already had would do. It also redrew the picture as an ASCII board, which became the notation for the rest of the project. Then it asked three questions: which corner counts as “opposite”, what counts as one move, and whether I wanted one solution or all of them.
- 08:54–08:56, decisions. I interrupted once to add my answers, then sent the prompt again.
Only top-left to bottom-right needs solving, since the other corners follow by symmetry. The
short L gets renamed to
v, which makes the boards easier to read. A slide of any length counts as one move. And the program should find every solution, because the count says something about how hard the puzzle is. - 08:57–09:00, project setup. Claude set up a cabal package with a library, an executable, and a tasty test suite using HUnit and QuickCheck. It committed the expanded plan and the scaffold separately.
- 09:04–09:06, the search. “Please proceed with the search.” The solver, its tests and the results were committed about two minutes later.
The notation it settled on looks like this, with the jammed position from the picture:
| Char | Piece | Cells |
|---|---|---|
S |
Square | 4 |
L |
L | 4 |
T |
T | 4 |
v |
Short L | 3 |
O |
Circle | 1 |
. |
Empty | – |
v . L L .
v v L O T
S S L T T
S S . . T
The analysis
For me, the most useful part of the session was the reasoning before any code was written.
Sliding never changes a piece’s orientation. So the placement phase is really a choice of
orientations: 8 for the L, 4 for the T and 4 for the v, giving 128 combinations. Each of those is
its own independent sliding puzzle, and no sequence of slides gets you from one to another.
The state space is tiny. Claude estimated something on the order of 10⁵ states across all the orientation choices, and was careful to label that as an unchecked estimate. Either way, that’s small enough for an exhaustive search, with no heuristics needed.
A placement is just a state. It’s a state that happens to have the disc in a corner. So the placement phase doesn’t need an algorithm of its own: a placement is solvable if a goal state can be reached from it.
Symmetry. The board looks the same flipped either way or rotated 180°, so all the corners are equivalent.
What came out of this was simple. Each piece is a bitmask in a Word32, with bit row * 5 + col
for each cell. A move shifts a mask by ±1 or ±5. Since every move can be undone, one breadth-first
search running backwards from every goal state at once gives each state its distance to the goal:
-- | The fewest moves from each state to any of the given states. States that
-- can't reach them are absent. Moves are reversible, so this is a
-- breadth-first search outwards from the targets.
distancesTo :: [State] -> Map State Int
distancesTo targets = go 0 (Set.fromList targets) (Map.fromList [(s, 0) | s <- targets])
where
go !d frontier seen
| Set.null frontier = seen
| otherwise =
let next =
Set.fromList
[ s'
| s <- Set.toList frontier
, (_, s') <- moves s
, Map.notMember s' seen
]
seen' = Map.union seen (Map.fromSet (const (d + 1)) next)
in go (d + 1) next seen'
Any placement that ends up with a distance is solvable, and walking the distances back down to zero gives a shortest solution.
The results
- There are 1767 possible placements with the disc in the top-left corner.
- Only 8 of them can be solved, and those come from just 2 of the 128 orientation choices.
- The shortest solutions take 18 moves (2 placements), 19 (3), 20 (2) and 21 (1).
- The full search runs in under half a second.
These are the two orientation choices that work, each shown in one of its solvable placements:
O . S S . O . . v v
T . S S L L L . v T
T T v . L L S S T T
T v v L L L S S . T
And here is one of the two 18-move solutions, as printed by cabal run one-way:
start O right 1 T up 1 v left 1 S down 1 O right 3 S up 1
O . S S . . O S S . T O S S . T O S S . T O . . . T . . . O T . S S O
T . S S L T . S S L T T S S L T T S S L T T S S L T T S S L T T S S L
T T v . L T T v . L T . v . L T v . . L T v S S L T v S S L T v . . L
T v v L L T v v L L . v v L L v v . L L v v . L L v v . L L v v . L L
v right 1 T down 1 S left 1 O left 1 O down 2 S right 1 T up 1
T . S S O . . S S O . S S . O . S S O . . S S . . . . S S . T . S S .
T T S S L T . S S L T S S . L T S S . L T S S . L T . S S L T T S S L
T . v . L T T v . L T T v . L T T v . L T T v O L T T v O L T . v O L
. v v L L T v v L L T v v L L T v v L L T v v L L T v v L L . v v L L
v left 1 O left 1 L up 1 O down 1 O right 2
T . S S . T . S S . T . S S L T . S S L T . S S L
T T S S L T T S S L T T S S L T T S S L T T S S L
T v . O L T v O . L T v O L L T v . L L T v . L L
v v . L L v v . L L v v . . . v v O . . v v . . O
That bears out “most placements can’t be solved”. Picking the orientations is most of the difficulty. Once you have one of the two right ones, the sliding is just a lot of patient shuffling.
What I liked
It checked its own work independently. The tests compare the solver against a separate, simpler forward search that shares none of its code. They check that every reported solution is shortest, that none of the other 1759 placements can reach the goal, and that the other three corner pairs give identical results.
It blamed a failing test correctly. One test failed, and Claude worked out that the test itself
was wrong: its example of a “wrong shape” was actually a valid orientation of v. It fixed the
test, rather than bending the code to make it pass.
It adapted when an assumption didn’t hold. Its first plan for generating random test positions was to start from the arrangement in the picture and make random moves. But that arrangement is jammed, and no piece in it can move. Claude noticed, generated random valid positions instead, and wrote down why in the plan.
It flagged a problem with my setup. My local Hackage index was 451 days old. Claude suggested
cabal update, and I ran it myself before the search step.
The history is clean. Four focused commits, with real commit messages and an Assisted-by:
trailer. PLAN.md was updated as decisions were made, so the repo documents itself.
Haskell
Most AI coding demos are in Python or JavaScript, so I was curious how this would go in Haskell. It went fine.
The tooling advice was current: ghcup and cabal, GHC2021, HLS. The code was idiomatic Haskell:
bitmasks with Data.Bits, Data.Map and Data.Set for the search, and QuickCheck properties in
the tests.
The friction was the usual Haskell kind, and the compiler errors sorted it out quickly:
- it needed the
LambdaCaseandDerivingStrategiesextensions; .&.clashed betweenData.BitsandTest.Tasty.QuickCheck;containerswas missing from the executable’s dependencies.
None of these needed any input from me, and the build ended with no warnings.
This problem also suits property-based testing well. Every move can be undone, pieces never overlap, and a move only changes the piece being moved. Claude wrote QuickCheck properties for all of these without being asked.
The whole thing is about 690 lines: Board 175, Pieces 63, Search 183, Main 72, and the tests
199.
Cost and time
These numbers are from /cost at the end of the session. I’m on a subscription plan, so I didn’t
pay this separately; it’s the API-equivalent estimate.
| Estimated cost | $1.83 |
| API time | 6m 42s |
| Wall time | 32m 41s |
| Code changes | 664 lines added, 35 removed |
| Opus 5.5 output | 37.5k tokens |
| Cache | 2.6M tokens read, 97% of input from cache, no misses |
Most of the wall time was me reading and deciding. Claude was actually working for under seven minutes of it. And I wasn’t giving this session my full attention either: the whole time, I had another Claude Code session running, analyzing Zephyr security issues, and I was going back and forth between the two.
The heavy use of the prompt cache is why a session this size costs under $2.
Wrap-up
A small, well-defined problem with a written plan is the ideal shape for this kind of session. My part was describing the problem, answering the design questions, and reviewing the commits. Claude did the analysis, the scaffolding, the code and the checking. Haskell wasn’t an obstacle.
Next, I’d like to print the solutions as proper diagrams, try the 18-move solution on the physical puzzle, and maybe have a go at the other puzzles in the same series.