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

Mazes

curriculum-main/exercises/mazes.livemd

Mazes

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue Family TreeFunctions

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

We’ve created a map that represents the following maze. Each cardinal direction (north, south, west, east) necessary to navigate the maze is stored as a key in the map.

Use map.key notation to access the "Exit!" string.

Example solution

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

maze.south.west.south.east.south.east.south
maze = %{
  south: %{
    west: %{
      south: %{
        east: %{
          south: %{
            east: %{
              south: "Exit!"
            }
          }
        }
      }
    }
  }
}

The String Maze

Use map[key] notation to access the maze and retrieve the "Exit!" string.

Example solution

maze = %{
  "south" => %{
    "east" => %{
      "south" => %{
        "west" => %{
          "south" => %{
            "west" => %{
              "south" => %{
                "east" => %{
                  "south" => "Exit!"
                }
              }
            }
          }
        }
      }
    }
  }
}

maze["south"]["east"]["south"]["west"]["south"]["west"]["south"]["east"]["south"]
maze = %{
  "south" => %{
    "east" => %{
      "south" => %{
        "west" => %{
          "south" => %{
            "west" => %{
              "south" => %{
                "east" => %{
                  "south" => "Exit!"
                }
              }
            }
          }
        }
      }
    }
  }
}

Bonus: Treasure Map

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

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

Example solution

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

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

(Extra Bonus) Update The Treasure Map

Use map update syntax to create and update the original treasure_map such that the "gold" is now "taken".

Example solution

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

We can update the treasure map using map update syntax %{old_map | updated_key}.

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

You might also have found the Kernel.put_in function for updating a deeply nested map.

put_in(
  treasure_map,
  ["south ten paces", 10, :"east three paces", [1, 2, 3], {"turn", "right"}, :dig],
  "taken"
)

Enter your solution below.

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

Bonus: Pattern Matching

Use pattern matching to bind the "Exit!" string to an exit variable.

maze = %{
  "south" => %{
    "east" => %{
      "south" => %{
        "west" => %{
          "south" => %{
            "west" => %{
              "south" => %{
                "east" => %{
                  "south" => "Exit!"
                }
              }
            }
          }
        }
      }
    }
  }
}

BONUS: 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.
  • Have a partner navigate your maze using either map[key] notation or map.key notation to retrieve the "Exit!" value.

Enter your solution below.

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Mazes exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue Family TreeFunctions