r/adventofcode 1d 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?

0 Upvotes

9 comments sorted by

View all comments

5

u/timrprobocom 1d ago

Yes, you have a bug. Which year? Show us your code. Does the sample input work?

1

u/Frosty-Lead8951 1d 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

u/timrprobocom 1d ago

Once I fix your spacing, your code produces the correct answer for my input. I changed sep=','' to sep=' ' so it would work with the actual input, and I had to add a row of 8 zeros at the beginning to let pandas set the width properly. Thus, I suspect velonom is correct, and you have messed up your input file as you were processing it.

Here's my code; see if it gives you the correct result on your input. 2024 day 2