Instead of looping through all the chain atoms for every hex to look for ones that fit, preprocess them so the lookup can be done in constant time.