Advent of Code 2019 Day 15 in Python I did Advent of Code recently and enjoyed Day 15, which involves exploring a maze (my attempt in Kotlin). I've tried to recreate it partially in Python, minus the emulator:

https://repl.it/@MalcolmCrum/ReflectingFarNaturaldocs

It works but my code is largely 1:1 from Kotlin, and I'm not certain if I'm doing things in a Pythonic way. For example, I use this pattern regularly in Kotlin:

val direction = when (path.first()) {
            position + NORTH -&gt; NORTH
            position + SOUTH -&gt; SOUTH
            position + EAST -&gt; EAST
            position + WEST -&gt; WEST
            else -&gt; error("Can't find first step from $position towards $destination in $path")
        }


The Python equivalent I found is this, which while nice and concise is maybe a little too "clever"? (ignore the 1 offset, I changed path logic slightly)

direction: Direction = {self.position + d: d for d in Direction}[path_to_destination[1]]
You have:

direction: Direction = {self.position + d: d for d in Direction}[path_to_destination[1]]


The dict is just mapping each key to key - self.position (I don't know your exact data models, but I'm going to assume that these are some kind of vector and that + and - behave in intuitive ways).  Hence:

direction = path_to_destination[1] - self.position


and maybe:

assert abs(direction) == 1


if you want to require that direction is a unit vector (again, I'm assuming your vector representation is reasonable and implements __abs__ to return the magnitude).