๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

hacking sorcerer

lantern walking problem

728x90
๋ฐ˜์‘ํ˜•

PROBLEM: The Lantern Path Decoder ๐Ÿฎ๐Ÿงญ

You’re exploring a hallway of lanterns. Each lantern is written as a character:

- `.` means an empty spot (safe to step on)
    
- `#` means a wall (cannot step on)
    
- `S` is your start
    
- `E` is your exit
    

You also find a “move spell” string that tells you how to try walking:

- `U` up, `D` down, `L` left, `R` right
    

But the hallway is tricky: some moves hit walls or go out of the map. Those moves are ignored (you stay where you are).

Your job is to simulate the walk and output:

1. Whether you ever reached `E`
    
2. How many unique spots you successfully stepped on (including `S`)
    
3. The first step number when you reached `E` (or `-1` if never)
    

Step numbers start at 1 for the first move character in the spell.

RULES โœ…

1. The map is a rectangle of characters (all rows same length).
    
2. Exactly one `S` and exactly one `E` exist.
    
3. You begin on `S`.
    
4. For each move in the spell:
    
    - Compute the next cell.
        
    - If it is outside the map OR it is `#`, ignore the move (stay put).
        
    - Otherwise, move onto it.
        
5. Count unique visited cells where you actually stand after each move (including starting cell).
    
6. The moment you stand on `E`, record the step number if it’s the first time.
    
7. Output three values:
    
    - `REACHED` or `NOT REACHED`
        
    - number of unique visited cells
        
    - first step index when reached `E`, else `-1`
        

EXAMPLES ๐Ÿงฉ

Example 1  
Input:  
Grid:  
[  
"S..",  
".#.",  
"..E"  
]  
Moves: "RRDD"

Walk:

- R → (0,1) ok
    
- R → (0,2) ok
    
- D → (1,2) ok
    
- D → (2,2) = E reached at step 4
    

Output:  
REACHED 5 4

(Unique visited: S + 4 positions after moves = 5)

Example 2  
Input:  
Grid:  
[  
"S#E",  
"..."  
]  
Moves: "RR"

Walk:

- R tries to go into `#` → ignored
    
- R tries again → ignored
    

Output:  
NOT REACHED 1 -1

Example 3  
Input:  
Grid:  
[  
"S..E"  
]  
Moves: "RRR"

Walk:

- R → (0,1)
    
- R → (0,2)
    
- R → (0,3) = E reached at step 3
    

Output:  
REACHED 4 3

PYTHON SKELETON (core logic removed) ๐Ÿ

```python
from typing import List, Tuple

def decode_lantern_path(grid: List[str], moves: str) -> Tuple[str, int, int]:
    """
    Returns:
      (status, unique_visited_count, first_reach_step)
      status is "REACHED" or "NOT REACHED"
      first_reach_step is the 1-based index of the move when E was first reached, or -1
    """
    rows = len(grid)
    cols = len(grid[0]) if rows else 0

    # 1) Find S and E
    # TODO: locate start (sr, sc) and exit (er, ec)

    # 2) Track current position and visited set
    # TODO: initialize current position to S
    # TODO: visited = set of positions, include start

    # 3) Simulate each move
    first_reach_step = -1

    for i, ch in enumerate(moves, start=1):
        # TODO: compute (nr, nc) from (r, c) and move ch

        # TODO: if out of bounds or wall '#', ignore (stay)
        # TODO: else update (r, c) = (nr, nc)

        # TODO: add (r, c) to visited

        # TODO: if (r, c) is E and first_reach_step == -1, set it to i
        pass

    # 4) Build output status
    # TODO: status = "REACHED" if first_reach_step != -1 else "NOT REACHED"
    # TODO: unique_count = len(visited)

    # return status, unique_count, first_reach_step
    raise NotImplementedError


if __name__ == "__main__":
    grid = [
        "S..",
        ".#.",
        "..E"
    ]
    moves = "RRDD"
    print(decode_lantern_path(grid, moves))  # expected: ("REACHED", 5, 4)
```

A few “thinking questions” to make it more fun ๐Ÿง โœจ

- If you’re allowed to “flip” exactly one `#` into `.` one time, can you always reach `E`? How would you find the best wall to break?
    
- What if the spell can include `?` meaning “choose the best move”? How would you maximize unique cells visited?
    
- Could you detect if the moves will get stuck in a loop without simulating all steps?
    

728x90
๋ฐ˜์‘ํ˜•

'hacking sorcerer' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

fun Probability Puzzle  (0) 2025.12.30
lantern walking problem-solving  (0) 2025.12.29
๋ฌดํ•œ์˜ ๋ฐ”๋‘‘ ์ฆ๊ฑฐ์›€  (0) 2025.12.28
longest_palindrome.py  (0) 2025.12.27
scavenger_hunt.py  (0) 2025.12.24