A Flipbook Written in Differences
The first flipbook page is easy: draw the puzzle, mark the blank square and make sure the registration pins line up. The next page needs only one tile to move. By the tenth page, though, copying the whole grid each time feels like an elaborate way to describe a very small change.
For one tile on a three-by-three board, number the squares from left to right, top to bottom, starting at zero. A journey through squares 0, 1, 4, 3 goes right, down and left. Delta encoding records the first position, then the difference between each pair of consecutive positions. Both programs print 0 1 3 -1.
positions = [0, 1, 4, 3]
deltas = [positions[0]]
for i in range(1, len(positions)):
deltas.append(positions[i] - positions[i - 1])
print(*deltas)
The Zig version keeps the original positions and prints each difference as it goes. Its fixed-size array also makes the number of recorded positions visible in the program.
const std = @import("std");
pub fn main() void {
const positions = [_]i32{ 0, 1, 4, 3 };
std.debug.print("{d}", .{positions[0]});
for (1..positions.len) |i| {
const delta = positions[i] - positions[i - 1];
std.debug.print(" {d}", .{delta});
}
std.debug.print("\n", .{});
}
To recover the journey, start at zero and add each remaining number in turn. The signs matter: the last delta takes the tile from square four back to square three. These programs assume the position list is non-empty, and they record a journey without checking whether it is legal on a puzzle board.
Differences can be smaller than absolute values, which can make them cheaper to store with a suitable encoding. A list of ordinary integers takes the same space here, though, and losing one delta throws off every later reconstructed position. For a longer flipbook, an occasional complete page gives you somewhere reliable to resume. Paper registration serves much the same purpose: each drawing needs a trustworthy reference.