Skip to content

refactor: extract common graph construction patterns into graph.rs - #220

Draft
semiexp with Copilot wants to merge 2 commits into
mainfrom
copilot/refactor-graph-structure
Draft

refactor: extract common graph construction patterns into graph.rs#220
semiexp with Copilot wants to merge 2 commits into
mainfrom
copilot/refactor-graph-structure

Conversation

Copilot AI commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Several puzzle files under cspuz_rs_puzzles/src/puzzles/ were constructing identical or near-identical Graph structures inline. This PR factors out the recurring patterns into reusable functions in cspuz_rs/src/graph.rs.

New functions

  • graph_8_neighbors(shape) — h×w graph with 8-neighbor (4-directional + diagonal) connectivity.
    Used by: bramble.rs, cocktail.rs, tetrochain_common.rs

  • graph_8_neighbors_with_outer_vertex(shape) — same as above, plus a single outer vertex (h*w) connected to all border cells.
    Used by: nurimaze.rs, city_space.rs

  • active_edges_acyclic(solver, edges: &BoolGridEdges) — adds a no-cycle constraint on a set of grid edges via the planar dual graph. An identical ~40-line block was copy-pasted verbatim in mintonette.rs and move_common.rs.

  • graph_slash_connectivity(shape) / graph_slash_connectivity_with_outer_vertex(shape) — sub-cell connectivity graph for slash/backslash puzzles, splitting each cell into 4 parity-indexed quadrants.
    Used by: slashpack.rs / gokigen.rs

Example: before → after

// Before (repeated in bramble.rs, cocktail.rs, tetrochain_common.rs)
let mut g = graph::Graph::new(h * w);
for y in 0..h {
    for x in 0..w {
        if y < h - 1 { g.add_edge(y * w + x, (y + 1) * w + x); }
        if x < w - 1 { g.add_edge(y * w + x, y * w + x + 1); }
        if y < h - 1 && x < w - 1 { g.add_edge(y * w + x, (y + 1) * w + x + 1); }
        if y < h - 1 && x > 0     { g.add_edge(y * w + x, (y + 1) * w + x - 1); }
    }
}
graph::active_vertices_connected(&mut solver, is_black.flatten(), &g);

// After
let g = graph::graph_8_neighbors((h, w));
graph::active_vertices_connected(&mut solver, is_black.flatten(), &g);

Copilot AI and others added 2 commits April 12, 2026 14:08
Add four new utility functions to cspuz_rs/src/graph.rs:
- graph_8_neighbors: 8-neighbor connectivity graph for an h*w grid
- graph_8_neighbors_with_outer_vertex: same + outer vertex connected to border cells
- active_edges_acyclic: dual-graph acyclicity constraint for BoolGridEdges
- graph_slash_connectivity / graph_slash_connectivity_with_outer_vertex: sub-cell
  connectivity graph for slash/backslash puzzles

Update puzzle files to use these new helpers:
- bramble.rs, cocktail.rs, tetrochain_common.rs -> graph_8_neighbors
- nurimaze.rs, city_space.rs -> graph_8_neighbors_with_outer_vertex
- mintonette.rs, move_common.rs -> active_edges_acyclic
- gokigen.rs -> graph_slash_connectivity_with_outer_vertex
- slashpack.rs -> graph_slash_connectivity

Agent-Logs-Url: https://github.com/semiexp/cspuz_core/sessions/767797f1-b6e9-4190-9c51-8b5099df4a97

Co-authored-by: semiexp <7336994+semiexp@users.noreply.github.com>
@ReverM

ReverM commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

litherslink has the no acyclic constraint as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants