Skip to content

🧩 Rush01 (42 Paris Piscine) β€” Sub-millisecond Skyscrapers puzzle solver in C, built with a custom backtracking core and a handcrafted constraint propagation engine.

Notifications You must be signed in to change notification settings

guillaumeast/42_rush01

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ™οΈ Rush01 β€” Skyscrapers Solver (v1.2.0)

Optimized backtracking solver for the 42 Paris Rush01 puzzle.
v1.2.0 introduces early pruning based on partial visibility counts, achieving sub-millisecond solves up to 9Γ—9.

Language: C Type: CLI Platform: macOS/Linux Status: v1.2.0 (baseline)


πŸŽ“ Context

This project is part of the 42 Piscine / Rush series. It solves the classic Skyscrapers puzzle (a.k.a. Rush01): fill an nΓ—n grid with values 1..n so that each row/column contains all numbers once, and each side clue indicates how many β€œbuildings” are visible from that side.


🎯 Objective

  • Parse command-line input of 4*n edge clues (top, bottom, left, right).
  • Pre-fill obvious values before backtracking:
    • If clue = n β†’ the entire row/column is {1..n} in the correct direction.
    • If clue = 1 β†’ the first cell on the clue side is set to n.
  • Early pruning based on partial visibility counts:
    • Dynamically check partial rows/columns against their visibility clues.
    • Stop recursion early when the current state can no longer match the clue.
  • Print the resulting grid to stdout, or Error\n when no solution exists or input is invalid.
  • Keep the code Norm-friendly and simple for learning/debugging.

πŸ—‚οΈ Repository Structure (v1.2.0)

.
β”œβ”€β”€ CHANGELOG.md
β”œβ”€β”€ ex00
β”‚Β Β  β”œβ”€β”€ includes
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ args.h
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ check.h
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ fix_max.h
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ fix_min.h
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ map.h
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ print.h
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ run.h
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ rush01.h
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ solve.h
β”‚Β Β  β”‚Β Β  └── types.h
β”‚Β Β  β”œβ”€β”€ Makefile
β”‚Β Β  └── srcs
β”‚Β Β      β”œβ”€β”€ args.c
β”‚Β Β      β”œβ”€β”€ check.c
β”‚Β Β      β”œβ”€β”€ main.c
β”‚Β Β      β”œβ”€β”€ map.c
β”‚Β Β      β”œβ”€β”€ print.c
β”‚Β Β      β”œβ”€β”€ run.c
β”‚Β Β      └── solver
β”‚Β Β          β”œβ”€β”€ fix_max.c
β”‚Β Β          β”œβ”€β”€ fix_min.c
β”‚Β Β          └── solve.c
β”œβ”€β”€ README.md
β”œβ”€β”€ rush01.en.subject.pdf
└── tests.txt

Subject included: rush01.en.subject.pdf


βš™οΈ Build

From ex00/:

make
# produces ./rush-01

Clean targets:

make clean   # remove objects
make fclean  # remove objects + binary
make re      # full rebuild

▢️ Run

The program expects 4*n clues (top, bottom, left, right order) as a single argument list.
Example for 6Γ—6:

./rush01 "6 5 4 3 2 1 1 2 2 2 2 2 6 5 4 3 2 1 1 2 2 2 2 2"

Successful output prints the solved grid:

1 2 3 4 5 6
2 3 4 5 6 1
3 4 5 6 1 2
4 5 6 1 2 3
5 6 1 2 3 4
6 1 2 3 4 5

On invalid/unsatisfiable inputs, prints Error\n.

Tip: See tests.txt for ready-to-run samples.


πŸ§ͺ Performance

Results measured on the author’s machine (macOS / Apple M4 / Xcode Time Profiler / main() execution time):

Size v1.0.0 (baseline) v1.1.0 (obvious values) v1.2.0 (early pruning)
3Γ—3 < 1 ms < 1 ms < 1 ms
4Γ—4 < 1 ms < 1 ms < 1 ms
5Γ—5 < 1 ms < 1 ms < 1 ms
6Γ—6 66 ms 4 ms < 1 ms
7Γ—7 ~600 000 ms ~5 000 ms < 1 ms
8Γ—8 > 30 min > 30 min < 1 ms
9Γ—9 > 30 min > 30 min < 1 ms

v1.1.0 added a pre-filling of obvious values (for clue = 1 and clue = n). v1.2.0 introduces real-time pruning of impossible branches using partial visibility checks.
With this, the solver reaches sub-millisecond performance up to 9Γ—9 grids. ⚑️


β€œFrom brute force to elegance β€” one segfault at a time.” 🧠

About

🧩 Rush01 (42 Paris Piscine) β€” Sub-millisecond Skyscrapers puzzle solver in C, built with a custom backtracking core and a handcrafted constraint propagation engine.

Topics

Resources

Stars

Watchers

Forks