From watching them, I feel that snakes are several orders of intelligence above small lizards. I have a pet door panel installed next to a sliding glass door. That leaves a gap, which is remedied by gluing a squeegee-like vinyl strip to the slider frame to block air drafts (and insects). It still leaves a very narrow slit open to the backyard at top and bottom. Frogs and lizards regularly find their way inside....and cannot for the life of them figure out where to go to leave. One morning (I love all herps, mind you), a four foot long Black Coach Whip that is a regular visitor in the yard was inside my house, ten feet from that slider door, half raised up, turning her head side to side, tongue flicking/smelling. I had just stepped into the kitchen, then she noticed me, turned about gracefully and with dignity went straight to the tiny gap and back out side. She did not bang against the glass in panic and cluelessness. She just left exactly as she came in, with no hesitation or confusion.
In other words, she exhibited advanced spatial orientation memory, and possibly experience memory that glass is a barrier.
Smart animal!