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
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
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
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
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
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
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
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
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
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
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)]
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)])
def sum_file(path: pathlib.Path = INPUT) -> Sum: total: Sum = sum(load_values_list(path)) return total
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
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'
def get_string(path: pathlib.Path = INPUT) -> Value: return load_values_list(path, as_int=False)[-1]