Featured resource
Forrester Wave Report 2025
Pluralsight named a Leader in the Forrester Wave™

Our tech skill development platform earned the highest scores possible across 11 criteria.

Learn more
  • Labs icon Lab
  • Core Tech
Labs

Guided: Solve Puzzles with TypeScript Interfaces and Data Structures

In this hands on lab, you will build a Sudoku Solver using TypeScript and node.js. A sequence of guided steps will walk you through some of the core concepts of building applications with TypeScript, including creating type definitions and processing data at scale. By the end of this lab, you'll have built a functional Sudoku solver capable of solving any easy-difficulty Sudoku puzzle in the blink of an eye.

Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 22m
Published
Clock icon Jan 30, 2024

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Load and Parse a .txt File Containing Sudoku Puzzles

    Real-world programming tasks often require loading data and parsing it correctly. This makes sense, as data is often considered an organization's most important asset. Therefore, you'll next implement a function which can load a text file from any specified location in the project directory.

  2. Challenge

    Create Type Definitions for Solving the Sudoku Puzzle

    Sudoku is a fascinating game of skill and concentration enjoyed by people all around the world. If you’ve ever solved a Sudoku puzzle, you know the satisfying and rewarding feeling that comes from completing one of these simple yet ingenious challenges.

    In this lab, you'll build an automated Sudoku solver that can produce the solutions to easy-level puzzles nearly instantaneously. Throughout, you'll learn about efficient data processing, TypeScript interfaces, and you might even become a better Sudoku player as well!

    Before you begin parsing the files containing Sudoku data, it can be helpful both for understanding the problem and building a maintainable and scalable codebase, to define types.

    In TypeScript, type definitions provide the IDE (integrated development environment) and compiler with information about how variables you define should interact with one another.

    Each square on a Sudoku board can have a value from 1 to 9, or currently have no value. For squares without a value, 0 is typically used.

    While you could represent the type of the value as a number or a string, neither is very precise. Numbers and strings can have any number of possible values, but a Sudoku value can only have one of ten. A Sudoku puzzle contains 81 squares, arranged in a 9x9 grid. You'll start by creating a type for a Sudoku square.

    Each square has the following properties:

    1. A definite value, which is set when you're certain what the square contains. This is a SudokuValue.
    2. A map of possible values, which is an Object where each key can be a SudokuValue, and each value either true or false
    3. An X coordinate
    4. A Y coordinate

    The Xand Y coordinate may seem like regular numbers, but since they can only be an integer between 0 and 8, you can define a custom type for them as well. Great work!

    You may now optionally create Type Defintions for

    1. SudokuPuzzle, which is simply an array of SudokuSquares
    2. SudokuUnit, which refers to a row or column, and is also an array of SudokuSquares.

    If you do not wish to create these additional types, use the any when we refer to them later on in this code lab.

  3. Challenge

    Write an Algorithm That Solves a Sudoku Puzzle

    Now that your Sudoku data is formatted as an array of Sudoku Squares, you can solve any simple puzzle.

    A SudokuUnit is one group of 9 SudokuSquare elements which can contain each number from 1 to 9 exactly once. In other words, each row, each column, and some 9x9 squares are Sudoku Units. In total, there are 27.

    You now have all the information you need to solve any easy-level difficulty Sudoku puzzle.

    In any given unit, the numbers 1 through 9 can each appear only once. You can use this constraint to solve the puzzle with a simple iterative algorithm:

    • For a given unit:
      • Note all the definiteValues that are in this unit
      • All these values may be removed from the possibleValues of each square in the unit
      • If a square has only one possibleValue, that value may be set as the square's definite value.

    Squares can hold information about interactions with different Sudoku Units, as the possibleValues properties persists between iterations.

  4. Challenge

    Solve All Sudoku Puzzles

    You can now use your algorithm to solve any easy-level Sudoku Puzzle.

    Of course, using a computer algorithm to process data is much faster and more efficient than doing it yourself.

    This concept is at the heart of "processing data at scale."

    The puzzles directory contains three puzzles:

    1.txt
    2.txt
    3.txt
    

    For the final task, you'll create a method that solves all three. Congratulations on completing this lab!

    In this lab, you learned the following things:

    • Writing TypeScript applications
    • Creating TypeScript type definitions, including union types
    • Loading text files with the fs module
    • Algorithms for solving Sudoku puzzles

    In summary, the programmatic nature of solving Sudoku puzzles make it easy and rewarding to solve these puzzles using computer code. Doing so also gives you a better understanding of the processes that go on in your own brain while solving these callenging puzzles.

Daniel Stern is a freelance web developer from Toronto, Ontario who specializes in Angular, ES6, TypeScript and React. His work has been featured in CSS Weekly, JavaScript Weekly and at Full Stack Conf in England.

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.