Esempio n. 1
0
def test_get_total_seconds():
    steps = load_values_list(EXAMPLE, as_int=False)
    queue = answer.Queue(*steps, workers=2)
    for step in queue.step_map.values():
        step.duration = ord(step.name) - 64
    queue.run()
    assert queue.total_seconds == 15
Esempio n. 2
0
def load_instructions(path: pathlib.Path = INPUT2) -> List[Instruction]:
    values: Values = load_values_list(path)
    instructions = []
    for value in values:
        instructions.append(
            Instruction(*(int(x) for x in PATTERN.findall(value)))
        )
    return instructions
Esempio n. 3
0
def get_satellites(path: pathlib.Path = INPUT) -> List[Satellite]:
    values = load_values_list(path, as_int=False)
    satellites = []
    for value in values:
        pos_x, pos_y, vel_x, vel_y = (int(x) for x in PATTERN.findall(value))
        satellites.append(
            Satellite(position=Point(pos_x, pos_y), velocity=Velocity(vel_x, vel_y))
        )
    return satellites
Esempio n. 4
0
def get_program(path: pathlib.Path = INPUT) -> Program:
    values: Values = load_values_list(path)
    ip = int(values.pop(0).split()[-1])
    instr = []
    for value in values:
        op, a, b, c = value.split()
        instr.append(Instruction(op, int(a), int(b), int(c)))

    program: Program = (ip, instr)
    return program
Esempio n. 5
0
def load_examples(path: pathlib.Path = INPUT) -> List[InstructionSet]:
    values: Values = load_values_list(path)
    examples = []
    for before, instruction, after in chunks(values, 3):  # Instruction sets are groups of three lines
        before: State = tuple(int(x) for x in PATTERN.findall(before))
        after: State = tuple(int(x) for x in PATTERN.findall(after))
        instruction = Instruction(*(int(x) for x in PATTERN.findall(instruction)))
        examples.append(InstructionSet(before, instruction, after))

    return examples
Esempio n. 6
0
def get_raw_log(path: pathlib.Path = INPUT) -> RawLog:
    values: Values = load_values_list(path, as_int=False)
    log: RawLog = []
    for value in values:
        match = LOG_PATTERN.match(value)
        if match:
            date = datetime.datetime.strptime(match.group('datestring'), DATE_PATTERN)
            log.append(RawLogEntry(date, match.group('entry')))
    log.sort(key=LOG_SORT_KEY)

    return log
Esempio n. 7
0
def get_coordinates(path: pathlib.Path = INPUT,
                    delim: str = ', ') -> Coordinates:
    values = load_values_list(path, as_int=False)
    coords = []
    for value in values:
        x, y = value.split(delim)
        coords.append((
            int(x),
            int(y),
        ))
    return coords
Esempio n. 8
0
def count_repeat_offenders(path: pathlib.Path = INPUT,
                           max_repeats: int = 3) -> RepeatOffenders:
    max_repeats: int = max_repeats + 1 if max_repeats > 2 else 3
    values: Values = load_values_list(path, as_int=False)
    offenders: RepeatOffenders = collections.defaultdict(int)
    for value in values:
        counter = collections.Counter(value)
        logger.debug("Line <%s>: %s", value, counter)
        for iteration in range(2, max_repeats):
            if iteration in counter.values():
                offenders[iteration] += 1
        logger.debug(offenders)
    return offenders
Esempio n. 9
0
def locate_matches(path: pathlib.Path = INPUT, max_dist: Distance = 1) -> Repeaters:
    values: Values = load_values_list(path, as_int=False)
    candidates = set(values)
    repeaters: Repeaters = {}
    tree = BKTree(levenshtein_distance, *candidates)
    while candidates:
        candidate = candidates.pop()
        matches = set(y for x, y in tree.match(candidate, max_dist) if y != candidate)
        if matches:
            repeaters[candidate] = matches
            candidates -= set(matches)

    return repeaters
Esempio n. 10
0
def load_vectors(path: pathlib.Path = INPUT) -> List[Vector]:
    values: Values = load_values_list(path)
    vectors = []
    for value in values:
        print(value)
        match = PATTERN.match(value)
        kwargs = {
            match.group('one'):
            OrderedSet([int(match.group('val'))]),
            match.group('two'):
            OrderedSet(
                range(int(match.group('start')),
                      int(match.group('stop')) + 1))
        }
        vectors.append(Vector(**kwargs))

    return vectors
Esempio n. 11
0
def get_boxes(path: pathlib.Path = INPUT) -> List[BoundingBox]:
    values: Values = load_values_list(path, as_int=False)
    return [BoundingBox.from_str(x) for x in values if BoundingBox.BOUNDING_BOX_PATTERN.match(x)]
Esempio n. 12
0
def get_license_key(path: pathlib.Path = INPUT) -> List[int]:
    return flatten_iter(
        [map(int, x.split()) for x in load_values_list(path, as_int=False)])
Esempio n. 13
0
def sum_file(path: pathlib.Path = INPUT) -> Sum:
    total: Sum = sum(load_values_list(path))
    return total
Esempio n. 14
0
def iter_duped_totals(path: pathlib.Path = INPUT) -> Duped:
    values: Values = load_values_list(path)
    seen: Seen = set()
    duped: Duped = (total for total in accumulate(cycle(values))
                    if total in seen or seen.add(total))
    return duped
Esempio n. 15
0
def test_get_order_with_no_workers():
    steps = load_values_list(EXAMPLE, as_int=False)
    queue = answer.Queue(*steps)
    queue.run()
    assert "".join(x.name for x in queue.final) == 'CABDFE'
Esempio n. 16
0
def get_string(path: pathlib.Path = INPUT) -> Value:
    return load_values_list(path, as_int=False)[-1]