Unable to load image

day 17 aoc thread: rocks fall everyone dies

bottom text

42
Jump in the discussion.

No email address required.

Very messy solution, but that's because I solved this on a samsung tablet while at a Christmas party so I don't lose my streak lmao


foo = ""
with open('day17_input.txt', 'r') as inp:
    foo = inp.read()

grid = [['.' for x in range(7)]]
v_heights = [1,3,3,4,2]
rocks = [[[0,2],[0,3],[0,4],[0,5]],
        [[0,3],[1,2],[1,3],[1,4],[2,3]],
        [[0,4],[1,4],[2,2],[2,3],[2,4]],
        [[0,2],[1,2],[2,2],[3,2]],
        [[0,2],[0,3],[1,2],[1,3]]]

turn = 0
height = 0
lines_trimmed = 0
rocks_placed = 0

memory = [[[] for y in range(5)] for x in range(len(foo))]
no_rocks_to_place = 1000000000000
while rocks_placed < no_rocks_to_place:
    while(len(grid) < 3+v_heights[rocks_placed%5] or '#' in grid[2+v_heights[rocks_placed%5]]):
        grid = [['.' for x in range(7)]] + grid
    current_rock_pos = rocks[rocks_placed%5].copy()
    placed = False
    while not placed:
        if foo[turn%len(foo)] == '>':
            right_pushable = True
            for r in current_rock_pos:
                if r[1] == 6 or grid[r[0]][r[1]+1] == '#':
                    right_pushable = False
                    break
            if right_pushable:
                for r in range(len(current_rock_pos)):
                    current_rock_pos[r] = [current_rock_pos[r][0], current_rock_pos[r][1]+1]
        else:
            left_pushable = True
            for r in current_rock_pos:
                if r[1] == 0 or grid[r[0]][r[1]-1] == '#':
                    left_pushable = False
                    break
            if left_pushable:
                for r in range(len(current_rock_pos)):
                    current_rock_pos[r] = [current_rock_pos[r][0], current_rock_pos[r][1]-1]
        for r in current_rock_pos:
            if r[0] == len(grid)-1 or grid[r[0]+1][r[1]] == '#':
                placed = True
                break
        if not placed:
            for r in range(len(current_rock_pos)):
                current_rock_pos[r] = [current_rock_pos[r][0]+1, current_rock_pos[r][1]]
        else:
            h = 0
            while h < len(grid) and '#' not in grid[h]:
                h += 1
            new_h = len(grid[0])
            for r in current_rock_pos:
                grid[r[0]][r[1]] = '#'
                if(r[0] < new_h):
                    new_h = r[0]
            height += max(0,h-new_h)
        turn += 1
    rocks_placed += 1
    while '#' not in grid[0]:
        grid = grid[1:]

    min_column = [False for n in range(7)]
    for g in range(len(grid)):
        for a in range(7):
            if grid[g][a] == '#':
                min_column[a] = True
        if sum(min_column) == 7:
            grid = grid[:g]
            break

    save_state = ['\n'.join([''.join(g) for g in grid]), rocks_placed, height]
    for m in memory[turn%len(foo)][rocks_placed%5]:
        if m[0] == save_state[0]:
            height += ((no_rocks_to_place-rocks_placed)//(save_state[1]-m[1]))*(save_state[2]-m[2])
            rocks_placed += ((no_rocks_to_place-rocks_placed)//(save_state[1]-m[1]))*(save_state[1]-m[1])
    memory[turn%len(foo)][rocks_placed%5].append(save_state)    
print(height)


Jump in the discussion.

No email address required.

:#marseywoah:

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.