Unable to load image

Advent of Code 2022 : Day 9

grate filter edition

:#marseycapyhacker:

17
Jump in the discussion.

No email address required.

Late submission because I was watching divegrass and nearly forgot to submit today :marseysweating:

Messy but it works. Part 2 made me have to rewrite the whole thing since I assumed the tail would always take the position of the head in the previous state.

with open('day9_input.txt', 'r') as inp:
    foo = inp.read().split('\n')

rope_pos = [[0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0]]
history = set()

for f in foo:
    f = f.split(' ')
    for n in range(int(f[1])):
        for m in range(len(prev_pos)):
            prev_pos[m] = rope_pos[m]
        if f[0] == 'L':
            rope_pos[0] = [rope_pos[0][0]-1, rope_pos[0][1]]
        elif f[0] == 'R':
            rope_pos[0] = [rope_pos[0][0]+1, rope_pos[0][1]]
        elif f[0] == 'U':
            rope_pos[0] = [rope_pos[0][0], rope_pos[0][1]+1]
        elif f[0] == 'D':
            rope_pos[0] = [rope_pos[0][0], rope_pos[0][1]-1]
        for m in range(1, len(rope_pos)):
            if abs(rope_pos[m-1][0] - rope_pos[m][0]) > 1 or abs(rope_pos[m-1][1] - rope_pos[m][1]) > 1:
                candidate_pos = [[rope_pos[m][0]+1, rope_pos[m][1]],
                                [rope_pos[m][0]-1, rope_pos[m][1]],
                                [rope_pos[m][0], rope_pos[m][1]+1],
                                [rope_pos[m][0], rope_pos[m][1]-1],
                                [rope_pos[m][0]+1, rope_pos[m][1]+1],
                                [rope_pos[m][0]-1, rope_pos[m][1]+1],
                                [rope_pos[m][0]+1, rope_pos[m][1]-1],
                                [rope_pos[m][0]-1, rope_pos[m][1]-1]]
                min_displacement = 10
                min_candidate = []
                for c in candidate_pos:
                    if (abs(rope_pos[m-1][0] - c[0]) + abs(rope_pos[m-1][1] - c[1])) < min_displacement:
                        min_displacement = (abs(rope_pos[m-1][0] - c[0]) + abs(rope_pos[m-1][1] - c[1]))
                        min_candidate = c
                rope_pos[m] = min_candidate
        history.add(str(rope_pos[-1]))
print(len(history))
Jump in the discussion.

No email address required.

divegrass

just looked that up, sounds funny. is it still going or is it over?

Jump in the discussion.

No email address required.

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