Unable to load image

Advent of Code 2022 : Day 9

grate filter edition

:#marseycapyhacker:

17
Jump in the discussion.

No email address required.

When that part 2 showed up i almost started sweating but it was actually pretty fine

import math
file=open("input.txt")

class Knot():
    def __init__(self, xpos, ypos):
        self.xpos = xpos
        self.ypos = ypos
        
k_c = 10
knots = []
for x in range (0,k_c):
    knots.append(Knot(0,0))

t_his = [(0,0)]
t_cnt = 1

def check(x, y):
    global t_his
    for val in t_his:
        if (x,y) == val: return True
    return False

def nextpos(x1, y1, x2, y2):
    if(abs(x1 - x2) > 1):
        x2 += int(math.copysign(1, x1 - x2))
        if y1 != y2:
            y2 += int(math.copysign(1, y1 - y2))
    if(abs(y1 - y2) > 1):
        y2 += int(math.copysign(1, y1 - y2))
        if x1 != x2:
            x2 +=  int(math.copysign(1, x1 - x2))
    return(x2, y2)
            
for line in file:
    cmds = line.rstrip().split()
    dir = (0,0)
    if cmds[0] == 'L': dir = (-1, 0)
    if cmds[0] == 'R': dir = (1, 0)
    if cmds[0] == 'U': dir = (0, 1)
    if cmds[0] == 'D': dir = (0, -1)
    
    for mag in range(0, int(cmds[1])):
        knots[0].xpos += dir[0]
        knots[0].ypos += dir[1]
        
        for knot in range(1, k_c):
            (knots[knot].xpos, knots[knot].ypos) = nextpos(knots[knot-1].xpos,knots[knot-1].ypos,knots[knot].xpos,knots[knot].ypos)
        
        if not check(knots[k_c-1].xpos, knots[k_c-1].ypos):
            t_his.append((knots[k_c-1].xpos, knots[k_c-1].ypos))
            t_cnt += 1
print(t_cnt)

Jump in the discussion.

No email address required.



Now playing: Funky the Main Monkey (DKC2).mp3

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