Skip to content

Commit

Permalink
day 7 part 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
K20shores committed Dec 8, 2024
1 parent bdc703f commit b767b8d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ build
inputs
inputs
.vscode
.DS_Store
2 changes: 1 addition & 1 deletion 2024/cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ FetchContent_Declare(

FetchContent_MakeAvailable(benchmark)

find_package(OpenMP REQUIRED)
# find_package(OpenMP REQUIRED)
79 changes: 62 additions & 17 deletions 2024/src/day7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,81 @@
#include <vector>
#include <benchmark/benchmark.h>
#include <aoc/2024/split.h>
#include <functional>

struct Data {
std::vector<long long> targets;
std::vector<std::vector<int>> nums;
};

template <typename BinaryOp>
bool get_value(long long target, long long acc, std::vector<int> nums, BinaryOp op) {
bool is_possible(long long target, long long acc, std::vector<int> nums) {
if (nums.size() == 0) {
return acc == target;
}

long long first = nums[0];
std::vector<int> rest{nums.begin()+1, nums.end()};

bool plus_result = is_possible(target, acc + first, rest);
bool multiply_result = is_possible(target, acc * first, rest);

return plus_result || multiply_result;
}

template <class T>
int numDigits(T number)
{
int digits = 0;
if (number < 0) digits = 1;
while (number) {
number /= 10;
digits++;
}
return digits;
}

if (nums.size() == 1) {
long long result = op(acc, first);
return result == target;
bool is_possible2(long long target, long long acc, std::vector<int> nums) {
if (nums.size() == 0) {
return acc == target;
}

long long first = nums[0];
std::vector<int> rest{nums.begin()+1, nums.end()};
long long result = op(first, acc);

if (get_value(target, acc, rest, std::plus))
int num_digits = numDigits(first);

bool plus_result = is_possible2(target, acc + first, rest);
bool multiply_result = is_possible2(target, acc * first, rest);
bool concat_result = is_possible2(target, acc * std::pow(10, num_digits) + first, rest);

return plus_result || multiply_result || concat_result;
}

int part1(const Data &data)
long long part1(const Data &data)
{
return 0;
long long sum = 0;
for(size_t i = 0; i < data.targets.size(); ++i) {
long long target = data.targets[i];
std::vector<int> nums = data.nums[i];

if (is_possible(target, 0, nums)) {
sum += target;
}
}
return sum;
}

int part2(const Data &data)
long long part2(const Data &data)
{
return 0;
long long sum = 0;
for(size_t i = 0; i < data.targets.size(); ++i) {
long long target = data.targets[i];
std::vector<int> nums = data.nums[i];

if (is_possible2(target, 0, nums)) {
sum += target;
}
}
return sum;
}

Data parse()
Expand Down Expand Up @@ -85,15 +130,15 @@ BENCHMARK_DEFINE_F(BenchmarkFixture, Part2Benchmark)
}
}

BENCHMARK_REGISTER_F(BenchmarkFixture, Part1Benchmark);
BENCHMARK_REGISTER_F(BenchmarkFixture, Part2Benchmark);
BENCHMARK_REGISTER_F(BenchmarkFixture, Part1Benchmark)->Unit(benchmark::kSecond);
BENCHMARK_REGISTER_F(BenchmarkFixture, Part2Benchmark)->Unit(benchmark::kSecond);

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

int answer1 = 0;
int answer2 = 0;
long long answer1 = 10741443549536;
long long answer2 = 500335179214836;

auto first = part1(data);
std::cout << "Part 1: " << first << std::endl;
Expand All @@ -111,4 +156,4 @@ int main(int argc, char **argv)
return 0;
}
}
}
}
4 changes: 2 additions & 2 deletions 2024/src/day_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ BENCHMARK_DEFINE_F(BenchmarkFixture, Part2Benchmark)
}
}

BENCHMARK_REGISTER_F(BenchmarkFixture, Part1Benchmark);
BENCHMARK_REGISTER_F(BenchmarkFixture, Part2Benchmark);
BENCHMARK_REGISTER_F(BenchmarkFixture, Part1Benchmark)->Unit(benchmark::kSecond);
BENCHMARK_REGISTER_F(BenchmarkFixture, Part2Benchmark)->Unit(benchmark::kSecond);

int main(int argc, char **argv)
{
Expand Down
2 changes: 1 addition & 1 deletion 2024/src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ target_sources(aoc2024

target_include_directories(aoc2024 PUBLIC ${PROJECT_SOURCE_DIR}/include)

target_link_libraries(aoc2024 PUBLIC OpenMP::OpenMP_CXX)
# target_link_libraries(aoc2024 PUBLIC OpenMP::OpenMP_CXX)

0 comments on commit b767b8d

Please sign in to comment.