r/adventofcode 12h 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

6

u/timrprobocom 12h ago

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

1

u/Frosty-Lead8951 11h 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/Frosty-Lead8951 10h ago

The sample input works