Unable to load image

Advent of Code 2022 : Day 9

grate filter edition

:#marseycapyhacker:

17
Jump in the discussion.

No email address required.

I did part a without classes. Part b is reworked that also works with part a

with open('input09.txt') as file:
    lines = [line.strip().split(' ') for line in file]


# part b
class Segment:
    def __init__(self, link_down) -> None:
        self.x = 0
        self.y = 0
        self.link_down = link_down
        self.visited = set()
        
    def move(self, x, y):
        self.x += x
        self.y += y
        if self.link_down != None:
            self.link_down.update(self)
        
    def update(self, link_up):
        distance_x = abs(link_up.x - self.x)
        distance_y = abs(link_up.y - self.y)
        if distance_x >= 2 or distance_y >= 2:
            self.x += min(max(link_up.x - self.x, -1), 1)
            self.y += min(max(link_up.y - self.y, -1), 1)
        if self.link_down != None:
            self.link_down.update(self)
        else:
            self.visited.add('{}/{}'.format(self.x, self.y))


tail = Segment(None)
head_seg = tail
for i in range(9): # 1 for part a
    seg = Segment(head_seg)
    head_seg = seg

for line in lines:
    if line[0] == 'R':
        for i  in range(int(line[1])):
            head_seg.mp4e(1,0)
    elif line[0] == 'L':
        for i  in range(int(line[1])):
            head_seg.mp4e(-1,0)
    elif line[0] == 'U':
        for i  in range(int(line[1])):
            head_seg.mp4e(0,1)
    else:
        for i  in range(int(line[1])):
            head_seg.mp4e(0,-1)
print("Tail visited:",len(tail.visited))
Jump in the discussion.

No email address required.

imagine using classes. soy. cringe. unbased.

Jump in the discussion.

No email address required.

Yo dawg, classes are just a bunch of lists. The more I list the more based I am, right? :marseygigachad:

Jump in the discussion.

No email address required.



Now playing: Fear Factory (Tard Mix) (DKC).mp3

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