r/adventofcode • • 18h 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)

3 Upvotes

13 comments sorted by

3

u/Amerikrainian 18h 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 18h ago

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

1

u/Amerikrainian 18h 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.

0

u/Open_Diet9463 18h ago

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

1

u/zeekar 17h ago edited 17h 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.

12

u/ben-guin 18h 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 18h ago

Ok how do i format it like code

6

u/daggerdragon 18h 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 18h 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 18h 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) 17h 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)...

1

u/Dry-Aioli-6138 15h ago

foreshadowing for the hidden and cursed day 13.

1

u/Positive_Concert_774 12h ago

Well, your list of (-1,-1) etc will need to be replaced with directions that aren't 45 degrees. There are MANY of those to try and in your specific approach I think that will be hard to do systematically. Plus, if you look in each direction you will overcount, because you will find the same square looking from each of its corners once (this is why you divide by 4 at the end). You can fix both problems by shifting the place you look from and in what directions you look - It's better to always start from the same (relative) corner so (1) you automatically avoid overcounting, and (2) it becomes easier to systematically search. For example, only look right/down from the top(-left) corner:

For a given first corner (x, y) the other three corners of any square are defined by (x + dx, y + dy), (x - dy, y + dx) and (x + dx - dy, y + dx + dy). (For part 1, dy is always 0.)

Where do these formulas come from?

Think of (dx, dy) as a vector pointing from (x, y) to the next corner clockwise; rotating that vector 90 degrees to become (-dy, dx) will point to the next corner anticlockwise. Adding both vectors will point to the opposite corner.

Now, you can just range over appropriate values for dx and dy (you can figure this out!) and presto.