Beispiel #1
0
def tokenize_context(context, center_word, dictionary):
    match_obj = SCWS_CONTEXT.match(context.lower())
    left_context = match_obj.group(1).strip()
    assert match_obj.group(2).strip() == center_word
    right_context = match_obj.group(3).strip()

    tokenization_type = 'wsi'

    return parse_line(left_context, dictionary,
                      tokenization_type), parse_line(right_context, dictionary,
                                                     tokenization_type)
Beispiel #2
0
 def read_in_image_subset(self):
     """
     Parses and reads in the csv files in the image directory. Looking for only
     those images that fit the user's requirements/choices.
     :return: Returns a list that contains information about certain images in the directory.
     """
     if len(self.choices) == 0:
         raise ValueError("self.choices array is EMPTY.")
     all_files = os.listdir(con.IMAGES_DIRECTORY)
     output_list = []
     for filename in all_files:
         extension = filename[-3:]
         if extension == "png":  # skip the image files
             continue
         new_path = os.path.join(con.IMAGES_DIRECTORY, filename)
         mylist = []
         # opens a .csv file and reads in its contents.
         with open(new_path, "r") as f:
             mylist = f.readlines()
             # The following line strips out newline characters and makes sure the line isn't empty.
             mylist = [i.strip() for i in mylist if len(i.strip()) > 0]
         # print(mylist)
         mylist = utils.parse_line(mylist)
         mylist.insert(0, filename)
         output_list.append(mylist)
     # ----------------------------
     output_list = self.apply_choices(output_list)
     # ----------------------------
     return output_list
def create_example(line):
    data = utils.parse_line(line.strip())

    input_image = create_input_image(args.input_dir, data['scan_id'],
                                     data['slice_id'], utils.INPUT_SHAPE)

    # normalize the image
    input_image = input_image / 0.0625

    if args.structure and args.structure not in data['structures']:
        return None
    mask = utils.create_joint_mask((512, 512), data['structures'],
                                   utils.LUNG_CLASSES)
    height, width = utils.INPUT_SHAPE

    classes = np.array([0] + [
        int(any(x in data['structures'] for x in v))
        for k, v in utils.LUNG_CLASSES
    ],
                       dtype=np.int32)

    print("Writing: ", data['structures'].keys(), "Classes:", classes,
          "Image max:", np.max(input_image), "Mask max: ", np.max(mask))

    example = tf.train.Example(features=tf.train.Features(
        feature={
            'height': _int64_feature(height),
            'width': _int64_feature(width),
            'image': _bytes_feature(input_image.tostring()),
            'mask': _bytes_feature(mask.tostring()),
            'scan_id': _bytes_feature(data['scan_id'].encode()),
            'slice_id': _bytes_feature(data['slice_id'].encode()),
            'classes': _bytes_feature(classes.tostring())
        }))
    return example
def pre_process(leaf_size=40,
                processed_data=r'./proc_data.tree',
                lookup=r'./id_star.lkp'):
    """
	Function to pre process the stars data into a kd tree and save the data

	Args:
		leaf_size: The number of points at which the algorithm switches over to brute-force. Has to be positive
		processed_data: The location where the processed data is to be stored as a pickle file
		lookup: The location where the data id to object lookup is stored

	Returns:
		No return
	"""
    utils.skip_line(1)
    stars = []
    idx = 0
    id_star_dct = {}
    for line in sys.stdin:
        star_name, x, y, z = utils.parse_line(line)
        stars.append([x, y, z])
        id_star_dct[idx] = star(x, y, z, star_name)
        idx += 1
    # construct a kd-tree
    tree = spatial.KDTree(stars, leafsize=leaf_size)
    utils.save_as_pickle(data=tree, pickle_file=processed_data)
    utils.save_as_pickle(data=id_star_dct, pickle_file=lookup)
Beispiel #5
0
    def _parse_file(self, storage_key, file_path, read_from_start=False, read_to_time=None):
        """
        Read recent part of the log file, update statistics storages and adjust seek.
        If only file parameter is supplied, read file from self.seek to the end.
        If the file is not found or cannot be read, log an error and return.

        @param str storage_key: a key to define statistics storage
        @param string file_path: path to file for parsing
        @param bool read_from_start: if true, read from the beginning of file, otherwise from `self.seek`
        @param datetime read_to_time: if set, records are parsed until their time is greater or equal of parameter value
        Otherwise the file is read till the end.
        """

        with open(file_path, 'r') as f:
            if read_from_start:
                logger.debug('Reading file %s from the beginning to %s'
                             % (file_path, read_to_time))
            else:
                logger.debug('Reading file %s from position %d to %s'
                             % (file_path, self.seek[file_path], read_to_time or 'the end'))

            if not read_from_start:
                f.seek(self.seek[file_path])
                logger.debug('Setting seek for file %s to %d based on a value from the storage'
                             % (f.name, self.seek[file_path]))

            log_parser = apachelog.parser(getattr(settings, 'ELF_FORMAT', ''))

            while True:
                current_seek = f.tell()
                line = f.readline()

                if not line:
                    #Reached end of file, record seek and stop
                    self.seek[file_path] = current_seek
                    logger.debug('Reached end of file %s, set seek in storage to %d' % (f.name, current_seek))
                    break

                record = utils.parse_line(line, log_parser, getattr(settings, 'LATENCY_IN_MILLISECONDS', False))

                if not record:
                    self._count_record(storage_key, 'error')
                    continue

                record_time = record.get_time()
                if record_time is None:
                    logger.error('Could not process time string: ' + record.time)
                    logger.error('Line: ' + record.line)
                    self._count_record(storage_key, 'error')
                    continue

                if read_to_time and record_time >= read_to_time:
                    #Reached a record with timestamp higher than end of current analysis period
                    #Stop here and leave it for the next invocation.
                    self.seek[file_path] = current_seek
                    logger.debug('Reached end of period, set seek for %s in storage to %d' % (f.name, current_seek))
                    break

                status = self._process_record(storage_key, record)
                self._count_record(storage_key, status)
Beispiel #6
0
def find_k_nearest(k, point_star):
    """
	Function to loop through each line in the stdin and keep track of the k
	closest points

	Args:
		k: the number of closest neighbors to find
		point_star: the star for which to find the k closest neighbors

	Returns:
		max_heap: a heap containing the k closest distances to the point_star
		star_dist: a key value pari of {dist-d1: [list of stars at dist-d1 to point_star]}
	"""
    max_heap = []
    star_dist = defaultdict(list)
    # skip header and sun row
    utils.skip_line(2)
    for line in sys.stdin:
        star_name, x, y, z = utils.parse_line(line=line)
        dist = utils.euclidean_dist(x=(x, y, z), y=point_star)
        new_star = star(x=x, y=y, z=z, star_name=star_name)
        if len(max_heap) < k:
            heappush(max_heap, -1 * dist)
            star_dist[dist].append(new_star)
        elif dist <= -1 * max_heap[0]:
            heappushpop(max_heap, -1 * dist)
            star_dist[dist].append(new_star)
    return max_heap, star_dist
Beispiel #7
0
def _read_record(f, log_parser):
    """
    Parse a single record from a log file
    @param FileIO f: file to seek
    @param parser log_parser: instance of a parser
    @return LogRecord parsed record
    """
    line = f.readline()
    if not line:
        return utils.END_OF_FILE
    return utils.parse_line(line, log_parser)
Beispiel #8
0
def main():
    # Graceful interruption handler.
    def quit(signum, frame):
        """ Handler to catch interrupting signals.
        :param signum: Number of a caught signal.
        :param frame: Interpreter stack frame (not used, added to comply with
            standard).
        :return: None
        :side-effects: Sets global exit event.
        """
        sys.stderr.write("\nInterrupted by %s, exiting.\n" % sig_names[signum])
        sys.stderr.flush()
        exit.set()

    exit = threading.Event()
    for s in sig_names.values():
        signal.signal(getattr(signal, s), quit)

    # Configuration manipulation.
    utils.update_config_from_cli_arguments(config, cliargs)
    collectors = utils.load(config, 'collectors', _collectors)
    display = utils.load(config,
                         'representer',
                         representers,
                         args=[collectors])[0]
    check_interval = config.getint('DEFAULT', 'check_interval')
    fname = config.get('DEFAULT', 'log_file')
    sys.exit(1) if utils.file_has_problems(fname) else just_continue

    # The processing itself.
    with open(fname, 'r') as f:
        f.seek(0, 2)  # Ignore historic data.
        display.show_stats()
        while True and not exit.is_set():
            start = datetime.datetime.now()
            data = list(
                filter(
                    None,
                    [utils.parse_line(x) for x in utils.get_fresh_events(f)]))
            for collector in collectors:
                for line in data:
                    collector.process_line(line)
            duration = (datetime.datetime.now() - start).total_seconds()
            to_wait = check_interval - duration
            if to_wait > 0:
                exit.wait(to_wait)
            else:
                sys.stderr.write("WARNING: too many events! Can not process"
                                 " them in %d second(s)!\n" % check_interval)
                sys.stderr.flush()
            display.tick()
