Skip to content

Commit 85c5665

Browse files
committed
Started Day 3
1 parent 2445735 commit 85c5665

File tree

9 files changed

+138
-23
lines changed

9 files changed

+138
-23
lines changed

include/advent_of_code/day_2.hxx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
#include "spdlog/spdlog.h"
1515

1616
namespace AdventOfCode24::Day2 {
17-
const int BUFFER_SIZE{1024};
1817
bool safe_pair(int val_1, int val_2, int direction);
19-
int check_intervals(const std::vector<int>& numbers);
20-
bool report_is_safe(std::istringstream& line);
18+
std::optional<int> check_intervals(const std::vector<int>& numbers);
19+
bool report_is_safe(std::istringstream& line, bool allow_dampening);
2120
std::vector<bool> check_reactor_safety(const std::filesystem::path& input_file, bool problem_dampening);
2221
};

include/advent_of_code/day_3.hxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include "spdlog/spdlog.h"
4+
#include <sstream>
5+
#include <vector>
6+
#include <regex>
7+
8+
namespace AdventOfCode24::Day3 {
9+
std::vector<std::pair<int, int>> parse_line(std::istringstream& line);
10+
};

solutions/data/day_3.txt

Lines changed: 6 additions & 0 deletions
Large diffs are not rendered by default.

src/day_2.cxx

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "advent_of_code/day_2.hxx"
22
#include "spdlog/spdlog.h"
3-
#include <string>
3+
#include <optional>
44

55
namespace 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;

src/day_3.cxx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "advent_of_code/day_3.hxx"
2+
#include "spdlog/spdlog.h"
3+
#include <regex>
4+
#include <stdexcept>
5+
#include <string>
6+
7+
namespace AdventOfCode24::Day3 {
8+
std::vector<std::pair<int, int>> parse_line(std::istringstream& line) {
9+
std::vector<std::pair<int, int>> operations;
10+
11+
const std::string line_str{line.str()};
12+
13+
const std::regex find_valid_mul_statements{"mul\\(-?\\d+,-?\\d+\\)"};
14+
const std::regex find_numbers{"-?\\d+"};
15+
16+
auto results_begin{std::sregex_iterator(line_str.begin(), line_str.end(), find_valid_mul_statements)};
17+
auto results_end {std::sregex_iterator()};
18+
19+
for(std::sregex_iterator iter{results_begin}; iter != results_end; ++iter) {
20+
const std::smatch match{*iter};
21+
const std::string match_str{match.str()};
22+
23+
auto numbers_begin{std::sregex_iterator(match_str.begin(), match_str.end(), find_numbers)};
24+
auto numbers_end {std::sregex_iterator()};
25+
26+
std::pair<int, int> arguments{-1000, -1000};
27+
28+
for(std::sregex_iterator n_iter{numbers_begin}; n_iter != numbers_end; ++n_iter) {
29+
const std::smatch n_match{*n_iter};
30+
int n_match_int{0};
31+
32+
try {
33+
n_match_int = std::stoi(match.str());
34+
} catch(std::invalid_argument& e) {
35+
spdlog::error("Could not parse " + n_match.str());
36+
return operations;
37+
}
38+
39+
if(arguments.first == -1000) arguments.first = n_match_int;
40+
else arguments.second = n_match_int;
41+
}
42+
43+
operations.push_back(arguments);
44+
}
45+
return operations;
46+
}
47+
};

tests/data/day_2_2.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@
2222
31 34 32 30 28 27 24 22
2323
75 77 72 70 69
2424
52 51 52 49 47 45
25-
8 9 10 11
25+
8 9 10 11
26+
8 7 8 10 13 15 17
27+
90 89 91 93 95 94

tests/data/day_3_1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))

tests/test_day_2.cxx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,24 @@
55
#include "spdlog/spdlog.h"
66

77
#include <filesystem>
8+
#include <sstream>
89

910
#ifndef ADVENT_OF_CODE_DATA
1011
#error "ADVENT_OF_CODE_DATA is not defined!"
1112
#endif
1213

14+
TEST(TestAOC, TestDay2pt2_fail_scenario_1) {
15+
spdlog::set_level(spdlog::level::debug);
16+
std::istringstream sstr(std::string("90 89 91 93 95 94"), std::ios::in);
17+
ASSERT_FALSE(AdventOfCode24::Day2::report_is_safe(sstr, true));
18+
}
19+
20+
TEST(TestAOC, TestDay2pt2_fail_scenario_2) {
21+
spdlog::set_level(spdlog::level::debug);
22+
std::istringstream sstr(std::string("1 1 1 1 1"), std::ios::in);
23+
ASSERT_FALSE(AdventOfCode24::Day2::report_is_safe(sstr, true));
24+
}
25+
1326
TEST(TestAOC, TestDay2pt1_file1) {
1427
spdlog::set_level(spdlog::level::debug);
1528
const std::filesystem::path input_file = std::filesystem::path(ADVENT_OF_CODE_DATA) / "day_2_1.txt";
@@ -29,11 +42,11 @@ TEST(TestAOC, TestDay2pt1_file2) {
2942

3043
const std::vector<bool> layer_safety = AdventOfCode24::Day2::check_reactor_safety(input_file, true);
3144

32-
ASSERT_EQ(layer_safety.size(), 25);
45+
ASSERT_EQ(layer_safety.size(), 27);
3346

3447
const int n_safe = std::count_if(layer_safety.begin(), layer_safety.end(), [](bool x){return x;});
3548

36-
ASSERT_EQ(n_safe, 24);
49+
ASSERT_EQ(n_safe, 25);
3750
}
3851

3952
TEST(TestAOC, TestDay2pt2) {

tests/test_day_3.cxx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <gtest/gtest.h>
2+
#include "advent_of_code/day_3.hxx"
3+
4+
5+
#include <filesystem>
6+
#include <sstream>
7+
#include <utility>
8+
#include <vector>
9+
10+
#ifndef ADVENT_OF_CODE_DATA
11+
#error "ADVENT_OF_CODE_DATA is not defined!"
12+
#endif
13+
14+
TEST(TestAOC, TestDay3pt1_line_parse) {
15+
std::istringstream sstr(std::string("Gmul(4,5)L3£9mul(34,)&3yMul(3,4)?"), std::ios::in);
16+
const std::vector<std::pair<int,int>> parsed{AdventOfCode24::Day3::parse_line(sstr)};
17+
18+
ASSERT_EQ(parsed.size(), 1);
19+
20+
ASSERT_EQ(parsed[0].first, 4);
21+
ASSERT_EQ(parsed[0].second, 5);
22+
}

0 commit comments

Comments
 (0)