DEV Community

Shahrouz Nikseresht
Shahrouz Nikseresht

Posted on

Day 7: Tracking the Final Floor, Up, Down, and Logic All Around!

Welcome to Challenge #7 in the #80DaysOfChallenges adventure!
Today's problem takes us on a vertical journey, literally. We're going to track the final floor after a sequence of "Up" and "Down" moves. It's a simple, elegant exercise in counters, loops, and conditional logic, three pillars of programming that show up everywhere from game mechanics to robotics.


💡 Objective: From Ground to Sky (and Maybe Back Down)

The challenge is straightforward:
You start on the ground floor (0).
Each "U" moves you up one floor.
Each "D" takes you down one.

At the end of the sequence, you need to report the final floor number.
This might sound trivial, but this tiny problem hides big lessons: state tracking and incremental reasoning, core ideas that make complex simulations and data analysis possible.


🧠 The Thinking Behind the Code

Imagine being an elevator operator in a skyscraper. You receive a long string of commands like:

"UUDUDDDUUU"
Enter fullscreen mode Exit fullscreen mode

You don't know the destination in advance, you just follow the sequence, one move at a time.
To figure out where you end up, you keep a running total:

  • Add 1 for every "U"
  • Subtract 1 for every "D"

That running total represents your current floor.
By the end, it holds your final position.

Simple arithmetic, but the beauty lies in its clarity, each step is both cause and consequence.


🔍 The Final Solution

def track_final_floor(moves: str) -> int:
    """
    Return the final floor after processing all moves.
    """
    current_floor = 0  # Start from ground floor

    for move in moves:
        if move == "U":
            current_floor += 1  # Move up one floor
        elif move == "D":
            current_floor -= 1  # Move down one floor

    return current_floor
Enter fullscreen mode Exit fullscreen mode

Example Runs

print(track_final_floor("UUDU"))   # Expected output: 3
print(track_final_floor("DDDD"))   # Expected output: -4
Enter fullscreen mode Exit fullscreen mode

Output:

3
-4
Enter fullscreen mode Exit fullscreen mode

Nice and tidy, just how Python likes it.


⚙️ Why This Approach Works So Well

  1. Clear logic: Every instruction directly affects the state, no hidden complexity, no side effects.
  2. Teachable structure: It's a perfect example for beginners to understand loops and conditionals without mathematical intimidation.
  3. Expandable idea: You can easily build on this, count how often you reach ground floor, detect when you first go below it, or visualize the entire path as a graph.

This problem is a microcosm of how state tracking powers everything from GPS navigation to 2D game movement. Once you grasp this logic, you've learned a universal programming pattern.


🎯 Key Takeaway

At its heart, this challenge shows how cumulative logic turns simple steps into meaningful outcomes.
Each move might seem insignificant, but together they form a story, a journey through logic, told one increment at a time.

In future challenges, we'll expand on this by introducing more complex state management: what happens when multiple dimensions are involved, or when moves carry weights or costs?
The ladder goes higher, step by step.


🚀 Try It Yourself

Experiment with your own sequences:

  • What if you never come back to zero?
  • What if you want to know when you first hit the basement (negative floors)?

Once you've played with it, post your findings and code snippets online under #80DaysOfChallenges. Share how your logic handles the ups and downs, literally.


Challenge Resources

• Source Code for Challenge #7: scripts/elevator_game.py
• Main Repository: 80-days-of-challenges
• Daily Updates: Twitter/X (@Shahrouzlogs)

Top comments (0)