From ccb603d77fe2f66d79508cbcfa94558d5b770bbc Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 5 Dec 2024 08:16:34 +0200 Subject: [PATCH] aoc2024: Implement day5 solution --- userspace/aoc2024/CMakeLists.txt | 1 + userspace/aoc2024/day5/CMakeLists.txt | 9 +++ userspace/aoc2024/day5/main.cpp | 109 ++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 userspace/aoc2024/day5/CMakeLists.txt create mode 100644 userspace/aoc2024/day5/main.cpp diff --git a/userspace/aoc2024/CMakeLists.txt b/userspace/aoc2024/CMakeLists.txt index 0300c639..4721973f 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 00000000..c361c6cb --- /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 00000000..66ce9304 --- /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); +}