Unable to load image

AoC Day 14 : Sands of time

I won't paste my code yet since my parser just does the needful, and I somehow manage to get overlapping grains of sand, though that doesn't matter in the end.

I'm also seeing a lot of pythonistas having high runtime (1-10s+ and more) and I'm here with the bruteforce grain-by-grain taking 150ms for part 2 (including drawing the final graph lmao). Matlab chads can't stop winning :gigachad2:

EDIT : part 2 webm

16
Jump in the discussion.

No email address required.

I probably overdid it since I assume there's a way of simulating it without recreating the entire temple structure in matrix format, but doing it that way seemed more fun.

foo = []
path_list = []
min_x, max_x, max_y = 0, 1000, 0

with open('day14_input.txt', 'r') as inp:
    foo = inp.read().split('\n')
for f in foo:
    f = f.split(' -> ')
    path = []
    for g in f:
        x, y = int(g.split(',')[0]), int(g.split(',')[1])
        if y >= max_y:
            max_y = y+1
        path.append([x, y])
    path_list.append(path)

grid = [['.' for x in range(max_x-min_x+1)] for y in range(max_y+1)]
grid.append(['#' for x in range(max_x-min_x+1)])

for path in path_list:
    for n in range(len(path)-1):
        if path[n][1] == path[n+1][1]:
            for x in range(min(path[n][0], path[n+1][0]), max(path[n][0], path[n+1][0])+1):
                grid[path[n][1]][x-min_x] = '#'
        elif path[n][0] == path[n+1][0]:
            for y in range(min(path[n][1], path[n+1][1]), max(path[n][1], path[n+1][1])+1):
                grid[y][path[n][0]-min_x] = '#'

def add_sand(x_pos):
    y_pos = 0
    if grid[y_pos][x_pos] != '.':
        return False
    while True:
        if grid[y_pos+1][x_pos] == '.':
            y_pos += 1
        elif grid[y_pos+1][x_pos-1] == '.':
            x_pos -= 1
            y_pos += 1
        elif grid[y_pos+1][x_pos+1] == '.':
            x_pos += 1
            y_pos += 1
        else:
            grid[y_pos][x_pos] = 'o'
            return True

count = 0
while add_sand(500):
    count += 1
print(count)
Jump in the discussion.

No email address required.

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