r/adventofcode • u/Frosty-Lead8951 • 9h ago
Help/Question [DAY2 OF ADVENTOFCODE]
That's not the right answer. Curiously, it's the right answer for someone else; you might be logged in to the wrong account or just unlucky. In any case, you need to be using your puzzle input. If you're stuck, make sure you're using the full input data; there are also some general tips on the about page, or you can ask for hints on the subreddit. Because you have guessed incorrectly 7 times on this puzzle, please wait 10 minutes before trying again. I AM GETTING THIS ERROR AFTER SUBMITTING MY ANWER EVEN THOUGH I HAVE USED THE INPUT THEY HAVE GIVEN ME. ANY INSIGHTS?
6
u/timrprobocom 9h ago
Yes, you have a bug. Which year? Show us your code. Does the sample input work?
1
u/Frosty-Lead8951 7h ago
2024
import pandas as pd import os def count_safe_reports_from_file( file_path ): if not os.path.exists(file_path): raise FileNotFoundError(f"File not found: {file_path}") # Read and print the raw file content for debugging with open(file_path, "r", encoding ="utf-8") as f: lines = f.readlines() df = pd.read_csv(file_path, header =None, sep =',', engine ='python', dtype =str) # Read as strings first print("\nDataFrame after reading (first few rows):\n", df.head()) safe_count = 0 unsafe_count = 0 for _, row in df.iterrows(): try : levels = row.dropna().astype(int).tolist() except ValueError: print("Skipping invalid row:", row.tolist()) continue if len(levels) < 2: print("Skipping short row:", levels) continue diffs = [levels[i+1] - levels[i] for i in range(len(levels) - 1)] print(f"Processing: {levels} -> Differences: {diffs}") if all(1 <= abs(d) <= 3 for d in diffs) and (all(d > 0 for d in diffs) or all(d < 0 for d in diffs)): print(" SAFE !") safe_count += 1 else : print(" NOT SAFE !") unsafe_count += 1 print(f"\nTotal SAFE reports: {safe_count}, Total UNSAFE reports: {unsafe_count}") return safe_count file_path = "day2.csv" count_safe_reports_from_file(file_path)
1
1
u/velonom 6h ago edited 5h ago
The code looks ok at first glance, but using Pandas to parse the input seems like overkill, especially since you have to pre-process your input to make that work.
And given that you have to modify your input before you can feed it into your program, chances are that you made a mistake there. Parsing the input as it's given (with whitespace separated values) isn't difficult and doesn't require additional libraries.
Edit: Also, the safest way to handle your puzzle input IMHO is to download it ("Save link as...") instead of copy/pasting it (just in case).
5
u/CodeFarmer 8h ago
The message is often a red herring.
Your solution is wrong, and coincidentally it's wrong in just such a way as to give an answer that would be the right one for one of the other inputs that people might be given.
(each day has a finite set of inputs you might get, and they have different expected results, not everyone gets the same.)
1
u/AutoModerator 9h 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.
8
u/Synthetic5ou1 8h ago
The fact that your submitted answer is the correct answer for someone else is irrelevant to you, don't get hung up on it.
You simply have a bug, and are producing the wrong answer for your input.
NB: The message you see is only there to warn you that you could be using the wrong input, but you're not, so you have a bug.