11#include " advent_of_code/day_2.hxx"
22#include " spdlog/spdlog.h"
3- #include < string >
3+ #include < optional >
44
55namespace AdventOfCode24 ::Day2 {
66 bool safe_pair (int val_1, int val_2, int direction) {
@@ -26,7 +26,7 @@ namespace AdventOfCode24::Day2 {
2626 return true ;
2727 }
2828
29- int check_intervals (const std::vector<int >& numbers) {
29+ std::optional< int > check_intervals (const std::vector<int >& numbers) {
3030 std::vector<int > directions (numbers.size () - 1 );
3131
3232 std::transform (
@@ -45,27 +45,31 @@ namespace AdventOfCode24::Day2 {
4545
4646 spdlog::debug (" \t - Overall direction is " + std::string ((overall_dir < 0 ) ? " Negative" : " Positive" ));
4747
48- if (! overall_dir) {
48+ if (overall_dir == 0 ) {
4949 spdlog::debug (" \t - All intervals failed with value 0" );
50- return overall_dir ;
50+ return {} ;
5151 }
5252
5353 const int differences = std::count_if (
5454 directions.begin (),
5555 directions.end (),
5656 [&overall_dir](int x){
57- const bool x_forward{x < 0 };
58- const bool overall_dir_forward{overall_dir < 0 };
59- if (x_forward != overall_dir_forward) {
60- spdlog::debug (" \t - Mismatch in direction between interval " + std::to_string (x) + " and direction " + std::to_string (overall_dir_forward));
57+ if (x == 0 ) {
58+ spdlog::debug (" \t - Interval of zero found. " );
59+ return true ;
6160 }
62- return x_forward != overall_dir_forward;
61+ const int x_vec{x / std::abs (x)};
62+ const int overall_dir_vec{overall_dir / std::abs (overall_dir)};
63+ if (x_vec != overall_dir_vec) {
64+ spdlog::debug (" \t - Mismatch in direction between interval " + std::to_string (x) + " and direction " + std::to_string (overall_dir_vec));
65+ }
66+ return x_vec != overall_dir_vec;
6367 }
6468 );
6569
6670 if (differences > 0 ) {
6771 spdlog::debug (" \t - Intervals failed due to variance in direction." );
68- return 0 ;
72+ return {} ;
6973 }
7074
7175 return overall_dir;
@@ -90,27 +94,38 @@ namespace AdventOfCode24::Day2 {
9094 numbers.push_back (number);
9195 }
9296
93- int interval_check{check_intervals (numbers)};
97+ std::optional<int > interval_check{check_intervals (numbers)};
98+
99+ if (!interval_check.has_value ()) return false ;
94100
95- if (!interval_check && !allow_dampening) return false ;
101+ const std::optional< int > fail_index{ check_report_values (numbers, interval_check. value ())} ;
96102
97- const std::optional<int > fail_index{check_report_values (numbers, interval_check)};
103+ if (!fail_index.has_value ()) {
104+ if (interval_check == 0 ) return false ;
105+ return true ;
106+ }
98107
99- if (!fail_index.has_value ()) return true ;
100108 if (!allow_dampening) return false ;
101109
110+ spdlog::debug (" \t * Dampen lower bound" );
111+
102112 std::vector<int > dampened_first{numbers.begin (), numbers.end ()};
103113 dampened_first.erase (dampened_first.begin () + fail_index.value () - 1 );
104114 interval_check = check_intervals (dampened_first);
105115
106- const std::optional<int > damp_fail_index_first{check_report_values (dampened_first, interval_check)};
107- if (!damp_fail_index_first.has_value ()) return true ;
116+ if (interval_check.has_value ()) {
117+ const std::optional<int > damp_fail_index_first{check_report_values (dampened_first, interval_check.value ())};
118+ if (!damp_fail_index_first.has_value ()) return true ;
119+ }
108120
121+ spdlog::debug (" \t * Dampen upper bound" );
109122 std::vector<int > dampened_second{numbers.begin (), numbers.end ()};
110123 dampened_second.erase (dampened_second.begin () + fail_index.value ());
111124 interval_check = check_intervals (dampened_second);
112125
113- const std::optional<int > damp_fail_index_second{check_report_values (dampened_second, interval_check)};
126+ if (!interval_check.has_value ()) return false ;
127+
128+ const std::optional<int > damp_fail_index_second{check_report_values (dampened_second, interval_check.value ())};
114129 if (!damp_fail_index_second.has_value ()) return true ;
115130
116131 return false ;
0 commit comments