Пример #1
0
def run():
    answer = None
    start_time = util.now()

    with open(os.path.join(INPUT_DIR, 'input02.dat'), 'r') as f:
        all_lines = [l.strip() for l in f.readlines()]

    for position in reversed(range(len(all_lines[0]))):
        have_seen = {}

        for word in all_lines:
            letters = list(word)
            del letters[position]
            minus_one = ''.join(letters)

            if minus_one in have_seen:
                answer = minus_one, word, have_seen[minus_one], position + 1
                break

            have_seen[minus_one] = word

        if answer:
            break

    print('answer is: %s (from %s and %s by masking position %d)' % answer)
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #2
0
def run():
    grid, answer = [], 0
    start_time = util.now()

    with open(os.path.join(INPUT_DIR, 'input03.dat'), 'r') as f:
        all_squares = [_define_square(l) for l in f.readlines() if l.strip()]

    max_x = max([s.x + s.width for s in all_squares])
    max_y = max([s.y + s.height for s in all_squares])

    for i in range(max_y + 1):
        grid.append([[] for j in range(max_x + 1)])

    for square in all_squares:
        for i in range(square.y, square.y + square.height):
            for j in range(square.x, square.x + square.width):
                grid[i][j].append(square.id)

    for row in grid:
        for column in row:
            if len(column) > 1:
                answer += 1

    print('answer is: %s' % str(answer))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #3
0
def run():
    starting_frequency, start_time, answer = 0, util.now(), 0
    
    with open(os.path.join(INPUT_DIR, 'input01.dat'), 'r') as f:
        answer = starting_frequency + sum([int(v) for v in f.readlines()])

    print('answer is: %s' % str(answer))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #4
0
def run():
    start_time, answer = util.now(), 0
    puzzle_input = 289326

    grid = Grid()
    x, y = grid.find_position(puzzle_input)
    answer = abs(x) + abs(y)

    print('answer is: %s' % str(answer))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #5
0
def run():
    start_time, answer = util.now(), 0
    input_file = join(dirname(abspath(__file__)), 'inputs', 'input02.dat')

    with open(input_file, 'r') as f:
        for row in f:
            for x, y in evenly_divisible([int(d) for d in row.split()]):
                answer += int(x / y)

    print('answer is: %s' % str(answer))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #6
0
def run():
    start_time = util.now()

    with open(INPUT, 'r') as f:
        digits = f.read().strip()

    wrapped = digits + digits[0]
    answer = sum([int(d) for i, d in enumerate(digits) if d == wrapped[i+1]])

    print('answer is: %s' % str(answer))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #7
0
def run():
    start_time, answer = util.now(), 0

    with open(os.path.join(INPUT_DIR, 'input01.dat'), 'r') as f:
        for row in f.readlines():
            fuel = fuel_needed(int(row))

            while fuel > 0:
                answer += fuel
                fuel = fuel_needed(fuel)

    print('answer is: %s' % str(answer))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #8
0
def run():
    start_time, answer = util.now(), 0
    input_file = join(dirname(abspath(__file__)), 'inputs', 'input02.dat')

    def checksum(row_data):
        return max(row_data) - min(row_data) if any(row_data) else 0

    with open(input_file, 'r') as f:
        for row in f:
            answer += checksum([int(d) for d in row.split()])

    print('answer is: %s' % str(answer))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #9
0
def run():
    start_time, answer = util.now(), None

    with open(os.path.join(INPUT_DIR, 'input01.txt'), 'r') as f:
        values = sorted([int(v) for v in f.readlines()])

    for x in values:
        for y in reversed([n for n in values if n >= x]):
            if x + y == 2020:
                answer = (x, y, x * y)

        if answer:
            break

    print('answer is: %s' % str(answer))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #10
0
def run():
    start_time, answer = util.now(), None

    with open(os.path.join(INPUT_DIR, 'input02.txt'), 'r') as f:
        lines = f.readlines()

    def _is_valid(entry):
        parts = [p for p in SPLITS_PATTERN.split(entry.strip())]
        index_a, index_b, required, given = int(parts[0]) - 1, int(
            parts[1]) - 1, parts[2], parts[3]
        matches = [i for i in (index_a, index_b) if given[i] == required]
        return bool(len(matches) == 1)

    valid_entries = [e for e in lines if _is_valid(e)]
    answer = len(valid_entries)

    print('answer is: %s' % str(answer))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #11
0
def run():
    current_frequency, answer, tried, already_seen = 0, 0, 0, set()
    start_time = util.now()
    
    with open(os.path.join(INPUT_DIR, 'input01.dat'), 'r') as f:
        all_values = [int(v) for v in f.readlines()]
       
    for value in repeating_iterator(all_values):
        current_frequency += int(value)
        tried += 1

        if current_frequency in already_seen:
            answer = current_frequency
            break
        
        already_seen.add(current_frequency)

    print('answer is: %s (found after %d changes)' % (str(answer), tried))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #12
