I would say this is not horrible code - or at least, it's "hollywood horrible". [1] Yes, it's not directly intuitive and probably would need some illustrating comments, but it's still reasonably close to the "naive" algorithm that you could quickly figure out how it works. More importantly, it's still localized, concrete and side-effects free: The code has a clear goal and accomplishes that goal completely inside that one function. The non-intuitive parts also have a clear reason.
I think actual horrible code is more something that uses global mutable state, has logic spread over half a dozen units or does have weird roundabout implementations - however not because of a particular reason but because of unclear goals, big-ball-of-mud architecture, evolving codebase etc.
I think actual horrible code is more something that uses global mutable state, has logic spread over half a dozen units or does have weird roundabout implementations - however not because of a particular reason but because of unclear goals, big-ball-of-mud architecture, evolving codebase etc.
[1] https://tvtropes.org/pmwiki/pmwiki.php/Main/HollywoodHomely