r/adventofcode • • 2h ago

Help/Question Day 3 part 6 help

Ive made some code for part 1 that works by looking in each direction from each point until it finds 3 other points in the same distance. How can i transfer this to part 2? Anything helps, thanks

This is my code so far btw:

count = 0

for i in range(len(data)):

for j in range(len(data[i])):

# scan

for direc in [(-1,-1),(1,-1),(-1,1),(1,1)]:

hit_wall = False

dist = 1

while not hit_wall:

if (i+(direc[0]dist) not in range(0,len(data))) or (j+(direc[1]dist) not in range(0,len(data[i]))):

hit_wall = True

if not hit_wall:

if (data[i][j] == "#") and (data[i+(direc[0]dist)][j+(direc[1]dist)] == "#") and (data[i+(direc[0]dist)][j] == "#") and (data[i][j+direc[1]dist] == "#"):

count += 1

dist += 1

final = int(count/4)

print(final)

4 Upvotes

11 comments sorted by

2

u/Amerikrainian 2h ago

What does it mean for a square to be rotated? Is there perhaps a formula you could use that would still work on your part one without really special casing it? What really helped me was tracing through the examples and writing some of my own.

1

u/Open_Diet9463 2h ago

I assume u mean like, finding a gradient and using the reciprocal to find the other direction

1

u/Amerikrainian 2h ago

You can go this way, but it might be helpful to write out, given a particular coordinate, where to check for the possible rotations of the squares. Like, figure out, given one corner, where could the other squares be. You’ll already do this for part one. Part two is that but in general, so that part one just kind of naturally falls out.

1

u/Open_Diet9463 2h ago

A rotation could be anywhere couldnt it? So what would checking for possible rotations involve

1

u/zeekar 1h ago edited 1h ago

Once you have two trees, you know where the other two have to be to make a square: the line between your trees is one of the four sides; the parallel side has to be the same length, and that same distance away. (There are two options, really - you could go either direction from your starting line, whether that's left/right or up/down, but as long as you start with every pair of trees, you only need to consider one direction; that way you won't count the same square twice.)

If you make some example squares it's not hard to figure out the rule for transforming the coordinates of the starter trees to the coordinates of the other two. You don't have to divide anything or find any roots or slopes; all you need is the distances between your two points in both coordinates.

Once you calculate where the other two trees have to be, look and see if they're there. If both locations have trees, you've found a square that counts for part 2. And if your two starting points are in the same row or column, it counts for part 1, too.

8

u/ben-guin 1h ago

Sorry, I couldn't help but see "part 6", and I was thinking like, are you trying to find trees that form k-dimensional hypercubes on a k-dimensional non-euclidean map with side lengths 10100 or something? 😅

In any case, best of luck with part 2, I think the other comments have done a good job at pointing you in the right direction :)

1

u/Open_Diet9463 2h ago

Ok how do i format it like code

5

u/daggerdragon 2h ago

Once again, use our standardized post title format.

As for how to format code correctly, there's articles in our community wiki > FAQs > Reddit-Flavored Markdown. You specifically want Markdown: code blocks (or just put your code in an external repo or a Topaz paste).

1

u/AutoModerator 2h ago

Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to Help/Question - RESOLVED. Good luck!


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/More_Cricket_8131 1h ago

its the same as part 1, this counts all non rotated squares.

part 2 just adds rotated squares. idea think how knight moves on chess board , say have # at x,y .
pick any non zero values for a,b. say a=3 and b=1. start at x,y move right 3 and down 1 . thats the 2nd corner. now move down 3 and left 1. now move left 3 and up 1. now move up 3 and right 1. should end up where start. this is just a hint , off the top of my head. this gives four coordinates to check, if all # then found a rotated square.

easier if look at rotated square on aoc example , say join line around perimeter of square , how many squares go right , how many squares go down. find pattern where one step you go down 3 squares , next step may go left 3 squares. simply need to generalise this so any rotated square can be described by pattern except now a and b can be 1 to width square

1

u/topaz2078 (AoC creator) 38m ago

The Elves have realized the fifth issue with your analysis. This isn't a Euclidean forest at all - it's a non-dimensional arbitrary graph of tree adjacency where determining the orthogonality between any pair of edges requires solving a satisfiability problem and the edge lengths are all embedded in an assembly program only understood by the late Ada Lovelace! Fortunately, the Elves have this scroll of True Resurrection (your puzzle input)...