Skip to content

Commit

Permalink
2023 day5 x0
Browse files Browse the repository at this point in the history
  • Loading branch information
duchonic committed Dec 6, 2023
1 parent 06df4ff commit 28951bf
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions 2023/day05/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "help/log.h"
#include "help/plot.h"

#include <assert.h>

/*
Seed 79, soil 81, fertilizer 81, water 81, light 74, temperature 78, humidity 78, location 82.
Expand All @@ -32,16 +33,21 @@
Seed 13, soil 13, fertilizer 52, water 41, light 34, temperature 34, humidity 35, location 35.
*/

int64_t solve( std::array<int, 3> input, int64_t seed) {
int64_t solve_part( std::array<int64_t, 3> input, int64_t seed) {
int64_t returnValue = seed;

int64_t destination = input.at(0);
int64_t source = input.at(1);
int64_t length = input.at(2);

std::cout << "seed: " << seed << std::endl;
std::cout << "input: " << input.at(0) << " " << input.at(1) << " " << input.at(2) << std::endl;
if (seed >= source && seed <= source + length-1) {
returnValue = seed - source + destination;
int64_t offset = seed - source;
assert(offset >= 0);
returnValue = offset + destination;
}
assert(returnValue >= 0);

return returnValue;
}
Expand All @@ -51,11 +57,11 @@ int64_t solve( std::array<int, 3> input, int64_t seed) {
* @brief solves the problem
*/
static int64_t solve(std::vector<std::string> input, bool DoPart2) {
int64_t returnValue = 0;
int64_t returnValue = 100000000000;


std::vector<int> seeds;
std::vector<std::array<int, 3>> commands;
std::vector<int64_t> seeds;
std::vector<std::vector<std::array<int64_t, 3>>> commands_global;
std::vector<std::array<int64_t, 3>> commands;

/**
* commands will always be
Expand All @@ -75,35 +81,50 @@ static int64_t solve(std::vector<std::string> input, bool DoPart2) {
std::vector<std::string> split_seeds;
split_str(split.at(1), ' ', split_seeds);
for (auto seed : split_seeds) {
seeds.push_back(std::atoi(seed.c_str()));
seeds.push_back(std::atoll(seed.c_str()));
}
}
if (isdigit(split.at(0)[0])) {
else if (isdigit(split.at(0)[0])) {
std::cout << "line : " << line << std::endl;
std::vector<std::string> split_command;
split_str(split.at(0), ' ', split_command);

std::array<int, 3> command_array = {std::atoi(split_command.at(0).c_str()), std::atoi(split_command.at(1).c_str()), std::atoi(split_command.at(2).c_str())};
int64_t destination = std::atoll(split_command.at(0).c_str());
int64_t source = std::atoll(split_command.at(1).c_str());
int64_t length = std::atoll(split_command.at(2).c_str());
std::array<int64_t, 3> command_array = {destination, source, length};
commands.push_back(command_array);

std::cout << "command : " << command_array.at(0) << " " << command_array.at(1) << " " << command_array.at(2) << std::endl;
}
else {
//std::cout << "newline" << std::endl;
commands_global.push_back(commands);
commands.clear();
}
}
}
commands_global.push_back(commands);


std::cout << "seed: -> soil v -> fertilizer v -> water v -> light v -> temperature v -> hum v -> map" << std::endl;
for (auto seed : seeds) {

int calc_seed = seed;

std::cout << "seed: ";
for (auto command : commands) {

calc_seed = solve(command, calc_seed);
std::cout << " -> " << calc_seed;
int64_t calc_seed = seed;
for (auto commands : commands_global) {
for (auto command : commands) {

int64_t old_seed = calc_seed;
calc_seed = solve_part(command, calc_seed);
if (old_seed != calc_seed) {
break;
}
}
}
if (calc_seed < returnValue) {
returnValue = calc_seed;
std::cout << "calc_seed : " << calc_seed << std::endl;
}
std::cout << std::endl;
}

}

return returnValue;
}
Expand All @@ -114,7 +135,7 @@ int main() {
std::cout << "2023 day05 solve part 1" << std::endl;
int64_t part1 = solve(data, false);
std::cout << "part1 : " << part1 << std::endl;

return 0;
std::cout << "2023 day05 solve part 2" << std::endl;
int64_t part2 = solve(data, true);
std::cout << "part2 : " << part2 << std::endl;
Expand Down

0 comments on commit 28951bf

Please sign in to comment.