Skip to content

Commit

Permalink
day 2 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
K20shores committed Dec 3, 2024
1 parent dc613fd commit bbfafa9
Show file tree
Hide file tree
Showing 4 changed files with 1,141 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/2024/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_subdirectory(lib)
# Problems

create_standard_test(NAME 2024_day1 SOURCES day1.cpp LIBRARIES aoc2024 YEAR 2024)
create_standard_test(NAME 2024_day2 SOURCES day2.cpp LIBRARIES aoc2024 YEAR 2024)

################################################################################
# Copy input data
Expand Down
8 changes: 4 additions & 4 deletions src/2024/day1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ Data parse()
while (std::getline(file, line))
{
std::istringstream buffer(line);
std::vector<double> line{std::istream_iterator<double>(buffer),
std::istream_iterator<double>()};
data.left.push_back(line[0]);
data.right.push_back(line[1]);
std::vector<int> lists{std::istream_iterator<int>(buffer),
std::istream_iterator<int>()};
data.left.push_back(lists[0]);
data.right.push_back(lists[1]);
}

return data;
Expand Down
136 changes: 136 additions & 0 deletions src/2024/day2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include <iostream>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <benchmark/benchmark.h>
#include <iterator>

struct Data {
std::vector<std::vector<int>> reports;
};

bool is_monotonic(const std::vector<int>& report) {
auto left = report.begin();
auto right = std::next(left, 1);
bool increasing = false;
bool decreasing = false;
while (left != report.end() && right != report.end()) {
bool _increasing = (*left < *right) && (*right > *left);
bool _decreasing = (*left > *right) && (*right < *left);
increasing |= _increasing;
decreasing |= _decreasing;

if (increasing && decreasing) return false;

if (_increasing || _decreasing)
{
int diff = std::abs(*left - *right);
if (!(diff >= 1 && diff <= 3)) {
return false;
}
}
else {
return false;
}
left = std::next(left, 1);
right = std::next(right, 1);
}
return true;
}

int part1(const Data &data)
{
int safe = 0;
for(auto& report: data.reports) {
if (is_monotonic(report)) {
safe += 1;
}
else {
}
}
return safe;
}

int part2(const Data &data)
{
return 0;
}

Data parse()
{
std::ifstream file(std::filesystem::path("inputs/day2.txt"));
if (!file.is_open())
{
throw std::runtime_error("file not found");
}
std::string line;
Data data;

while (std::getline(file, line))
{
std::istringstream buffer(line);
std::vector<int> report{std::istream_iterator<int>(buffer),
std::istream_iterator<int>()};
data.reports.push_back(report);
}

return data;
}

class BenchmarkFixture : public benchmark::Fixture
{
public:
static Data data;
};

Data BenchmarkFixture::data = parse();

BENCHMARK_DEFINE_F(BenchmarkFixture, Part1Benchmark)
(benchmark::State &state)
{
for (auto _ : state)
{
int s = part1(data);
benchmark::DoNotOptimize(s);
}
}

BENCHMARK_DEFINE_F(BenchmarkFixture, Part2Benchmark)
(benchmark::State &state)
{
for (auto _ : state)
{
int s = part2(data);
benchmark::DoNotOptimize(s);
}
}

BENCHMARK_REGISTER_F(BenchmarkFixture, Part1Benchmark);
BENCHMARK_REGISTER_F(BenchmarkFixture, Part2Benchmark);

int main(int argc, char **argv)
{
Data data = parse();

int answer1 = 0;
int answer2 = 0;

auto first = part1(data);
auto second = part2(data);

std::cout << "Part 1: " << first << std::endl;
std::cout << "Part 2: " << second << std::endl;

first != answer1 ? throw std::runtime_error("Part 1 incorrect") : nullptr;
second != answer2 ? throw std::runtime_error("Part 2 incorrect") : nullptr;

for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "--benchmark") {
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
return 0;
}
}
}
Loading

0 comments on commit bbfafa9

Please sign in to comment.