Vigyata.AI
Is this your channel?

ADVENT OF CODE 2024 | Day 2 Jackie's Python Solution

9.3K views· 329 likes· 4:41· Dec 4, 2024

How did you solve Day 2? Leave a comment below, like and subscribe! :) 0:00 challenge description 0:53 PART 1 solution 3:32 PART 2 solution #adventofcode #vlogmas #programming

About This Video

In this Advent of Code 2024 video, I’m solving Day 2: “Rednosed Reports” in Python, and the whole task is basically about checking whether each report (each line of numbers) is “safe.” A report is safe if all the levels are strictly increasing or strictly decreasing, and every adjacent difference is between 1 and 3 (or between -1 and -3 for decreasing). I walk through the examples from the prompt, then show the exact approach I coded up to validate each report efficiently. My solution leans on NumPy because we’re doing a lot of subtraction across arrays, and it’s just really clean to compute adjacent differences with np.diff. From there, I check the safety rules using np.all: either all diffs are in [1, 3] (ascending) or all diffs are in [-3, -1] (descending). For Part 1, I sum up the safe reports by looping through each row and calling my is_safe function. For Part 2, I explain the updated rule: if removing a single value makes the report safe, we count it as safe. I handle this with a straightforward brute-force method—remove one element at a time, re-check safety, and accept the report if any variation passes. That’s Day 2 done, and I’ll be back for Day 3.

Frequently Asked Questions

🎬 More from CS Jackie