__ __ _ __ _ _
/ /____ / /______(_)____ ____ ____ / /_(_)___ ___ (_)___ ___ _____
/ __/ _ \/ __/ ___/ / ___/_____/ __ \/ __ \/ __/ / __ `__ \/ /_ / / _ \/ ___/
/ /_/ __/ /_/ / / (__ )_____/ /_/ / /_/ / /_/ / / / / / / / / /_/ __/ /
\__/\___/\__/_/ /_/____/ \____/ .___/\__/_/_/ /_/ /_/_/ /___/\___/_/
/_/
A Go program that reads a list of tetrominoes from a file and assembles them into the smallest possible square.
The program takes a text file containing tetrominoes, validates them, and uses a backtracking algorithm to find the smallest NxN square that fits all of them. Each tetromino is labeled with a letter (A, B, C...) in the output. Empty cells are represented by ..
go run . <path_to_file>Example:
go run . sample.txtExample output:
ABBBB.
ACCCEE
AFFCEE
A.FFGG
HHHDDG
.HDD.G
- Each tetromino is represented as a 4x4 grid
- Valid characters are
#(filled cell) and.(empty cell) - Each tetromino must contain exactly 4 connected
#cells - Tetrominoes are separated by a blank line
Example file:
#...
#...
#...
#...
....
####
....
....
.##.
.##.
....
....
The program prints ERROR and exits in the following cases:
- No file argument provided
- File cannot be read
- Any tetromino does not have exactly 4 rows
- Any row does not have exactly 4 characters
- Any character is not
.or# - Any tetromino does not have exactly 4
#cells - Any tetromino has disconnected
#cells
tetris-optimizer/
├── main.go # Entry point and pipeline orchestration
├── formatter/
│ ├── format.go # File reading and parsing
│ ├── format_test.go
│ ├── validate.go # Input validation and connectivity check
│ ├── validate_test.go
│ ├── normalize.go # Coordinate extraction and normalization
│ └── normalize_test.go
└── solver/
├── solver.go # Board creation and backtracking solver
└── solver_test.go
go test ./...Verbose:
go test -v ./...- Parse the file into a slice of tetrominoes
- Validate each tetromino
- Normalize each tetromino's coordinates to start at
[0,0] - Calculate the minimum board size:
ceil(sqrt(4N))where N is the number of tetrominoes - Use backtracking to place all tetrominoes on the board
- If no solution is found for the current size, increment the board size and retry
- Print the solved board
- Go 1.25 or higher
- Only standard Go packages are used