Created
December 9, 2023 13:13
-
-
Save korjavin/2709ac0b06ed305d53d7f65888260c6f to your computer and use it in GitHub Desktop.
aoc3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def main(): | |
# Read input from stdin | |
lines = [] | |
for line in sys.stdin: | |
chars = list(line.strip()) | |
lines.append(chars) | |
#print(lines) | |
current_number_as_string = "" | |
adjacent_flag = False | |
sum = 0 | |
list_of_stars = [] | |
list_of_numbers = [] # New list to store numbers | |
for i, line in enumerate(lines): | |
for j, char in enumerate(line): | |
if char == '*': | |
list_of_stars.append((i, j)) | |
if char.isdigit(): # Check if the character is a digit | |
current_number_as_string += char | |
adjacent_flag = True | |
elif adjacent_flag: # Check if adjacent_flag is True | |
list_of_numbers.append((i,j-1,int(current_number_as_string))) | |
current_number_as_string = "" | |
adjacent_flag = False | |
print(f"List of numbers: {list_of_numbers}") | |
print(f"List of selected stars: {list_of_stars}") | |
def find_connected_stars(stars, numbers): | |
connected_stars = [] | |
for star in stars: | |
connected_numbers = 1 | |
count = 0 | |
for number in numbers: | |
if abs(star[0] - number[0]) <=1 and -1 - len(str(number[2])) < star[1] - number[1] <= +1: | |
count += 1 | |
connected_numbers *= number[2] | |
if count == 2: | |
connected_stars.append((star, connected_numbers)) | |
return connected_stars | |
connected_stars = find_connected_stars(list_of_stars, list_of_numbers) | |
print(f"Connected stars: {connected_stars}") | |
for star in connected_stars: | |
sum += star[1] | |
print(f"Sum: {sum}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment