For a binary symmetric channel with maximum-likelihood decoding, the problem of finding the optimal code with fixed blocklength and codebook size is open in general. We solve the problem for codes of four codewords and blocklength up to 8 analytically, and up to 300 by combining analysis and computer evaluation. Our approach has the potential to extend to larger codebooks.