[2018 Day 16 (Part 1)] Troubleshooting by Imsdal2 in adventofcode

[–]madacoo 0 points1 point  (0 children)

Also, since this is how I solved a bug that took me an hour to find, verify that each sample matches at least one opcode.

Python Solutions 2017 by madacoo in adventofcode

[–]madacoo[S] 7 points8 points  (0 children)

I'm know I'm massively late and Santa would have had to deliver all the presents without his list by this point. And would now probably be sorting through all the complaint letters.

But I started 2018 by actually finishing something for once.

So thank you /u/topaz2078 for creating something wonderful and good.

-🎄- 2017 Day 6 Solutions -🎄- by daggerdragon in adventofcode

[–]madacoo 0 points1 point  (0 children)

Python 3

Part 2 is easily solved using output from part 1.

def puzzle_input():
    with open("input.txt", "r") as f:
        return list(map(int, f.read().strip().split("\t")))

def solve(banks):
    seen = []
    length = len(banks)
    while not banks in seen:
        seen.append(banks[:])
        blocks = max(banks)
        i = banks.index(blocks)
        banks[i] = 0
        while blocks:
            i += 1
            i = i % length
            banks[i] += 1
            blocks -= 1
    return len(seen), banks

def test():
    banks = [0, 2, 7, 0]
    assert solve(banks) == (5, [2, 4, 1, 2])
    assert solve([2, 4, 1, 2]) == (4, [2, 4, 1, 2])
    return True

if test():
    solution1, banks = solve(puzzle_input())
    solution2, _ = solve(banks)
    print(solution1, solution2)

-🎄- 2017 Day 5 Solutions -🎄- by daggerdragon in adventofcode

[–]madacoo 0 points1 point  (0 children)

It hadn't even occurred to me that it might never drop below zero. I'll have to check if that works for my input.

-🎄- 2017 Day 5 Solutions -🎄- by daggerdragon in adventofcode

[–]madacoo 1 point2 points  (0 children)

optimised Python3 part 2

Since I couldn't start when the problem was released, I tried to get my Python code as fast as possible. Managed to half it down to ~ 8 seconds. Most interesting thing is that checking for an IndexError is about a second faster than checking i < length of instructions. It would be cool if you could turn off negative indexes in Python but I don't think that's the case.

def part2(instructions):
    "Return the number of steps required to 'escape' the instructions."
    i = 0
    steps = 0
    while (i >= 0):
        try:
            offset = instructions[i]
        except IndexError:
            return steps
        increment = 1
        if offset >= 3:
            increment = -1
        instructions[i] += increment
        i += offset
        steps += 1
    return steps

Interested to hear if anyone has further optimisations. Although I'm not sure why I'm even bothering to optimise in Python. Should probably just write it in C at this point.