Day 2. Red-Nosed Reports¶
Part one¶
The nuclear fusion/fission plant engineers have provided us data reports and we need to validate levels in reports by provided rules. The rules are simple:
- All numbers in the list are increasing or decreasing
- Any two adjacent numbers in the list differ by at least 1 and at most 3
We need to count the number of the safe reports. This is pretty straightforward:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
Part two¶
Now one of our numbers (level) in each report can be skipped but only once. And we should define if the level is safe by the same rules as in part one. The task can be solved by the simple brute force approach. We can check if full row is safe and then check each of the new row variotions with the skipped level. If any of the variations is safe we can return true:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
But I have implemented another approach with Depth First Search on the state space.
The state is defined by the first, previous, current and next levels in the row. It also
has a flag to indicate if the level was skipped before. The isSafe
method here was
really hard for me to come around. Cause each skip can change the level trend and we need
to comply with the first change, but also the first element may be deleted and also has
the same value as the second one. Brrrr... All of these conditions are result of my
trial and error approach to understand the task flow.
Bruteforce approach is much simpler and more readable. I also think on this data size it is faster. But I've decided to keep the DFS approach for the sake of learning.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
When we have safe logic checking and all helpers solving the task is just DFS before we've reached the end of the row. The important part here is to initialize our queue with the skipped first level-row (will be checked later in case of not safe row) and the full row. This manual intervention is much more simple and robust than my previous attempts to calculate the first skipped level.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
Tags¶
- backtracking
- DFS
- state space