So while he was sleepwalking naked:
He unlocked his own door(im assuming his door was locked, he still had to open the door)
walked outside
closed his door(im assuming his door was closed)
went to the neighbors apartment
tried to enter the code on that door
realized he was at the wrong door
went to his apartment and entered the right code(im assuming his door was locked)
That's a lot of shyt to do while your sleepwalking, even if you take out the parts where Im assuming shyt. Sound like he was high as hell realized he was at the wrong door.