Beispiel #9
0
 def do_add_user(self, line):
     '''
     This command adds a single user to the list of users
     USAGE: ad_user PersonE {(17, 42), [Running, Reading, Trekking]}
     '''
     try:
         name, x, y, interests, attributes = parse_line(line.strip())
     except Exception as e:
         print 'Unable to add user: ERROR: %s' % e
         return
     location = Location(x, y)
     user = User(name, location, interests, **attributes)
     cell = location.cell()
     if cell in self.users:
         self.users[cell].append(user)
     else:
         self.users[cell] = [user]
     self.users_by_name[name] = user
     print 'User added'
def create_lung_example(line):
    data = utils.parse_line(line.strip())

    input_image = create_input_image(args.input_dir, data['scan_id'],
                                     data['slice_id'], (256, 256), 3)

    # normalize the image
    input_image = input_image / 0.0625

    if args.structure and args.structure not in data['structures']:
        return None

    with open(os.path.join(args.input_dir, data['scan_id'], 'coordinates.txt'),
              'r') as f:
        r0, c0, r1, c1 = [int(x) for x in f.read().split(",")]

    radiomics = {
        k: v
        for k, v in data['structures'].items() if k == 'radiomics_gtv'
    }

    mask = utils.create_mask((512, 512), radiomics)
    mask = mask[r0:r1, c0:c1]
    height, width = 256, 256

    classes = np.array([0] + ['radiomics_gtv' in data['structures']],
                       dtype=np.int32)

    print("Writing: ", data['structures'].keys(), "Classes:", classes,
          "Image max:", np.max(input_image), "Mask max: ", np.max(mask))

    example = tf.train.Example(features=tf.train.Features(
        feature={
            'height': _int64_feature(height),
            'width': _int64_feature(width),
            'image': _bytes_feature(input_image.tostring()),
            'mask': _bytes_feature(mask.tostring()),
            'scan_id': _bytes_feature(data['scan_id'].encode()),
            'slice_id': _bytes_feature(data['slice_id'].encode()),
            'classes': _bytes_feature(classes.tostring())
        }))
    return example
Beispiel #11
0
 def read_in_all_images(self):
     """
     Reads in all the faces in the directory.
     :return: Returns a list that contains information about each image in the directory.
     """
     all_files = os.listdir(con.IMAGES_DIRECTORY)
     output_list = []
     for filename in all_files:
         extension = filename[-3:]
         if extension == "png":
             continue
         new_path = os.path.join(con.IMAGES_DIRECTORY, filename)
         # print(new_path)
         with open(new_path, "r") as f:
             mylist = f.readlines()
             mylist = [i.strip() for i in mylist if len(i.strip()) > 0]
         parsed_list = utils.parse_line(mylist)
         parsed_list.insert(0, filename)
         output_list.append(parsed_list)
     return output_list
Beispiel #12
0
#!/usr/bin/env python
import sys
import utils

if __name__ == "__main__":
    host = sys.argv[1]
    port = int(sys.argv[2])
    collection = sys.argv[3]
    chunk_size = 10000
    added = 0
    for lines in utils.grouper(sys.stdin, chunk_size):
        lines = [x for x in lines if x != None]
        objects = [utils.parse_line(line) for line in lines]
        utils.index_objects(objects, host, port, collection)
        added += chunk_size
        print >>sys.stderr, added
Beispiel #13
0
 def next_act(self):
     l = f[self.process_num].readline()
     act = utils.parse_line(l)
     utils.norm_act(act, 1, t_maxlen)
     return act
Beispiel #14
0
def test_ari(wsi_dir, testset_name, dictionary,
             estimate_sense_distribution_fct):
    wsi_ctx_file = os.path.join(wsi_dir, 'dataset.txt')
    wsi_gt_file = os.path.join(wsi_dir, 'key.txt')

    # logging.info(' Loading ground truth')

    ground_truth = {}

    with open(wsi_gt_file) as f:
        for l in f:
            splitted = l.split()
            word_with_pos = splitted[0]
            if word_with_pos not in ground_truth:
                ground_truth[word_with_pos] = {}
            word_with_pos_with_ctx_id = splitted[1].split('.')
            assert word_with_pos == '.'.join(
                [word_with_pos_with_ctx_id[0], word_with_pos_with_ctx_id[1]])
            ctx_id = int(word_with_pos_with_ctx_id[2])
            word_with_pos_with_sense_id = splitted[2].split('.')
            assert word_with_pos.split('.')[0] == word_with_pos_with_sense_id[
                0]  # There is no PoS in WWSI
            if '&&' in word_with_pos_with_sense_id[
                    2]:  # TODO: handle it better; this occurs in SemEval 2010
                continue
            sense_id = int(word_with_pos_with_sense_id[2])
            ground_truth[word_with_pos][ctx_id] = sense_id

    if __debug__:
        sense_numbers = []

        # logging.info('Total ctx number: {}. Unique word with POS number: {}'.format(sum([len(ctx_dict) for ctx_dict in ground_truth.itervalues()]), len(ground_truth)))

        for w in ground_truth.itervalues():
            sense_numbers.append(len(set(w.itervalues())))

        # logging.info('Sense numbers. Min: {}, max: {}, median: {} and mean: {}.'.format(min(sense_numbers), max(sense_numbers), np.median(sense_numbers), np.mean(sense_numbers)))

    # logging.info('Predicting')

    ctx_number = 0
    ctx_expected_number = 0
    current_word_with_pos = None
    aris = []

    # unknown_word_id = dictionary[UNKNOWN_WORD]
    # reverse_dictionary = dict(zip(dictionary.values(), dictionary.keys()))

    disambiguation_examples = {}
    disambiguation_examples_words = (
    )  # ('mouse', 'goal', 'heart', 'wave', 'root', 'coup', 'java', 'ruby', 'time', 'train', 'mass', 'bus', 'foot', 'mini', 'lion', 'mirror', 'burn', 'monopoly', 'apache')

    #    if testset_name == 'WWSI':
    #        wwsi_raw = read_raw_wwsi(wsi_dir)

    with open(wsi_ctx_file, 'r') as f:
        for l in f:
            splitted = l.split()
            if len(splitted) == 2 and '\t' not in l:
                assert ctx_number == ctx_expected_number

                if current_word_with_pos:
                    aris.append(
                        adjusted_rand_score(predicted_senses, expected_senses))

                predicted_senses = []
                expected_senses = []

                current_word_with_pos = splitted[0]
                current_word = current_word_with_pos.split('.')[0]
                if current_word not in dictionary:
                    current_word_with_pos = None
                    continue
                ctx_expected_number = int(splitted[1])
                ctx_number = 0
            elif current_word_with_pos:
                ctx_number += 1

                sense_dict = ground_truth[current_word_with_pos]
                if ctx_number not in sense_dict:
                    continue  # due to skipping '&&' in SemEval2010

                tab_splitted = l.split('\t')
                assert len(tab_splitted) == 2

                ctx = (parse_line(tab_splitted[0], dictionary, 'wsi'),
                       parse_line(tab_splitted[1], dictionary, 'wsi'))

                if ctx != ([], []):  # this is possible in case of SemEval 2013
                    expected_senses.append(sense_dict[ctx_number])

                    sense_distribution = estimate_sense_distribution_fct(
                        w=dictionary[current_word], c=ctx)

                    # logging.info('word: {}, ctx size: {}, first word: {}, unknown number: {}, expected sense: {}, predicted sense: {}'.format(current_word, len(ctx[0]) + len(ctx[1]), reverse_dictionary[list(chain.from_iterable(ctx))[0]], len([t for t in chain.from_iterable(ctx) if t == unknown_word_id]), expected_senses[-1], predicted_senses[-1]))
                    predicted_senses.append(np.argmax(sense_distribution) + 1)

                    if testset_name == 'WWSI' and current_word in disambiguation_examples_words:
                        if current_word not in disambiguation_examples:
                            disambiguation_examples[current_word] = []
                        disambiguation_examples[current_word].append(
                            (wwsi_raw[current_word][ctx_number - 1],
                             expected_senses[-1], sense_distribution))

    logging.info('  Averaged ARI for {:12}: {:.3}'.format(
        testset_name, np.mean(aris)))

    #    logging.info('Disambiguation_examples:')

    for w in disambiguation_examples:
        logging.info('  Word {}:'.format(w))
        for sense_id in xrange(len(disambiguation_examples[w][0][2])):
            logging.info('    Sense {}:'.format(sense_id))
            top_examples = sorted(disambiguation_examples[w],
                                  key=lambda ex: ex[2][sense_id],
                                  reverse=True)[:4]
            for ex in top_examples:
                logging.info('      {:.3} {} {}'.format(
                    ex[2][sense_id], ex[1], ex[0]))
Beispiel #15
0
# combinations('ABCD', 2)                     AB AC AD BC BD CD
# combinations_with_replacement('ABCD', 2)    AA AB AC AD BB BC BD CC CD DD

tot = 0
res = []
board = {}
table = new_table(None, width=2, height=4)

# Uncomment for multi-group style inputs. :c
# data = ''.join([line for line in fileinput.input()])
# groups = [g.split('\n') for g in data.split('\n\n')]

for y, line in enumerate(fileinput.input()):
    line = line.strip()
    nums = parse_nums(line)
    data = parse_line(r'', line)
    nums = [int(x) for x in line.split(',')]

    for x, c in enumerate(line):
        board[Point(x, y)] = c

    if y == 0:
        print(data)

print nums

seen = defaultdict(list)

for i, n in enumerate(nums):
    seen[n].append(i)
Beispiel #16
0
def create_indexes():
    db = connect_db()
    cursor = db.cursor()
    data = get_all_data_from_db(cursor)

    norm_redis = connect_word_to_norm_word()
    id_item_redis = connect_id_to_item()
    word_ids_redis = connect_word_to_bag_ids()
    word_ids_redis_quick = connect_word_to_bag_ids_quick()
    idBag_length_redis = connect_bag_id_to_length()

    idBag_bag = connect_bag_id_to_bag()

    for rec in data:
        item_id = rec[0]
        name = rec[1]
        print str(item_id)  + " " + name
        id_item_redis.set(item_id, name)

        for i in range(1, 4):
            #print rec[i]
            cur_bag_of_words =  filter_bag_of_words(normalize_bag_of_words_with_index(parse_line(rec[i]), norm_redis), STOP_LIST)

            idBag_length_redis.set((item_id - 1)*3 + i - 1, len(cur_bag_of_words))
            idBag_bag.set((item_id - 1)*3 + i - 1, rec[i])

            for word in cur_bag_of_words:
                word_ids_redis.sadd(word, (item_id - 1)*3 + i - 1)
                word_ids_redis_quick.zadd(word, 1, (item_id - 1)*3 + i - 1)

    disconnect_db(db)
Beispiel #17
0
 def test_second(self):
     fish_dates = utils.parse_line("test_input.txt", ",", int)
     self.assertEqual(day06.count_fish(fish_dates, 256), 26984457539)
Beispiel #18
0
        raise Exception('modular inverse does not exist')
    else:
        return x % m


def chinese_remainder_theorem(discs):
    M = mul(d[0] for d in discs)
    x = 0

    for i, (size, initial) in enumerate(discs, start=1):
        # Disc #2 has 17 positions; at time=0, it is at position 15.
        # => x \equiv (17 - 15 - 2) (mod 17)
        M_i = (M / size)
        x += (size - initial - i) * M_i * modinv(M_i, size)

    return x


DISCS = []
DISC_RE = re.compile(
    r'Disc #\d+ has (\d+) positions; at time=0, it is at position (\d+).')

for line in fileinput.input():
    disc = parse_line(DISC_RE, line.strip())
    DISCS.append(disc)

# print "Timing to press button:", button_timing(DISCS)
# print "Timing with added disc:", button_timing(DISCS + [[11, 0]])
print "Timing to press button:", chinese_remainder_theorem(DISCS)
print "Timing with added disc:", chinese_remainder_theorem(DISCS + [[11, 0]])
Beispiel #19
0
import utils


def calc_fuel(start_position: int,
              end_position: int,
              const: bool = False) -> int:
    const_burn = abs(start_position - end_position)
    return const_burn if not const else ((const_burn**2 + const_burn) // 2)


def find_min_fuel(start_positions: list[int], const: bool = False) -> int:
    return min(
        sum(
            calc_fuel(start_position, end_position, const)
            for start_position in start_positions)
        for end_position in range(max(start_positions)))


if __name__ == "__main__":
    in_fpath = input("Enter file containing starting positions: ")
    in_data = utils.parse_line(in_fpath, ",", int)

    print(f"Minimum fuel required (constant burn): {find_min_fuel(in_data)}")

    print(
        f"Minimum fuel required (non-constant burn): {find_min_fuel(in_data, True)}"
    )
Beispiel #20
0
import fileinput
from utils import parse_line

part_1 = 0
part_2 = 0

for line in fileinput.input():
    start, end, letter, pwd = parse_line(r'(\d+)-(\d+) (\w+): (\w+)', line)

    if start <= pwd.count(letter) <= end:
        part_1 += 1

    if (pwd[start - 1] == letter) ^ (pwd[end - 1] == letter):
        part_2 += 1

print "Part 1:", part_1
print "Part 2:", part_2
Beispiel #21
0
 def test_first(self):
     start_positions = utils.parse_line("test_input.txt", ",", int)
     self.assertEqual(day07.find_min_fuel(start_positions), 37)
Beispiel #22
0
 def test_second(self):
     start_positions = utils.parse_line("test_input.txt", ",", int)
     self.assertEqual(day07.find_min_fuel(start_positions, True), 168)
def create_seed_example(line):
    data = utils.parse_line(line.strip())

    input_image = create_input_image(args.input_dir, data['scan_id'],
                                     data['slice_id'], (512, 512), 1)

    # normalize the image
    input_image = input_image / 0.0625

    if args.structure and args.structure not in data['structures']:
        return None

    radiomics = {
        k: v
        for k, v in data['structures'].items() if k == 'radiomics_gtv'
    }

    examples = []

    for seed in data['seeds']:
        mask = utils.create_mask((512, 512), radiomics)
        height, width = 224, 224
        r0 = max(0, seed[1] - height // 2)
        c0 = max(0, seed[0] - width // 2)
        r1 = seed[1] + height // 2
        c1 = seed[0] + width // 2

        mask = mask[r0:r1, c0:c1]
        image = input_image[r0:r1, c0:c1, :]

        pad_h = (height - image.shape[0]) / 2
        pad_w = (width - image.shape[1]) / 2

        print("Point", seed, "Image", image.shape, "Mask", mask.shape, "Pad",
              pad_h, pad_w)

        if pad_h or pad_w:
            mask = np.pad(mask, ((int(np.ceil(pad_h)), int(np.floor(pad_h))),
                                 (int(np.ceil(pad_w)), int(np.floor(pad_w)))),
                          mode='constant')

            image = np.pad(image, ((int(np.ceil(pad_h)), int(np.floor(pad_h))),
                                   (int(np.ceil(pad_w)), int(np.floor(pad_w))),
                                   (0, 0)),
                           mode='constant')

        assert (image.shape[0] == height and image.shape[1] == width)
        assert (mask.shape[0] == height and mask.shape[1] == width)

        classes = np.array([0] + ['radiomics_gtv' in data['structures']],
                           dtype=np.int32)

        print("Writing: ", data['structures'].keys(), "Classes:", classes,
              "Image max:", np.max(image), "Mask max: ", np.max(mask))

        example = tf.train.Example(features=tf.train.Features(
            feature={
                'height': _int64_feature(height),
                'width': _int64_feature(width),
                'image': _bytes_feature(image.tostring()),
                'mask': _bytes_feature(mask.tostring()),
                'scan_id': _bytes_feature(data['scan_id'].encode()),
                'slice_id': _bytes_feature(data['slice_id'].encode()),
                'classes': _bytes_feature(classes.tostring())
            }))

        examples.append(example)
    return examples
Beispiel #24
0
import fileinput
from collections import defaultdict

from utils import parse_line

PROG = {}

# Parse puzzle input
for i, line in enumerate(fileinput.input()):
    if i == 0:
        STATE = parse_line(r'Begin in state (\w+).', line)[0]
    elif i == 1:
        STEPS = int(
            parse_line(r'Perform a diagnostic checksum after (\d+) steps.',
                       line)[0])

    if i <= 2:
        continue

    if i % 10 == 3:
        start = parse_line(r'In state (\w+):', line)[0]
    elif i % 10 == 5:
        write_0 = parse_line(r'    - Write the value (\d+).', line)[0]
    elif i % 10 == 6:
        move_0 = parse_line(r'    - Move one slot to the (\w+).', line)[0]
        move_0 = 1 if move_0 == 'right' else -1
    elif i % 10 == 7:
        next_0 = parse_line(r'    - Continue with state (\w+).', line)[0]
    elif i % 10 == 9:
        write_1 = parse_line(r'    - Write the value (\d+).', line)[0]
    elif i % 10 == 0:
Beispiel #25
0
# import sys
# import time
import fileinput

from utils import parse_line

WIDTH = 50
HEIGHT = 6
SCREEN = [[False for _ in range(WIDTH)] for _ in range(HEIGHT)]

# Make space for animated output
# print '\n' * HEIGHT

for line in fileinput.input():
    if line.startswith('rect'):
        a, b = parse_line(r'rect (\d+)x(\d+)', line)

        for y in range(b):
            for x in range(a):
                SCREEN[y][x] = True

    else:
        rc, n, offset = parse_line(r'rotate (\w+) .=(\d+) by (\d+)', line)

        if rc == 'row':
            temp = SCREEN[n][:]
            for i, x in enumerate(temp):
                SCREEN[n][(offset + i) % WIDTH] = x

        else:
            temp = [row[n] for row in SCREEN]
Beispiel #26
0
    fish_count = defaultdict(int)
    for dates in init_dates:
        fish_count[dates] += 1

    # Determine number of fish created after num_days
    for _ in range(num_days):
        new_fish_count = defaultdict(int)
        for days_left, fish_count in fish_count.items():
            if days_left == 0:
                # Reset timer and add same number of new fish
                new_fish_count[6] += fish_count
                new_fish_count[8] += fish_count
            else:
                # Minus 1 day for fish not due
                new_fish_count[days_left - 1] += fish_count

        # Update tracker
        fish_count = new_fish_count

    return sum(fish_count.values())


if __name__ == "__main__":
    input_data = input("Enter path containing fish due dates: ")
    fish_dates = utils.parse_line(input_data, ",", int)

    print(f"Number of laternfish after 80 days: {count_fish(fish_dates, 80)}")

    print(
        f"Number of laternfish after 256 days: {count_fish(fish_dates, 256)}")
Beispiel #27
0
from utils import parse_line


def decrypt(c, n):
    if c == '-':
        return ' '

    return chr((((ord(c) - ord('a')) + n) % 26) + ord('a'))


total_sector_id = 0
north_pole_sector_id = None

for line in fileinput.input():
    name, sector, checksum = parse_line(r'(\S+)-(\d+)\[(\w{5})\]', line)
    sector = int(sector)

    real_name = ''.join(decrypt(c, sector) for c in name)

    if 'northpole' in real_name:
        north_pole_sector_id = sector

    occurences = Counter([x for x in name if x.isalpha()])
    commons = sorted(occurences.most_common(), key=lambda (x, y): (-y, x))

    if ''.join(zip(*commons)[0][:5]) == checksum:
        total_sector_id += sector

print "Sum of sector IDs of real rooms: %i" % total_sector_id
print "North Pole storage room sector: %i" % north_pole_sector_id
Beispiel #28
0
import re
import fileinput
from collections import Counter

from utils import parse_line

formulas = {}
from_ore = set()

for i, line in enumerate(fileinput.input()):
    inp, out = line.split(' => ')
    re_form = r'(\d+) (.+)'

    inputs = [parse_line(re_form, i) for i in inp.split(', ')]
    out = parse_line(re_form, out)
    formulas[out[1]] = (out[0], inputs)

    if inputs[0][1] == 'ORE':
        from_ore.add(out[1])


def create_fuel(n):
    excesses = Counter()

    def needed(n, type):
        ret = Counter()

        if type in from_ore:
            ret[type] += n
            return ret
Beispiel #29
0
 def test_first(self):
     fish_dates = utils.parse_line("test_input.txt", ",", int)
     self.assertEqual(day06.count_fish(fish_dates, 18), 26)
     self.assertEqual(day06.count_fish(fish_dates, 80), 5934)
Beispiel #30
0
import fileinput
from collections import defaultdict
from utils import parse_line

GOAL = 'shiny gold'

graph = defaultdict(set)

for i, line in enumerate(fileinput.input()):
    line = line.strip()
    color, rest = parse_line(r'(\w+ \w+) bags contain (.+)+', line)
    rest = rest.replace('.', '').split(',')

    for r in rest:
        if r != 'no other bags':
            parts = r.split()
            new_color = ' '.join(parts[1:3])
            graph[color].add((new_color, int(parts[0])))


def dfs_1(node):
    if node == GOAL:
        return True

    return any(dfs_1(n) for n, _ in graph[node])


def dfs_2(node, count=1):
    tot = 1

    for nxt, amt in graph[node]:
Beispiel #31
0
import fileinput
from collections import Counter, defaultdict

from utils import parse_line

SQUARES = Counter()
CLAIMS = defaultdict(list)

for i, line in enumerate(fileinput.input()):
    _id, x, y, w, h = parse_line(r'#(\d+) @ (\d+),(\d+): (\d+)x(\d+)', line)

    for i in range(x, x + w):
        for j in range(y, y + h):
            SQUARES[(i, j)] += 1
            CLAIMS[_id].append((i, j))

print "Square inches of fabric within multiple claims:", sum(n > 1 for c, n in SQUARES.items())

for _id in CLAIMS:
    for pos in CLAIMS[_id]:
        if SQUARES[pos] != 1:
            break
    else:
        print "ID of only non-overlapping claim:", _id
Beispiel #32
0
        print ''.join(
            grid.get(Point(x, y), '.') for x in range(min_x - 1, max_x + 2))

    print


grid = {}

min_y = 1e10
max_y = -1e10

min_x = 1e10
max_x = -1e10

for i, line in enumerate(fileinput.input()):
    a, x, b, y, z = parse_line(r'(.)=(\d+), (.)=(\d+)..(\d+)', line)

    for i in range(y, z + 1):
        if a == 'x':
            grid[Point(x, i)] = '#'
            min_y = min(min_y, i)
            max_y = max(max_y, i)
            min_x = min(min_x, x)
            max_x = max(max_x, x)
        else:
            grid[Point(i, x)] = '#'
            min_y = min(min_y, x)
            max_y = max(max_y, x)
            min_x = min(min_x, i)
            max_x = max(max_x, i)
Beispiel #33
0
def read_log_file(log_file):

    # initialize data structures
    logger.info('Initializing data structures')
    hosts_count = {} # data struct for feature1, dict containing top hosts
    resources_count = {} # data struct for feature 2, dict containing top resources
    hours_queue = [] # data struct for feature 3, maintains 1 hour worth of log entries
    hours_count = {} # data struct for feature 3, dict containing busiest 10 hours

    blocked_list = [] # data struct for feature 4, list containing all blocked login attempts
    blocked_attempts = BlockedAttempts()

    # iterate through log file and update data structures
    with open(str(log_file)) as file:
        for line in file:
            # parse line
            line = str(line)
            data = parse_line(line)

            logger.info('Updating data structures')
            # update data structures
            # feature 1
            hosts_count = update_feature1(data, hosts_count)

            # feature 2
            resources_count = update_feature2(data, resources_count)

            # feature 3
            hours_queue, hours_count = update_feature3(data, hours_queue, hours_count)

            # feature 4
            blocked_attempts, blocked_list = update_feature4(line, data, blocked_attempts, blocked_list)

    logger.info('Begin writing to output files')
    # write to output files
    # feature 1: save top 10 hosts to hosts.txt
    logger.info('Begin writing to hosts.txt')
    with open(str(hosts_file), 'w') as file:
        host_number = 0
        for host, value in sorted(hosts_count.items(), key=lambda kv: kv[1], reverse=True):
            if host_number < 10:
                file.write(host + ',' + str(value) + '\n')
            else:
                break
            host_number += 1
    logger.info('Completed writing to hosts.txt')

    logger.info('Begin writing to resources.txt')
    # feature 2: save top 10 resources to resources.txt, sort by value in descending order, then by key ascending value
    with open(str(resources_file), 'w') as file:
        resource_number = 0

        for resource, value in sorted(resources_count.items(), key=lambda(k, v): (-v, k)):
            if resource_number < 10:
                file.write(resource + '\n')
            else:
                break
            resource_number += 1
    logger.info('Completed writing to resources.txt')

    logger.info('Begin writing to hours.txt')
    # feature 3: save top 10 hours to hours.txt
    with open(str(hours_file), 'w') as file:
        hour_number = 0
        for hour, value in sorted(hours_count.items(), key=lambda kv: kv[1], reverse=True):
            if hour_number < 10:
                file.write(hour + ',' + str(value) + '\n')
            else:
                break
                hour_number += 1
    logger.info('Completed writing to hours.txt')

    logger.info('Begin writing to blocked.txt')
    # feature 4: save all blocked login attempts to blocked.txt
    with open(str(blocked_file), 'w') as file:
        for row in blocked_list:
            file.write(row)
    logger.info('Begin writing to blocked.txt')
Beispiel #34
0

def allowed_ips(blacklist, max_ip=MAX_IP):
    """Generator over all allowed IPs given a blacklist."""
    i = 0

    for start, end in sorted(blacklist):
        if i < start:
            for n in range(i, start):
                yield n

        if i <= end:
            i = end + 1

    if i <= max_ip:
        for n in range(i, max_ip + 1):
            yield n


if __name__ == '__main__':
    blacklist = []

    for line in fileinput.input():
        start, end = parse_line(r'(\d+)-(\d+)', line)
        blacklist.append((start, end))

    whitelist = list(allowed_ips(blacklist))

    print "Lowest-valued whitelisted IP:", whitelist[0]
    print "Number of allowed addresses:", len(whitelist)