Metadata
-
Date
-
Tagged
-
Part of series
-
Older post
-
Newer post
Advent of Code 2024 Day 2
Day 2: Red-Nosed Reports
https://adventofcode.com/2024/day/2
Today’s location to search for the chief historian is familiar. You remember visiting it, in the before times (of 2015).
The elves in charge of the place ask you to help with some weird report data the reactor spit out. That’s your input, a list of reports, one per line. Each report is a bunch of numbers separated by spaces.
An example input looks like this:
Parsing
Today, I chose to make a function to parse a single line into a list of numbers. Why not the entire input? Because I wrote it like this first, and this is fine.
Part 1
You need to determine if a report is safe.
A report is safe if:
- All levels are increasing or they are all decreasing
- Any two adjacent levels differ by at least 1 and at most 3
I wrote two solutions, one with good ol’ for
loops, and one focussing on using iterators.
Both solutions use a helper method called is_safe
that takes in a report and returns a boolean.
For both solutions I used itertools
to help iterate over a sliding window of size 2.
The question asks how many reports are safe.
Helper
Option 1: Loopin’ with for
Option 2: Iterators
Option 1: Loopin’ with for
Option 2: Iterators
Part 2
Turns out the reactor can handle one bad level. If it sees a bad level for the first time, it pretends nothing happened.
Do the same sum, but count reports that have a single bad value as well.
Same thing here, I coded 2 solutions.
The question asks how many reports are safe.
Helper
The helper for part2 uses the helper for part1. Aaaaaaah, code reuse.
It loops over each report, deleting one value at a time. If it finds a safe report (with a deleted value), the report is deemed safe.
It’s brute-force, but no worries, computer goes BRRRRRRRRRRR after all.