import re


def parse_unit(line, unit_type):
    """Parse line to unit representation with unit_type."""
    regex = r'''(\d+)             # number of units
                [\w\s]+?
                (\d+)             # hit points
                [\w\s]+?
                (\([\w;,\s]+\))?  # immunity and weaknesses
                [\w\s]+?
                (\d+\s\w+)   # damage with type
                [\w\s]+?
                (\d+)             # initiative'''
    count, hit_points, immunes, damage, initiative = re.findall(
        regex, line, re.VERBOSE)[0]
    damage, damage_type = [
        int(part) if part.isdigit() else part for part in damage.split()]
    count, hit_points, initiative = [
        int(num) for num in (count, hit_points, initiative)]
    immune = set()
    weak = set()
    if immunes:
        immunes = immunes[1:-1].split('; ')
        for attributes in immunes:
            to_type, attributes = attributes.split(' to ')
            if to_type == 'immune':
                immune = set(attributes.split(', '))
            else:
                weak = set(attributes.split(', '))
    return {
        'type': unit_type,
        'count': count,
        'hit points': hit_points,
        'immune': immune,
        'weak': weak,
        'damage': damage,
        'damage_type': damage_type,
        'initiative': initiative
    }