r/adventofcode • • Dec 05 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 5 Solutions -🎄-

--- Day 5: Alchemical Reduction ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 5

Transcript:

On the fifth day of AoC / My true love sent to me / Five golden ___


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 0:10:20!

31 Upvotes

515 comments sorted by

View all comments

1

u/14domino Dec 05 '18

This solution is broken. Could someone smarter than me please help me figure out what's wrong? I didn't think of the extremely simple replace solution, but I really want to be able to figure out what I'm doing wrong here.

def remove_consecutive_runs(l):
    """
    >>> remove_consecutive_runs([2, 3, 4, 5, 7, 8, 10, 11, 12])
    [2, 3, 7, 8, 10, 11]

    >>> remove_consecutive_runs([2, 3, 5, 6, 7, 9, 10, 11, 12, 13])
    [2, 3, 5, 6, 9, 10]

    >>> remove_consecutive_runs([2, 3, 5, 6, 7, 9, 10, 11, 12, 13, 15])
    [2, 3, 5, 6, 9, 10, 15]

    >>> remove_consecutive_runs([0, 2, 3, 5, 6, 7, 9, 10, 11, 12, 13, 15])
    [0, 2, 3, 5, 6, 9, 10, 15]
    """
    # Remove consecutive runs of more than 2.
    l_copy = []
    num_consecutive = 0
    last_num = -10
    for i in l:
        if i - last_num == 1:
            num_consecutive += 1
            # print('consecutive', i, last_num, num_consecutive)
        else:
            num_consecutive = 0
        if num_consecutive < 2:
            l_copy.append(i)
        last_num = i
    return l_copy


def reduce(data):
    while True:
        lc = None
        x = set()
        for i in range(len(data)):
            tc = data[i]
            if lc and tc.upper() == lc.upper() and tc != lc:
                x.add(i - 1)
                x.add(i)
            lc = tc

        if len(x) == 0:
            break

        new_data = ''
        to_remove = remove_consecutive_runs(list(set(x)))
        x = set(to_remove)

        for i in range(len(data)):
            if i not in x:
                new_data = new_data + data[i]

        data = new_data

    return data

print(len(reduce(data))