0
def run():
    start_time, answer = util.now(), None

    with open(os.path.join(INPUT_DIR, 'input01.txt'), 'r') as f:
        values = sorted([int(v) for v in f.readlines()])

    def _get_answer(values):
        values_length = len(values)

        for i in range(0, values_length):
            for j in range(i + 1, values_length):
                for k in range(i + 2, values_length):
                    x, y, z = values[i], values[j], values[k]
                    if x + y + z == 2020:
                        return (x, y, z, x * y * z)

        return None

    print('answer is: %s' % str(_get_answer(values)))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #13
0
def run():
    start_time, answer = util.now(), 0
    grid, x = None, 0

    with open(os.path.join(INPUT_DIR, 'input03.txt'), 'r') as f:
        grid = [l.strip() for l in f.readlines()]

    repeating_width = len(grid[0])

    def _is_tree(x, y):
        """Uses modulus math to extend the columns."""
        modulated_x = x % repeating_width
        return bool(grid[y][modulated_x] == TREE_SYMBOL)

    for y in range(len(grid)):
        if _is_tree(x, y):
            answer += 1
        x += 3

    print('answer is: %s' % str(answer))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #14
0
def run():
    has_exactly = {2: 0, 3: 0}
    start_time = util.now()

    with open(os.path.join(INPUT_DIR, 'input02.dat'), 'r') as f:
        for line in f.readlines():
            entry = line.strip()
            histogram = {c: 0 for c in entry}

            for letter in entry:
                histogram[letter] += 1

            for looking_for in (2, 3):
                has_exactly[looking_for] += 1 if bool(
                    [k for k, v in histogram.items()
                     if v == looking_for]) else 0

    answer = has_exactly[2] * has_exactly[3]

    print('answer is: %s' % str(answer))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #15
0
def run():
    answer, done = {'a': {}, 'b': {}}, False
    start_time = util.now()

    with open(os.path.join(INPUT_DIR, 'input05.dat'), 'r') as f:
        initial_material = f.read().strip()

    # part A

    reduced_material = _reduce(initial_material)
    answer['a'].update({
        'length': len(reduced_material),
        'material': ''.join(reduced_material)
    })
    print('Part One answer is: %d' % answer['a']['length'])

    # part B

    attempts = []
    for letter in string.ascii_lowercase:
        print('reducing less %s' % letter)
        stripped_and_reduced = _reduce(
            _strip_unit_type(letter, initial_material))
        attempts.append({
            'unit_type': letter,
            'size': len(stripped_and_reduced)
        })

    ranked = sorted(attempts, key=lambda d: d['size'])
    answer['b'].update(ranked[0])

    print('Part Two answer is: %d' % answer['b']['size'])

    # summary

    print('whole answer dict is: %s' % str(answer))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))
Пример #16
0
def run():
    unsorted_lines, answer = {}, {'a': {}, 'b': {}}
    start_time = util.now()

    with open(os.path.join(INPUT_DIR, 'input04.dat'), 'r') as f:
        for line in f.readlines():
            entry_time, entry = REGEX['event'].search(line).groups()
            unsorted_lines[entry_time] = entry

    observations, guard, nap_start, total_sleepage = OrderedDict(), None, 0, {}

    for key in sorted(unsorted_lines.keys()):
        entry = unsorted_lines[key]
        day, hour, minute = re.split('[\s:]', key)

        is_new_series = REGEX['guard'].search(entry)
        if is_new_series:
            guard = is_new_series.group(1)

        elif REGEX['sleep'].search(entry):
            nap_start = int(minute)

        elif REGEX['wake'].search(entry):
            if day not in observations:
                observations[day] = {
                    'guard': guard,
                    'is_asleep': [False for i in range(60)]
                }
            for i in range(nap_start, int(minute)):
                observations[day]['is_asleep'][i] = True

    # part A

    for entry in observations.values():
        id = entry['guard']
        total_sleepage[id] = total_sleepage.get(id, 0) + len(
            [y for y in entry['is_asleep'] if y])

    for k, v in total_sleepage.items():
        if v > answer['a'].get('sleepage', 0):
            answer['a'] = {'guard': k, 'sleepage': v}

    histograms = {}

    for observed in observations.values():
        id = observed['guard']
        if id not in histograms:
            histograms[id] = [0 for i in range(60)]
        for minute in [i for i, v in enumerate(observed['is_asleep']) if v]:
            histograms[id][minute] += 1

    for i, v in enumerate(histograms[answer['a']['guard']]):
        if v > answer['a'].get('max_per_minute', 0):
            answer['a'].update({'worst_minute': i, 'max_per_minute': v})

    print('Part One answer is: %d' %
          (int(answer['a']['guard']) * answer['a']['worst_minute']))

    # part B

    def _most_minute(guard_id, all_histograms):
        """Find the minute a guard naps most, and how many times."""
        ranked = sorted([{
            'minute': i,
            'count': v
        } for i, v in enumerate(all_histograms[guard_id])],
                        key=lambda d: d['count'])
        return ranked[-1]

    ranked_likelies = sorted([{
        'guard': k,
        'most': _most_minute(k, histograms)
    } for k in histograms.keys()],
                             key=lambda e: e['most']['count'])
    answer['b'].update(ranked_likelies[-1])

    print('Part Two answer is: %d' %
          (int(answer['b']['guard']) * answer['b']['most']['minute']))

    print('whole answer dict is: %s' % str(answer))
    print('  elapsed: %f seconds' % util.elapsed_since(start_time))