Skip to content

Commit 818d4d3

Browse files
rudisimoRudy Puig
andauthored
Implement Day 2 (#4)
* chore: implement partial day 2 solution * chore: disable part 2 for now... --------- Co-authored-by: Rudy Puig <[email protected]>
1 parent 4d53fc1 commit 818d4d3

File tree

5 files changed

+1124
-0
lines changed

5 files changed

+1124
-0
lines changed

docs/2024/day-02.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Day 2: Red-Nosed Reports
2+
3+
## Part One
4+
5+
Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office.
6+
7+
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.
8+
9+
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.
10+
11+
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:
12+
13+
```
14+
7 6 4 2 1
15+
1 2 7 8 9
16+
9 7 6 2 1
17+
1 3 2 4 5
18+
8 6 4 4 1
19+
1 3 6 7 9
20+
```
21+
22+
This example data contains six reports each containing five levels.
23+
24+
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:
25+
26+
- The levels are either _all increasing_ or _all decreasing_.
27+
- Any two adjacent levels differ by _at least one_ and _at most three_.
28+
29+
In the example above, the reports can be found safe or unsafe by checking those rules:
30+
31+
- `7 6 4 2 1`: _Safe_ because the levels are all decreasing by 1 or 2.
32+
- `1 2 7 8 9`: _Unsafe_ because `2 7` is an increase of 5.
33+
- `9 7 6 2 1`: _Unsafe_ because `6 2` is a decrease of 4.
34+
- `1 3 2 4 5`: _Unsafe_ because `1 3` is increasing but `3 2` is decreasing.
35+
- `8 6 4 4 1`: _Unsafe_ because `4 4` is neither an increase or a decrease.
36+
- `1 3 6 7 9`: _Safe_ because the levels are all increasing by 1, 2, or 3.
37+
38+
So, in this example, `_2_` reports are _safe_.
39+
40+
Analyze the unusual data from the engineers. _How many reports are safe?_

src/aoc/2024/day_02.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from __future__ import annotations
2+
from functools import reduce
3+
from itertools import pairwise
4+
from operator import sub
5+
from typing import Iterable, Iterator, List, Tuple
6+
7+
8+
def answer_1(input: List[str]) -> int:
9+
all_reports = extract_reports(input)
10+
safe_reports = calculate_safety(all_reports, failure_threshold=0)
11+
return sum(safe_reports)
12+
13+
14+
# def answer_2(input: List[str]) -> int:
15+
# all_reports = extract_reports(input)
16+
# safe_reports = calculate_safety(all_reports, failure_threshold=1) # noqa: F841
17+
# return sum(safe_reports)
18+
19+
20+
def extract_reports(input: Iterable[str], /) -> Iterator[List[int]]:
21+
levels = [list(map(int, line.split())) for line in input]
22+
yield from levels
23+
24+
25+
def calculate_safety(input: Iterator[List[int]], /, failure_threshold: int = 0) -> Iterator[bool]:
26+
for _, deltas, directions in filter_reports(input, failure_threshold):
27+
delta_anomalies = [d for d in deltas if d == 0 or not -4 < d < 4]
28+
safety_checks = [len(delta_anomalies) == 0, len(set(directions)) == 1]
29+
yield all(safety_checks)
30+
31+
32+
def filter_reports(
33+
input: Iterator[List[int]], failure_threshold: int, /
34+
) -> Iterator[Tuple[List[int], List[int], List[int]]]:
35+
failure_count = 0
36+
report = next(input)
37+
while report:
38+
try:
39+
deltas = [reduce(sub, reversed(p)) for p in pairwise(report)]
40+
directions = [-1 if d < 0 else +1 if d > 0 else 0 for d in deltas]
41+
metadata = zip(deltas, directions)
42+
43+
# Tolerate bad levels up to a certain threshold
44+
if failure_count < failure_threshold:
45+
prevdir = None
46+
for idx, (delta, curdir) in enumerate(metadata):
47+
# Criteria: is neither an increase or a decrease
48+
if delta == 0:
49+
raise ValueError(idx)
50+
# Criteria: is neither an increase or a decrease of at least 1 and at most 3
51+
elif not -4 < delta < 4:
52+
raise ValueError(idx)
53+
# Criteria: is neither all increasing or all decreasing
54+
elif prevdir is not None and prevdir != curdir:
55+
raise ValueError(idx)
56+
prevdir = curdir
57+
58+
# Return values
59+
yield report, deltas, directions
60+
61+
# Pull next report or STOP
62+
failure_count = 0
63+
report = next(input)
64+
except ValueError as err:
65+
# Remove failure and try again
66+
failure_count += 1
67+
del report[err.args[0]]
68+
continue
69+
except StopIteration:
70+
break

src/aoc/__init__.py

Whitespace-only changes.

tests/fixtures/2024/02-example.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
7 6 4 2 1
2+
1 2 7 8 9
3+
9 7 6 2 1
4+
1 3 2 4 5
5+
8 6 4 4 1
6+
1 3 6 7 9
7+
--EXPECT--
8+
2
9+
--EXPECT--
10+
-1

0 commit comments

Comments
 (0)