How to stop contributors editing post type but allowing them to edit a custom post type?

Add the capabilities to the role.
Replace ‘cpt’ with the name of your custom post type:

$role = get_role( 'contributor' );
$role->add_cap( 'delete_published_cpt' );
$role->add_cap( 'delete_others_cpt' );
$role->add_cap( 'delete_cpt' );
$role->add_cap( 'edit_others_cpt' );
$role->add_cap( 'edit_published_cpt' );
$role->add_cap( 'edit_cpt' );
$role->add_cap( 'publish_cpt' );

But you shouldn’t add or remove caps on every page load.
That’s why a plugin is better.
You adjust caps on the plugin activation using register_activation_hook. And you can revert your changes using register_deactivation_hook because you have a conscience.