But now all of sudden 17 zeroes in the beginning followed by the byte with value 1 results in an address of length 27 again.
What logic is there? It is said that the number of 1s in the beginning of the address depends on the number of zeroes in the beginning of the address payload but this dependency is no way trivial.
The logic is that a hash160 with 17 leading zeros and the 18th byte being 1 still has 2 following bytes that, even if zero, are not leading. So, those remaining 3 bytes concatenated with the 4 byte checksum is converted to base58. That number is larger than the 5 bytes of a hash160 with 19 leading zeros so the resulting encoding is longer.