-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import numpy | ||
|
||
from . import SeparateRunner | ||
|
||
|
||
def _safe(nums: numpy.ndarray) -> bool: | ||
steps = nums[1:] - nums[:-1] | ||
|
||
if numpy.all(steps > 0): | ||
return numpy.all((steps >= 1) & (steps <= 3)) | ||
elif numpy.all(steps < 0): | ||
return numpy.all((steps <= -1) & (steps >= -3)) | ||
else: | ||
return False | ||
|
||
|
||
def is_safe(line: str) -> bool: | ||
nums = numpy.fromstring(line, dtype=numpy.int32, sep=" ") | ||
|
||
return _safe(nums) | ||
|
||
|
||
def is_savable(line: str) -> bool: | ||
nums = numpy.fromstring(line, dtype=numpy.int32, sep=" ") | ||
|
||
return any( | ||
_safe(numpy.concatenate((nums[:i], nums[i + 1 :]), axis=None)) | ||
for i in range(len(nums)) | ||
) | ||
|
||
|
||
class DayRunner(SeparateRunner): | ||
@classmethod | ||
def part1(cls, data: str) -> int: | ||
lines = data.strip().split("\n") | ||
|
||
safe = sum(1 for line in lines if is_safe(line)) | ||
|
||
return safe | ||
|
||
@classmethod | ||
def part2(cls, data: str) -> int: | ||
lines = data.strip().split("\n") | ||
|
||
safe = sum(1 for line in lines if is_savable(line)) | ||
|
||
return safe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import os | ||
|
||
from aoc.days.day2 import DayRunner, is_savable | ||
|
||
|
||
def get_data() -> str: | ||
sample = os.path.dirname(__file__) + "/samples/02.txt" | ||
with open(sample, mode="rt", encoding="utf-8") as f: | ||
return f.read() | ||
|
||
|
||
def test_individual_samples() -> None: | ||
assert is_savable("7 6 4 2 1") | ||
assert not is_savable("1 2 7 8 9") | ||
assert not is_savable("9 7 6 2 1") | ||
assert is_savable("1 3 2 4 5") | ||
assert is_savable("8 6 4 4 1") | ||
assert is_savable("1 3 6 7 9") | ||
|
||
|
||
def test_sample_part1() -> None: | ||
assert DayRunner.part1(get_data()) == 2 | ||
|
||
|
||
def test_sample_part2() -> None: | ||
assert DayRunner.part2(get_data()) == 4 |