Skip to content

Latest commit

 

History

History
117 lines (91 loc) · 2.95 KB

9.md

File metadata and controls

117 lines (91 loc) · 2.95 KB

Day 9

Part 1 Part 2 Total
Time 29:26 6:57 36:23

Not proud of Part 1 time, but proud of my delta.

Part 1

I was stubbornly thinking that using if statements would be easier than implementing the distance calculation and edge cases, but alas I was wrong. I'm happy with my solution, how it moves T to the closest spot to H in the surrounding 3x3 unless H is within that 3x3.

from helpers.datagetter import data_in, submit
import math

ans = 0
din = data_in(split=True, numbers=False)

hx = 0
hy = 0
tx = 0
ty = 0
visited = {}


def move_t():
    global hx, hy, tx, ty
    sh_dist = 2
    sh = [tx, ty]
    for x in range(-1, 2):
        for y in range(-1, 2):
            if not (x == 0 and y == 0) and [tx, ty] != [hx, hy]:
                if [tx+x, ty+y] == [hx, hy]:
                    return
                if math.dist([tx+x, ty+y], [hx, hy]) < sh_dist:
                    sh_dist = math.dist([tx+x, ty+y], [hx, hy])
                    sh = [tx+x, ty+y]
    tx, ty = sh


visited[f"{tx},{ty}"] = 1
for line in din:
    instr = line.split(" ")[0]
    times = int(line.split(" ")[1])

    for i in range(times):
        if instr == "U":
            hy += 1
        if instr == "D":
            hy -= 1
        if instr == "L":
            hx -= 1
        if instr == "R":
            hx += 1
        move_t()
        visited[f"{tx},{ty}"] = 1


ans = len(visited)
print(ans)
submit(ans)

Part 2

Find and replace came in handy here! Move 1 according to H, move 2 according to 1, etc.

from helpers.datagetter import data_in, submit
import math

ans = 0
din = data_in(split=True, numbers=False)

links = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
visited = {}


def move_t(move, ref):
    global links
    sh_dist = 2
    sh = [links[move][0], links[move][1]]
    for x in range(-1, 2):
        for y in range(-1, 2):
            if not (x == 0 and y == 0) and [links[move][0], links[move][1]] != [links[ref][0], links[ref][1]]:
                if [links[move][0]+x, links[move][1]+y] == [links[ref][0], links[ref][1]]:
                    return
                if math.dist([links[move][0]+x, links[move][1]+y], [links[ref][0], links[ref][1]]) < sh_dist:
                    sh_dist = math.dist([links[move][0]+x, links[move][1]+y], [links[ref][0], links[ref][1]])
                    sh = [links[move][0]+x, links[move][1]+y]
    links[move][0], links[move][1] = sh


visited[f"{links[9][0]},{links[9][1]}"] = 1
for line in din:
    instr = line.split(" ")[0]
    times = int(line.split(" ")[1])

    for i in range(times):
        if instr == "U":
            links[0][1] += 1
        if instr == "D":
            links[0][1] -= 1
        if instr == "L":
            links[0][0] -= 1
        if instr == "R":
            links[0][0] += 1
        for link in range(1, 10):
            move_t(link, link-1)
        visited[f"{links[9][0]},{links[9][1]}"] = 1


ans = len(visited)
print(ans)
submit(ans)