From 26e0a7bd3a8a77cf9596a009ee9af0ab10cedf4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:27:39 +0300 Subject: [PATCH 1/4] Replace row sweeps with natural roaming route --- src/animation.ts | 232 ++++++++++++++++++++++++++++------------------- 1 file changed, 141 insertions(+), 91 deletions(-) diff --git a/src/animation.ts b/src/animation.ts index ef0f07f..aa2333c 100644 --- a/src/animation.ts +++ b/src/animation.ts @@ -45,32 +45,18 @@ const DEFAULT_FRAME_DELAY_MS = 70; const DEFAULT_ZIGZAG_SEED = 0xC0FFEE; const CONTRIBUTION_ROWS = 7; -const MIN_ZIGZAG_RUN = 8; -const MAX_ZIGZAG_RUN = 14; - -const SCORE_POPUP_DURATION_FRAMES = 5; -const SCORE_POPUP_RISE_PER_FRAME = 1; -const SCORE_POPUP_MAX_RISE = 4; -const SCORE_GLYPH_WIDTH = 3; -const SCORE_GLYPH_HEIGHT = 5; -const SCORE_GLYPHS: Record = { - "+": ["...", ".#.", "###", ".#.", "..."], - "0": ["###", "#.#", "#.#", "#.#", "###"], - "1": [".#.", "##.", ".#.", ".#.", "###"], - "2": ["###", "..#", "###", "#..", "###"], - "3": ["###", "..#", "###", "..#", "###"], - "4": ["#.#", "#.#", "###", "..#", "..#"], - "5": ["###", "#..", "###", "..#", "###"], - "6": ["###", "#..", "###", "#.#", "###"], - "7": ["###", "..#", "..#", ".#.", ".#."], - "8": ["###", "#.#", "###", "#.#", "###"], - "9": ["###", "#.#", "###", "..#", "###"], -}; +const HORIZONTAL_RUN_LENGTHS = [6, 7, 7] as const; +const SHORT_RUN_MIN = 2; +const SHORT_RUN_MAX = 4; +const VERTICAL_SHIFT_PATTERN = [2, 1, 1, 2, 2] as const; /** - * Build a deterministic, connected path through every contribution cell. - * Long, varied column bands reduce the rapid back-and-forth movement while - * retaining a continuous serpentine route through the full grid. + * Build a deterministic roaming route through the contribution garden. + * + * Instead of sweeping every row in order, Commit-Man travels in horizontal + * runs, sometimes continues in the same direction after shifting one or two + * rows, and reverses only at selected points or at a grid edge. The coverage + * pass may revisit gray cells, but it guarantees that every cell is visited. */ export function buildZigzagPath( columns: number, @@ -84,85 +70,140 @@ export function buildZigzagPath( } const random = createSeededRandom(seed); - const bandWidths = createZigzagBandWidths(columns, random); - const bands: Array<{ startColumn: number; width: number }> = []; - let nextColumn = 0; + const totalCells = columns * CONTRIBUTION_ROWS; + const path: GridPosition[] = [{ column: 0, row: 0 }]; + const visited = new Set([positionKey(path[0]!)]); + let current = path[0]!; + let horizontalDirection = 1; + let segmentsUntilTurn = 3; + + const append = (position: GridPosition): void => { + path.push(position); + current = position; + visited.add(positionKey(position)); + }; - for (const width of bandWidths) { - bands.push({ startColumn: nextColumn, width }); - nextColumn += width; - } + // Roam in long horizontal bursts before doing a tidy coverage pass. + // This creates the less repetitive movement pattern requested for the GIF. + for ( + let segmentIndex = 0; + segmentIndex < columns && visited.size < totalCells; + segmentIndex += 1 + ) { + const runLength = segmentsUntilTurn === 1 + ? SHORT_RUN_MIN + Math.floor(random() * (SHORT_RUN_MAX - SHORT_RUN_MIN + 1)) + : HORIZONTAL_RUN_LENGTHS[ + Math.floor(random() * HORIZONTAL_RUN_LENGTHS.length) + ]!; + + let hitGridEdge = false; + for (let step = 0; step < runLength; step += 1) { + const nextColumn = current.column + horizontalDirection; + if (nextColumn < 0 || nextColumn >= columns) { + hitGridEdge = true; + break; + } - const path: GridPosition[] = []; - let startsAtTop = true; + append({ column: nextColumn, row: current.row }); + if (visited.size === totalCells) break; + } - for (const band of bands) { - const endColumn = band.startColumn + band.width - 1; + if (visited.size === totalCells) break; - for (let rowOffset = 0; rowOffset < CONTRIBUTION_ROWS; rowOffset += 1) { - const row = startsAtTop ? rowOffset : CONTRIBUTION_ROWS - 1 - rowOffset; + const atGridEdge = current.column === 0 || current.column === columns - 1; + if (hitGridEdge || atGridEdge || segmentsUntilTurn <= 1) { + horizontalDirection *= -1; + segmentsUntilTurn = random() < 0.7 ? 3 : 2; + } else { + segmentsUntilTurn -= 1; + } - if (rowOffset % 2 === 0) { - for (let column = band.startColumn; column <= endColumn; column += 1) { - path.push({ column, row }); - } - } else { - for (let column = endColumn; column >= band.startColumn; column -= 1) { - path.push({ column, row }); - } - } + const preferredShift = VERTICAL_SHIFT_PATTERN[ + segmentIndex % VERTICAL_SHIFT_PATTERN.length + ]!; + let verticalDirection = current.row === 0 + ? 1 + : current.row === CONTRIBUTION_ROWS - 1 + ? -1 + : random() < 0.72 + ? 1 + : -1; + + let availableRows = verticalDirection > 0 + ? CONTRIBUTION_ROWS - 1 - current.row + : current.row; + if (availableRows === 0) { + verticalDirection *= -1; + availableRows = verticalDirection > 0 + ? CONTRIBUTION_ROWS - 1 - current.row + : current.row; } - startsAtTop = !startsAtTop; + const actualShift = Math.min(preferredShift, availableRows); + for (let step = 0; step < actualShift; step += 1) { + append({ column: current.column, row: current.row + verticalDirection }); + if (visited.size === totalCells) break; + } } - return path; -} - -/** - * Choose varied band widths while avoiding a very short final band on normal - * contribution grids. Small grids can be narrower than the preferred range. - */ -function createZigzagBandWidths( - columns: number, - random: () => number, -): number[] { - const minimumBandCount = Math.ceil(columns / MAX_ZIGZAG_RUN); - const maximumBandCount = Math.max(1, Math.floor(columns / MIN_ZIGZAG_RUN)); - const preferredBandCount = Math.max(1, Math.round(columns / 11)); - const bandCount = minimumBandCount <= maximumBandCount - ? Math.max(minimumBandCount, Math.min(maximumBandCount, preferredBandCount)) - : minimumBandCount; - - const widths: number[] = []; - let remaining = columns; - - for (let index = 0; index < bandCount; index += 1) { - const bandsLeft = bandCount - index - 1; - if (bandsLeft === 0) { - widths.push(remaining); - break; + // Cover cells the roaming section has not reached yet. Connect to the + // nearest unvisited cell using adjacent steps, allowing harmless revisits. + while (visited.size < totalCells) { + let target: GridPosition | undefined; + let targetDistance = Number.POSITIVE_INFINITY; + let targetTieBreaker = Number.NEGATIVE_INFINITY; + + for (let row = 0; row < CONTRIBUTION_ROWS; row += 1) { + for (let column = 0; column < columns; column += 1) { + const candidate = { column, row }; + if (visited.has(positionKey(candidate))) continue; + + const distance = + Math.abs(candidate.column - current.column) + + Math.abs(candidate.row - current.row); + const tieBreaker = random(); + + if ( + distance < targetDistance || + (distance === targetDistance && tieBreaker > targetTieBreaker) + ) { + target = candidate; + targetDistance = distance; + targetTieBreaker = tieBreaker; + } + } } - const minimumWidth = Math.max( - MIN_ZIGZAG_RUN, - remaining - bandsLeft * MAX_ZIGZAG_RUN, - ); - const maximumWidth = Math.min( - MAX_ZIGZAG_RUN, - remaining - bandsLeft * MIN_ZIGZAG_RUN, - ); - - const width = minimumWidth <= maximumWidth - ? minimumWidth + Math.floor(random() * (maximumWidth - minimumWidth + 1)) - : Math.floor(remaining / (bandsLeft + 1)) - + (index < remaining % (bandsLeft + 1) ? 1 : 0); + if (!target) break; - widths.push(width); - remaining -= width; + const moveHorizontallyFirst = random() < 0.5; + const moveHorizontally = (): void => { + while (current.column !== target!.column) { + append({ + column: current.column + (target!.column > current.column ? 1 : -1), + row: current.row, + }); + } + }; + const moveVertically = (): void => { + while (current.row !== target!.row) { + append({ + column: current.column, + row: current.row + (target!.row > current.row ? 1 : -1), + }); + } + }; + + if (moveHorizontallyFirst) { + moveHorizontally(); + moveVertically(); + } else { + moveVertically(); + moveHorizontally(); + } } - return widths; + return path; } function createSeededRandom(seed: number): () => number { @@ -203,12 +244,17 @@ export function buildScoreEvents( cellByKey: ReadonlyMap, ): ScoreEvent[] { const events: ScoreEvent[] = []; + const scoredCells = new Set(); let score = 0; path.forEach((position, eatenAtFrame) => { - const day = cellByKey.get(positionKey(position)); + const key = positionKey(position); + if (scoredCells.has(key)) return; + + const day = cellByKey.get(key); if (!day || day.contributionLevel === "NONE") return; + scoredCells.add(key); score += 1; events.push({ position, score, eatenAtFrame }); }); @@ -294,7 +340,11 @@ export function renderCommitManGif( cellByKey.set(positionKey({ column, row }), day); } - const visitOrder = new Map(path.map((position, index) => [positionKey(position), index])); + const visitOrder = new Map(); + path.forEach((position, index) => { + const key = positionKey(position); + if (!visitOrder.has(key)) visitOrder.set(key, index); + }); const scoreEvents = buildScoreEvents(path, cellByKey); const encoder = GIFEncoder(); const radius = Math.max(3, cellSize * 0.66); From bd1a4e71878b7c96ebbfdfefd7ed88e9b83148ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:27:42 +0300 Subject: [PATCH 2/4] Test offset runs, full coverage, and natural revisits --- tests/animation.test.ts | 85 ++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 40 deletions(-) diff --git a/tests/animation.test.ts b/tests/animation.test.ts index 342a957..2847bfd 100644 --- a/tests/animation.test.ts +++ b/tests/animation.test.ts @@ -16,33 +16,35 @@ const sampleDays: ContributionDay[] = [ { date: "2026-10-13", contributionCount: 5, contributionLevel: "SECOND_QUARTILE" }, ]; -test("creates a continuous serpentine path through every grid cell", () => { - const path = buildZigzagPath(3); - assert.equal(path.length, 21); - assert.deepEqual(path.slice(0, 6), [ - { column: 0, row: 0 }, - { column: 1, row: 0 }, - { column: 2, row: 0 }, - { column: 2, row: 1 }, - { column: 1, row: 1 }, - { column: 0, row: 1 }, - ]); +function directionSegments(path: readonly { column: number; row: number }[]) { + const segments: Array<{ deltaColumn: number; deltaRow: number; length: number }> = []; for (let index = 1; index < path.length; index += 1) { const previous = path[index - 1]!; const current = path[index]!; - assert.equal(Math.abs(current.column - previous.column) + Math.abs(current.row - previous.row), 1); + const deltaColumn = current.column - previous.column; + const deltaRow = current.row - previous.row; + const last = segments[segments.length - 1]; + + if ( + last && + last.deltaColumn === deltaColumn && + last.deltaRow === deltaRow + ) { + last.length += 1; + } else { + segments.push({ deltaColumn, deltaRow, length: 1 }); + } } -}); -test("builds a deterministic zigzag with varied run lengths", () => { - const columns = 17; - const seed = 20261012; - const path = buildZigzagPath(columns, seed); + return segments; +} - assert.deepEqual(path, buildZigzagPath(columns, seed)); - assert.equal(path.length, columns * 7); +test("builds a connected route that visits every cell, allowing natural revisits", () => { + const columns = 3; + const path = buildZigzagPath(columns); assert.deepEqual(path[0], { column: 0, row: 0 }); + assert.deepEqual(path, buildZigzagPath(columns)); assert.equal( new Set(path.map(({ column, row }) => `${column}:${row}`)).size, columns * 7, @@ -62,32 +64,34 @@ test("builds a deterministic zigzag with varied run lengths", () => { `path jumps between ${previous.column}:${previous.row} and ${current.column}:${current.row}`, ); } +}); - const firstHorizontalRunLength = path.findIndex((position) => position.row !== 0); - assert.ok(firstHorizontalRunLength >= 8 && firstHorizontalRunLength <= 14); - assert.ok(firstHorizontalRunLength < columns, "the first run should end before crossing the entire garden"); +test("moves forward across offset runs before turning back", () => { + const seed = 20261012; + const path = buildZigzagPath(53, seed); + assert.deepEqual(path, buildZigzagPath(53, seed)); + assert.deepEqual(path[0], { column: 0, row: 0 }); + assert.equal( + new Set(path.map(({ column, row }) => `${column}:${row}`)).size, + 53 * 7, + "the route must eventually visit every contribution cell", + ); - const normalGridPath = buildZigzagPath(53, seed); - const horizontalRunLengths: number[] = []; - let horizontalRunLength = 1; + const segments = directionSegments(path); + assert.equal(segments[0]!.deltaColumn, 1); + assert.ok(segments[0]!.length >= 6 && segments[0]!.length <= 7); - for (let index = 1; index < normalGridPath.length; index += 1) { - const previous = normalGridPath[index - 1]!; - const current = normalGridPath[index]!; + assert.equal(segments[1]!.deltaColumn, 0); + assert.equal(segments[1]!.length, 2, "the first long run should be followed by a two-cell drop"); - if (previous.row === current.row) { - horizontalRunLength += 1; - } else { - horizontalRunLengths.push(horizontalRunLength); - horizontalRunLength = 1; - } - } - horizontalRunLengths.push(horizontalRunLength); + assert.equal(segments[2]!.deltaColumn, 1); + assert.ok(segments[2]!.length >= 6 && segments[2]!.length <= 7); + assert.equal(segments[3]!.deltaColumn, 0); + assert.equal(segments[3]!.length, 1); - assert.ok( - horizontalRunLengths.every((length) => length >= 8), - `normal-grid path contains a short horizontal run: ${horizontalRunLengths.join(", ")}`, - ); + assert.equal(segments[4]!.deltaColumn, 1); + assert.ok(segments[4]!.length >= 2 && segments[4]!.length <= 4); + assert.equal(segments[6]!.deltaColumn, -1, "the route reverses after the shorter run rather than after every row"); }); test("keeps empty contribution cells gray throughout the animation", () => { @@ -111,6 +115,7 @@ test("awards increasing scores only for green contribution cells", () => { { column: 0, row: 0 }, { column: 1, row: 0 }, { column: 2, row: 0 }, + { column: 1, row: 0 }, { column: 0, row: 1 }, ]; const cellByKey = new Map([ From 74249bc1676bfad1250eec4362fb0289642b9ea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:29:03 +0300 Subject: [PATCH 3/4] Restore score popup rendering constants --- src/animation.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/animation.ts b/src/animation.ts index aa2333c..0cf15a0 100644 --- a/src/animation.ts +++ b/src/animation.ts @@ -49,6 +49,24 @@ const HORIZONTAL_RUN_LENGTHS = [6, 7, 7] as const; const SHORT_RUN_MIN = 2; const SHORT_RUN_MAX = 4; const VERTICAL_SHIFT_PATTERN = [2, 1, 1, 2, 2] as const; +const SCORE_POPUP_DURATION_FRAMES = 5; +const SCORE_POPUP_RISE_PER_FRAME = 1; +const SCORE_POPUP_MAX_RISE = 4; +const SCORE_GLYPH_WIDTH = 3; +const SCORE_GLYPH_HEIGHT = 5; +const SCORE_GLYPHS: Record = { + "+": ["...", ".#.", "###", ".#.", "..."], + "0": ["###", "#.#", "#.#", "#.#", "###"], + "1": [".#.", "##.", ".#.", ".#.", "###"], + "2": ["###", "..#", "###", "#..", "###"], + "3": ["###", "..#", "###", "..#", "###"], + "4": ["#.#", "#.#", "###", "..#", "..#"], + "5": ["###", "#..", "###", "..#", "###"], + "6": ["###", "#..", "###", "#.#", "###"], + "7": ["###", "..#", "..#", ".#.", ".#."], + "8": ["###", "#.#", "###", "#.#", "###"], + "9": ["###", "#.#", "###", "..#", "###"], +}; /** * Build a deterministic roaming route through the contribution garden. From 540bcced33be041e51be2e2f9c33b7b26332b30d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:32:23 +0300 Subject: [PATCH 4/4] Let roaming runs cross more of the grid before returning --- src/animation.ts | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/animation.ts b/src/animation.ts index 0cf15a0..a17e76d 100644 --- a/src/animation.ts +++ b/src/animation.ts @@ -93,7 +93,9 @@ export function buildZigzagPath( const visited = new Set([positionKey(path[0]!)]); let current = path[0]!; let horizontalDirection = 1; - let segmentsUntilTurn = 3; + let segmentsUntilTurn: number | undefined = 3; + let earlyTurnAllowed = true; + let rearmAtColumn: number | undefined; const append = (position: GridPosition): void => { path.push(position); @@ -101,8 +103,8 @@ export function buildZigzagPath( visited.add(positionKey(position)); }; - // Roam in long horizontal bursts before doing a tidy coverage pass. - // This creates the less repetitive movement pattern requested for the GIF. + // Keep moving in the same direction across multiple runs. The occasional + // short return is followed by a full traverse before another early turn. for ( let segmentIndex = 0; segmentIndex < columns && visited.size < totalCells; @@ -129,10 +131,25 @@ export function buildZigzagPath( if (visited.size === totalCells) break; const atGridEdge = current.column === 0 || current.column === columns - 1; - if (hitGridEdge || atGridEdge || segmentsUntilTurn <= 1) { + if (hitGridEdge || atGridEdge) { horizontalDirection *= -1; - segmentsUntilTurn = random() < 0.7 ? 3 : 2; - } else { + + if (!earlyTurnAllowed && current.column === rearmAtColumn) { + earlyTurnAllowed = true; + segmentsUntilTurn = random() < 0.35 + ? 3 + Math.floor(random() * 2) + : undefined; + } else if (earlyTurnAllowed) { + segmentsUntilTurn = random() < 0.35 + ? 3 + Math.floor(random() * 2) + : undefined; + } + } else if (earlyTurnAllowed && segmentsUntilTurn === 1) { + horizontalDirection *= -1; + earlyTurnAllowed = false; + rearmAtColumn = horizontalDirection > 0 ? 0 : columns - 1; + segmentsUntilTurn = undefined; + } else if (earlyTurnAllowed && segmentsUntilTurn !== undefined) { segmentsUntilTurn -= 1; }