Implement a mock of the Unix cd (change directory) command.
The code does not have to change actual directories; it only needs to return the new path after cd is executed.
The function takes two arguments: the current working directory and the directory to change to, and returns the output directory as if the cd command were executed. There is a filesystem underneath, and all paths are valid.
Question statement
Example table of inputs and outputs:
cwd | cd(arg) | output
--- | --- | ---
/ | foo | /foo
/baz | /bar | /bar
/foo/bar | ../../.. | /
/x/y | ../p/../q | /x/q
/x/y | /p/./q | /p/q
This problem asks you to simulate Unix path resolution for cd without touching the filesystem. The key idea is to split the current path and target path into components, then normalize them with a stack: push normal directory names, pop on '..', and ignore '.' or empty segments. If the target path is absolute, start from root; otherwise, start from the current working directory. Finally, rebuild the canonical path from the stack.