Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/2024/day-02.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Day 2: Red-Nosed Reports

## Part One

Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office.

While the [Red-Nosed Reindeer nuclear fusion/fission plant](https://adventofcode.com/2015/day/19) appears to contain no sign of the Chief Historian, the engineers there run up to you as soon as they see you. Apparently, they _still_ talk about the time Rudolph was saved through molecular synthesis from a single electron.

They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data.

The unusual data (your puzzle input) consists of many _reports_, one report per line. Each report is a list of numbers called _levels_ that are separated by spaces. For example:

```
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
```

This example data contains six reports each containing five levels.

The engineers are trying to figure out which reports are _safe_. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true:

- The levels are either _all increasing_ or _all decreasing_.
- Any two adjacent levels differ by _at least one_ and _at most three_.

In the example above, the reports can be found safe or unsafe by checking those rules:

- `7 6 4 2 1`: _Safe_ because the levels are all decreasing by 1 or 2.
- `1 2 7 8 9`: _Unsafe_ because `2 7` is an increase of 5.
- `9 7 6 2 1`: _Unsafe_ because `6 2` is a decrease of 4.
- `1 3 2 4 5`: _Unsafe_ because `1 3` is increasing but `3 2` is decreasing.
- `8 6 4 4 1`: _Unsafe_ because `4 4` is neither an increase or a decrease.
- `1 3 6 7 9`: _Safe_ because the levels are all increasing by 1, 2, or 3.

So, in this example, `_2_` reports are _safe_.

Analyze the unusual data from the engineers. _How many reports are safe?_
70 changes: 70 additions & 0 deletions src/aoc/2024/day_02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from __future__ import annotations
from functools import reduce
from itertools import pairwise
from operator import sub
from typing import Iterable, Iterator, List, Tuple


def answer_1(input: List[str]) -> int:
all_reports = extract_reports(input)
safe_reports = calculate_safety(all_reports, failure_threshold=0)
return sum(safe_reports)


# def answer_2(input: List[str]) -> int:
# all_reports = extract_reports(input)
# safe_reports = calculate_safety(all_reports, failure_threshold=1) # noqa: F841
# return sum(safe_reports)


def extract_reports(input: Iterable[str], /) -> Iterator[List[int]]:
levels = [list(map(int, line.split())) for line in input]
yield from levels


def calculate_safety(input: Iterator[List[int]], /, failure_threshold: int = 0) -> Iterator[bool]:
for _, deltas, directions in filter_reports(input, failure_threshold):
delta_anomalies = [d for d in deltas if d == 0 or not -4 < d < 4]
safety_checks = [len(delta_anomalies) == 0, len(set(directions)) == 1]
yield all(safety_checks)


def filter_reports(
input: Iterator[List[int]], failure_threshold: int, /
) -> Iterator[Tuple[List[int], List[int], List[int]]]:
failure_count = 0
report = next(input)
while report:
try:
deltas = [reduce(sub, reversed(p)) for p in pairwise(report)]
directions = [-1 if d < 0 else +1 if d > 0 else 0 for d in deltas]
metadata = zip(deltas, directions)

# Tolerate bad levels up to a certain threshold
if failure_count < failure_threshold:
prevdir = None
for idx, (delta, curdir) in enumerate(metadata):
# Criteria: is neither an increase or a decrease
if delta == 0:
raise ValueError(idx)
# Criteria: is neither an increase or a decrease of at least 1 and at most 3
elif not -4 < delta < 4:
raise ValueError(idx)
# Criteria: is neither all increasing or all decreasing
elif prevdir is not None and prevdir != curdir:
raise ValueError(idx)
prevdir = curdir

# Return values
yield report, deltas, directions

# Pull next report or STOP
failure_count = 0
report = next(input)
except ValueError as err:
# Remove failure and try again
failure_count += 1
del report[err.args[0]]
continue
except StopIteration:
break
Empty file added src/aoc/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions tests/fixtures/2024/02-example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
--EXPECT--
2
--EXPECT--
-1
Loading
Loading