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/Cyphase Dec 05 '18 edited Dec 05 '18

EDIT: My heavily optimized version is in a child comment. It runs in ~1.75ms for my AoC 50K input.

This is my Python solution that took waay to long to write for how simple it is. :/

def part1(data):  # 0.429s, best out of three
    i = 0
    d = list(data)

    while i + 1 < len(d):
        if d[i].lower() == d[i + 1].lower():
            if d[i] != d[i + 1]:  # match
                d.pop(i)
                d.pop(i)
                i -= 1
                continue
        i += 1

    return len(d)


def part2(data):  # 6.923s, best out of three
    return min(
        part1(list(x for x in data if x.lower() != c))
        for c in "abcdefghijklmnopqrstuvwxyz"
    )

4

u/Cyphase Dec 05 '18 edited Dec 05 '18

Here's my heavily optimized version of essentially the same algorithm.

Benchmarks are of the function only, not the interpreter starting or the input being read.


Update 1:

Benchmarks (on PyPy2): ~1.75ms for my AoC 50K input, ~4.44ms for a randomly generated 50K input, ~97.6ms for a 1M input, and ~521ms for a 5M input (the 1M * 5). The 50K and 1M random inputs were created by /u/mebeim.

def part2(d):
    d = [ord(x) for x in d]
    r = [0] * len(d)
    r[0] = d[0]
    ri = 1
    for i in range(1, len(d)):
        if ri and (r[ri - 1] ^ d[i]) == 32:
            ri -= 1  # .pop()
        else:
            r[ri] = d[i]  # .append(d[i])
            ri += 1
    d = r[:ri]
    min = float("inf")
    for c in range(97, 122 + 1):
        r[0] = d[0]
        ri = 1
        for i in range(1, len(d)):
            di = d[i]
            if di == c or di == c - 32:
                continue
            if ri and (r[ri - 1] ^ di) == 32:
                ri -= 1  # .pop()
                continue
            r[ri] = d[i]  # .append(d[i])
            ri += 1
        if ri < min:
            min = ri
    return min

Original post

Benchmarks (on PyPy2): ~8.33ms for my 50K input, ~29.2ms for a randomly generated 50K input from /u/mebeim, and ~674ms for a 1M input, also from /u/mebeim.

def part2(d):
    r = [0] * len(d)
    r[0] = d[0]
    ri = 1
    for i in range(1, len(d)):
        if ri and abs(ord(r[ri - 1]) - ord(d[i])) == 32:
            ri -= 1  # .pop()
        else:
            r[ri] = d[i]  # .append(d[i])
            ri += 1
    d = r[:ri]
    min = float("inf")
    for c in "abcdefghijklmnopqrstuvwxyz":
        r[0] = d[0]
        ri = 1
        for i in range(1, len(d)):
            di = d[i]
            if di.lower() == c:
                continue
            if ri and abs(ord(r[ri - 1]) - ord(di)) == 32:
                ri -= 1  # .pop()
            else:
                r[ri] = d[i]  # .append(d[i])
                ri += 1
        if ri < min:
            min = ri
    return min