Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Mazes

mazes.livemd

Mazes

Mix.install([
  {:kino, github: "livebook-dev/kino", override: true},
  {:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
  {:vega_lite, "~> 0.1.4"},
  {:kino_vega_lite, "~> 0.1.1"},
  {:benchee, "~> 0.1"},
  {:ecto, "~> 3.7"},
  {:math, "~> 0.7.0"},
  {:faker, "~> 0.17.0"},
  {:utils, path: "#{__DIR__}/../utils"},
  {:tested_cell, git: "https://github.com/BrooklinJazz/tested_cell"}
])

Navigation

Return Home Report An Issue

Setup

Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.

Overview

In this exercise, you’re going to navigate a maze game.

Each maze is a deeply nested map.

If you navigate successfully, your answer will return "Exit!"

The Atom Maze

In the Elixir cell below, use map.key notation to navigate the following maze. The maze is in an atom_maze variable.

Replace nil with your answer.

atom_maze = %{
  south: %{
    west: %{
      south: %{
        east: %{
          south: %{
            east: %{
              south: "Exit!"
            }
          }
        }
      }
    }
  }
}

path = nil

Utils.feedback(:atom_maze, [atom_maze, path])

The String Maze

In the Elixir cell below, use map[key] notation to get to the end of the following maze. The maze is in a string_maze variable. Replace nil with your answer.

string_maze = %{
  "south" => %{
    "east" => %{
      "south" => %{
        "west" => %{
          "south" => %{
            "west" => %{
              "south" => %{
                "east" => %{
                  # using string_maze[key] syntax, access this deeply nested value.
                  "south" => "Exit!"
                }
              }
            }
          }
        }
      }
    }
  }
}

path = nil

Utils.feedback(:string_maze, [string_maze, path])

Create Your Own Maze

Generate a maze using the Online Maze Generator. It should be at least a 5 * 5 in width and height.

  • Create a deeply nested map to represent your custom_maze. It should have an "Exit!" value at the end.
  • Use either map[key] notation or map.key notation to show how to navigate your maze path.
  • BONUS: Use both atom keys and string keys in your custom_maze.

Replace nil with your answers below.

custom_maze = nil

path = nil

Utils.feedback(:custom_maze, [custom_maze, nil])

Treasure Map

You have been given a treasure map (pun intended). That will lead you to "gold".

Use map[key] and/or map.key to access the "gold" value in the treasure_map.

Here’s a simple example of how to take the gold:

treasure_map = %{
  "ten paces south" => %{
    10 => "gold"
  }
}

path = treasure_map["ten paces south"][10]

Replace nil with your answer below.

treasure_map = %{
  "south ten paces" => %{
    10 => %{
      :"east three paces" => %{
        [1, 2, 3] => %{
          {"turn", "right"} => %{
            :dig => "gold"
          }
        }
      }
    }
  }
}

path = nil

Utils.feedback(:treasure_map, [treasure_map, path])

(Bonus) Update the Treasure Map

Use map update syntax %{map | key => "new value"} or %{map | key: "new value"} and update the treasure_map to show the "gold" has been "taken".

For example,

treasure_map = %{
  "ten paces south" => %{
    10 => "gold"
  }
}

updated_map = %{treasure_map | "ten paces south" => %{10 => "taken"}}

Replace nil with your answer below.

treasure_map = %{
  "south ten paces" => %{
    10 => %{
      :"east three paces" => %{
        [1, 2, 3] => %{
          {"turn", "right"} => %{
            :dig => "gold"
          }
        }
      }
    }
  }
}

updated_map = %{}

Utils.feedback(:update_treasure_map, [treasure_map, updated_map])

Commit Your Progress

Run the following in your command line from the project folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish mazes exercise"