diff --git a/userspace/aoc2024/CMakeLists.txt b/userspace/aoc2024/CMakeLists.txt index 0300c63976..4721973f1e 100644 --- a/userspace/aoc2024/CMakeLists.txt +++ b/userspace/aoc2024/CMakeLists.txt @@ -3,6 +3,7 @@ set(AOC2024_PROJECTS day2 day3 day4 + day5 full ) diff --git a/userspace/aoc2024/day5/CMakeLists.txt b/userspace/aoc2024/day5/CMakeLists.txt new file mode 100644 index 0000000000..c361c6cb3f --- /dev/null +++ b/userspace/aoc2024/day5/CMakeLists.txt @@ -0,0 +1,9 @@ +set(SOURCES + main.cpp +) + +add_executable(aoc2024_day5 ${SOURCES}) +banan_include_headers(aoc2024_day5 ban) +banan_link_library(aoc2024_day5 libc) + +install(TARGETS aoc2024_day5 OPTIONAL) diff --git a/userspace/aoc2024/day5/main.cpp b/userspace/aoc2024/day5/main.cpp new file mode 100644 index 0000000000..66ce9304e2 --- /dev/null +++ b/userspace/aoc2024/day5/main.cpp @@ -0,0 +1,109 @@ +#include +#include +#include + +#include +#include +#include + +using i8 = int8_t; +using i16 = int16_t; +using i32 = int32_t; +using i64 = int64_t; + +using u8 = uint8_t; +using u16 = uint16_t; +using u32 = uint32_t; +using u64 = uint64_t; + +i64 part1(FILE* fp) +{ + bool requirements[100][100] {}; + char buffer[128]; + + int a, b; + while (fgets(buffer, sizeof(buffer), fp) && sscanf(buffer, "%d|%d", &a, &b) == 2) + requirements[b][a] = true; + + i64 result = 0; + + while (fgets(buffer, sizeof(buffer), fp)) + { + BAN::Vector update; + for (size_t i = 0; isdigit(buffer[i]); i += 3) + MUST(update.push_back(atoi(&buffer[i]))); + + bool valid = true; + for (size_t i = 0; i < update.size() && valid; i++) + for (size_t j = i + 1; j < update.size() && valid; j++) + if (requirements[update[i]][update[j]]) + valid = false; + + if (valid) + result += update[update.size() / 2]; + } + + return result; +} + +i64 part2(FILE* fp) +{ + bool requirements[100][100] {}; + char buffer[128]; + + int a, b; + while (fgets(buffer, sizeof(buffer), fp) && sscanf(buffer, "%d|%d", &a, &b) == 2) + requirements[b][a] = true; + + i64 result = 0; + + while (fgets(buffer, sizeof(buffer), fp)) + { + BAN::Vector update; + for (size_t i = 0; isdigit(buffer[i]); i += 3) + MUST(update.push_back(atoi(&buffer[i]))); + + bool correct { true }; + for (size_t i = 0; i < update.size(); i++) + { + for (size_t j = i + 1; j < update.size(); j++) + { + if (requirements[update[i]][update[j]]) + { + BAN::swap(update[i], update[j]); + correct = false; + i--; + break; + } + } + } + + if (!correct) + result += update[update.size() / 2]; + } + + return result; +} + +int main(int argc, char** argv) +{ + const char* file_path = "/usr/share/aoc2024/day5_input.txt"; + + if (argc >= 2) + file_path = argv[1]; + + FILE* fp = fopen(file_path, "r"); + if (fp == nullptr) + { + perror("fopen"); + return 1; + } + + printf("part1: %" PRId64 "\n", part1(fp)); + + fseek(fp, 0, SEEK_SET); + + printf("part2: %" PRId64 "\n", part2(fp)); + + fclose(fp); +}