Skip to content

Latest commit

 

History

23 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

   __       __       _                        __  _           _                
  / /____  / /______(_)____      ____  ____  / /_(_)___ ___  (_)___  ___  _____
 / __/ _ \/ __/ ___/ / ___/_____/ __ \/ __ \/ __/ / __ `__ \/ /_  / / _ \/ ___/
/ /_/  __/ /_/ /  / (__  )_____/ /_/ / /_/ / /_/ / / / / / / / / /_/  __/ /    
\__/\___/\__/_/  /_/____/      \____/ .___/\__/_/_/ /_/ /_/_/ /___/\___/_/     
                                   /_/                                         
A Go program that reads a list of tetrominoes from a file and assembles them into the smallest possible square.

How It Works

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 ..


Usage

go run . <path_to_file>

Example:

go run . sample.txt

Example output:

ABBBB.
ACCCEE
AFFCEE
A.FFGG
HHHDDG
.HDD.G

Input File Format

  • 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:

#...
#...
#...
#...

....
####
....
....

.##.
.##.
....
....

Error Handling

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

Project Structure

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

Running Tests

go test ./...

Verbose:

go test -v ./...

Algorithm

  1. Parse the file into a slice of tetrominoes
  2. Validate each tetromino
  3. Normalize each tetromino's coordinates to start at [0,0]
  4. Calculate the minimum board size: ceil(sqrt(4N)) where N is the number of tetrominoes
  5. Use backtracking to place all tetrominoes on the board
  6. If no solution is found for the current size, increment the board size and retry
  7. Print the solved board

Requirements

  • Go 1.25 or higher
  • Only standard Go packages are used

About

Go backtracking solver that packs validated tetrominoes into the smallest possible square grid.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages