THIS_DIR = os.path.dirname(os.path.abspath(__file__)) EXEC_FUNC = 'exec_day' if __name__ == "__main__": parser = argparse.ArgumentParser(description='Execute Code Advent Days') parser.add_argument('-s', '--sample', action='store_true', help='If flag exists, days will be executed' 'with sample data and not the actual input.') parser.add_argument('days', nargs='+', help='Days to execute') args = parser.parse_args() days_re = os.path.join(THIS_DIR, 'day*.py') day_modules = glob(days_re) existing_days = [determine_day(x) for x in day_modules] mapped_days = dict(zip(existing_days, day_modules)) for day in args.days: if day not in mapped_days: print(">>> SKIPPING UNAVAILABLE DAY: {0} <<<\n".format(day)) continue spec = util.spec_from_file_location("day{0}".format(day), mapped_days[day]) mod = util.module_from_spec(spec) spec.loader.exec_module(mod) to_exec = getattr(mod, EXEC_FUNC) print("################ DAY {0} ################".format(day)) to_exec(sample=args.sample)
from commonlib import determine_day, read_input_data DAY = determine_day(__file__) SAMPLE_DATA = ['abcde', 'fghij', 'klmno', 'pqrst', 'fguij', 'axcye', 'wvxyz'] def get_counts(in_data, checksum_only=True): counts = {2: 0, 3: 0} for line in in_data: for i in (2, 3): if [x for x in line if line.count(x) == i]: counts[i] += 1 if checksum_only: return counts[2] * counts[3] return counts def find_similar_ids(in_data, common_only=True): def is_diff_by_one(str1, str2): results = [] for i in range(len(str1)): results.append(str1[i] == str2[i]) return results.count(False) == 1, results for indx, s1 in enumerate(in_data[:-1], 1): for s2 in in_data[indx:]: is_diff, diff_res = is_diff_by_one(s1, s2) if is_diff and common_only: return [s1[i] for i in range(len(s1)) if diff_res[i] is True] elif is_diff and not common_only: return s1, s2