This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
2
[2022 Day 9 (Part 1)] Need some help, not sure why it's not working
Post Flair (click to view more posts with a particular flair)
Post Body
import math
import itertools
with open('puzzle_input.txt') as data:
datalines = (line.rstrip('\r\n') for line in data)
moves = []
for line in datalines:
split = line.split(" ", 1)
moves.append([split[0], int(split[1])])
def new_chain_of_coordinates(last_coordinate, direction, number_of_moves):
last_x_coordinate = last_coordinate[0]
last_y_coordinate = last_coordinate[1]
if(direction == "U"):
return([[last_x_coordinate, (last_y_coordinate i)] for i in range(1, number_of_moves 1)])
elif(direction == "D"):
return([[last_x_coordinate, (last_y_coordinate - i)] for i in range(1, number_of_moves 1)])
elif(direction == "L"):
return([[(last_x_coordinate-i), last_y_coordinate] for i in range(1, number_of_moves 1)])
elif(direction == "R"):
return([[(last_x_coordinate i), last_y_coordinate] for i in range(1, number_of_moves 1)])
else:
return(last_coordinate)
head_coordinates_list = [[0, 0]]
for move in moves:
head_coordinates_list = new_chain_of_coordinates(
head_coordinates_list[-1], move[0], move[1])
def tails_next_position(tail_current_position, head_next_position):
head_x = head_next_position[0]
head_y = head_next_position[1]
tail_x = tail_current_position[0]
tail_y = tail_current_position[1]
if points_touching(head_next_position, tail_current_position) == True:
return(tail_current_position)
# head and tail on same horizontal and aren't touching
if((tail_y == head_y) & (abs(head_x - tail_x) == 2)):
if(head_x > tail_x):
return([tail_x 1, tail_y])
if(head_x < tail_current_position[1]):
return([tail_x - 1, tail_y])
# head and tail on same vertical and aren't touching
if((tail_x == head_x) & (abs(head_y - tail_y) == 2)):
if(head_y > tail_y):
return([tail_x, tail_y 1])
if(head_x < tail_x):
return([tail_x, tail_y-1])
# head and tail are on diagonals and aren't touching
if((head_x > tail_x) & (head_y > tail_y)):
return([tail_x 1, tail_y 1])
if((head_x < tail_x) & (head_y < tail_y)):
return([tail_x-1, tail_y-1])
if((head_x < tail_x) & (head_y > tail_y)):
return([tail_x-1, tail_y 1])
if((head_x > tail_x) & (head_y < tail_y)):
return([tail_x 1, tail_y-1])
return(tail_current_position)
def points_touching(head_coordinate, tail_coordinate):
head_x = head_coordinate[0]
head_y = head_coordinate[1]
tail_x = tail_coordinate[0]
tail_y = tail_coordinate[1]
distance = math.sqrt(
pow((head_x-tail_x), 2) pow((head_y-tail_y), 2))
# if distance is square root of 2 then they are touching
if distance <= math.sqrt(2):
return True
return False
tail_coordinates_list = [[0, 0]]
for h in range(1, len(head_coordinates_list)):
tail_coordinates_list.append(tails_next_position(
tail_coordinates_list[-1], head_coordinates_list[h]))
#print(tail_coordinates_list)
tail_coordinates_list_deduplicated = []
for elem in tail_coordinates_list:
if elem not in tail_coordinates_list_deduplicated:
tail_coordinates_list_deduplicated.append(elem)
print(len(tail_coordinates_list_deduplicated))
I'm not sure why this isn't working. For the example puzzle input I tracked the path of the head and tail and I get the correct path for each. So I'm not sure where I'm going wrong. It's probably something simple but if you can help me that would be great! Thanks.
Author
User Disabled
Account Strength
0%
Disabled 1 year ago
Account Age
5 years
Verified Email
Yes
Verified Flair
No
Total Karma
1,622
Link Karma
1,013
Comment Karma
390
Profile updated: 5 hours ago
Posts updated: 1 year ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 1 year ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/adventofcod...