tricky logic problem