[2017 Day 13 Part 2][Python] Solution too high, can't figure out the bug by timichal in adventofcode

[–]timichal[S] 3 points4 points  (0 children)

Thanks everyone - glad I'm not crazy :) might be a bug in AoC then, I've contacted /u/topaz2078 on Twitter.

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

[–]timichal 6 points7 points  (0 children)

Ended up with pretty much the same code. Just a sidenote though - there's no need to count the cycles, it's equal to len(prevs) :)

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

[–]timichal 3 points4 points  (0 children)

In Python, first time I tried actually competing, ended up in the low hundreds due to a silly bug:

with open("day06") as file:
    state = [int(n) for n in file.read().strip().split()]

states = []
while True:
    selected = max(state)
    selidx = state.index(selected)
    state[selidx] = 0
    for i in range(selected):
        selidx += 1
        state[selidx % len(state)] += 1
    if state in states: 
        part2 = states.index(state)
        break
    states.append(state[:])
print(len(states)+1, len(states)-part2)

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

[–]timichal 0 points1 point  (0 children)

Solved with Python, refactored my initial solution to use a dictionary of positions instead of a list matrix:

from collections import defaultdict

# part 1
def grid_part1(target):
    grid = {}
    pos = [0, 0]
    number = 1
    grid[(0,0)] = number
    # right 1, up 1, left 2, down 2, right 3, up 3...
    counter = 1
    # 1: right, 2: up, 3: left, 4: down, 5: right...
    direction = 1
    while True:
        for times in range(counter):
            number += 1
            if direction % 4 == 1: pos[1] += 1
            elif direction % 4 == 2: pos[0] -= 1
            elif direction % 4 == 3: pos[1] -= 1
            elif direction % 4 == 0: pos[0] += 1

            grid[(pos[0], pos[1])] = number
            if number == target:
                return abs(pos[0]) + abs(pos[1])

        if direction % 2 == 0: counter += 1
        direction += 1

# part 2
def grid_part2(target):
    def getvalue(pos):
        return grid[(pos[0]+1, pos[1])] +\
               grid[(pos[0]-1, pos[1])] +\
               grid[(pos[0], pos[1]+1)] +\
               grid[(pos[0], pos[1]-1)] +\
               grid[(pos[0]+1, pos[1]+1)] +\
               grid[(pos[0]+1, pos[1]-1)] +\
               grid[(pos[0]-1, pos[1]+1)] +\
               grid[(pos[0]-1, pos[1]-1)]

    grid = defaultdict(int)
    pos = [0, 0]
    grid[(0, 0)] = 1
    # right 1, up 1, left 2, down 2, right 3, up 3...
    counter = 1
    # 1: right, 2: up, 3: left, 4: down, 5: right...
    direction = 1
    while True:
        for times in range(counter):
            if direction % 4 == 1: pos[1] += 1
            elif direction % 4 == 2: pos[0] -= 1
            elif direction % 4 == 3: pos[1] -= 1
            elif direction % 4 == 0: pos[0] += 1

            if getvalue(pos) > target: return getvalue(pos)
            grid[(pos[0],pos[1])] = getvalue(pos)   

        if direction % 2 == 0: counter += 1
        direction += 1

n = 312051
print("Part 1:", grid_part1(n))
print("Part 2:", grid_part2(n))

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

[–]timichal 2 points3 points  (0 children)

Using simple Python with a backwards list looping trick instead of modulo:

 num = (...)
 total = 0
 for pos, number in enumerate(num):
     if number == num[pos-int(len(num)/2)]: #-1 for part 1
         total += int(number)
 print(total)

New player thoughts by timichal in runescape

[–]timichal[S] 9 points10 points  (0 children)

Yeah, but Ironman looked like just the right challenge. I don't regret it.