Unable to load image

Advent of Code 2022 : Day 9

grate filter edition

:#marseycapyhacker:

17
Jump in the discussion.

No email address required.

Lost like 9 minutes because I forgot to add the final position to the visited set

import numpy as np
def updateTail(chx,chy,ctx,cty):
    dx = chx-ctx
    dy = chy-cty
    if max(abs(dx),abs(dy)) < 2:
        return (ctx,cty)
    if dx == 0 and abs(dy) == 2:
        cty += dy//2
        return (ctx,cty)
    if dy == 0 and abs(dx) == 2:
        ctx += dx//2
        return (ctx,cty)
    else:
        return (ctx+dx//abs(dx),cty+dy//abs(dy))
def updateKnots(chx,chy,knots):
    for i in range(len(knots)):
        knots[i] = updateTail(chx,chy,knots[i][0],knots[i][1])
        chx,chy = knots[i][0],knots[i][1]
    return knots

f = open('AOC2022Day9.txt')
lines = f.read().strip().split('\n')
info = [line.split(' ') for line in lines]
visited = set()
chx = 0
chy = 0
ctx = 0
cty = 0
knots = [(0,0) for i in range(9)]
for i in info:
    direc = i[0]
    numsteps = int(i[1])
    for j in range(numsteps):
        visited.add(knots[-1])
        if direc == 'R':
            chx += 1
        elif direc == 'L':
            chx -= 1
        elif direc == 'U':
            chy += 1
        elif direc == 'D':
            chy -= 1
        knots = updateKnots(chx,chy,knots)
visited.add(knots[-1])
print(len(visited))
Jump in the discussion.

No email address required.



Now playing: Stickerbrush Symphony (DKC2).mp3

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