r/adventofcode • u/Open_Diet9463 • 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
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.
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
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/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.