From edeb667eadd563d229855a77837fa57ec7edf60f Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 12 Dec 2025 06:30:03 +0200 Subject: [PATCH] aoc2025: Implement day5 solution --- userspace/aoc2025/CMakeLists.txt | 1 + userspace/aoc2025/day5/CMakeLists.txt | 9 +++ userspace/aoc2025/day5/main.cpp | 106 ++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 userspace/aoc2025/day5/CMakeLists.txt create mode 100644 userspace/aoc2025/day5/main.cpp diff --git a/userspace/aoc2025/CMakeLists.txt b/userspace/aoc2025/CMakeLists.txt index fc6013e2..6a8146cb 100644 --- a/userspace/aoc2025/CMakeLists.txt +++ b/userspace/aoc2025/CMakeLists.txt @@ -3,6 +3,7 @@ set(AOC2025_PROJECTS day2 day3 day4 + day5 full ) diff --git a/userspace/aoc2025/day5/CMakeLists.txt b/userspace/aoc2025/day5/CMakeLists.txt new file mode 100644 index 00000000..236105e5 --- /dev/null +++ b/userspace/aoc2025/day5/CMakeLists.txt @@ -0,0 +1,9 @@ +set(SOURCES + main.cpp +) + +add_executable(aoc2025_day5 ${SOURCES}) +banan_include_headers(aoc2025_day5 ban) +banan_link_library(aoc2025_day5 libc) + +install(TARGETS aoc2025_day5 OPTIONAL) diff --git a/userspace/aoc2025/day5/main.cpp b/userspace/aoc2025/day5/main.cpp new file mode 100644 index 00000000..37e07736 --- /dev/null +++ b/userspace/aoc2025/day5/main.cpp @@ -0,0 +1,106 @@ +#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; + +struct Range +{ + u64 min; + u64 max; +}; + +static BAN::Vector parse_ranges(FILE* fp) +{ + BAN::Vector ranges; + + u64 min, max; + ungetc('\n', fp); + while (fscanf(fp, "\n%" SCNu64 "-%" SCNu64, &min, &max) == 2) + MUST(ranges.emplace_back(min, max)); + + return ranges; +} + +i64 part1(FILE* fp) +{ + auto ranges = parse_ranges(fp); + + i64 result = 0; + + u64 value; + while (fscanf(fp, "%" SCNu64, &value) == 1) + { + bool is_fresh = false; + for (const auto range : ranges) + if (range.min <= value && value <= range.max) + is_fresh = true; + result += is_fresh; + } + + return result; +} + +i64 part2(FILE* fp) +{ + auto ranges = parse_ranges(fp); + + BAN::sort::sort(ranges.begin(), ranges.end(), + [](auto lhs, auto rhs) -> bool + { + return lhs.min < rhs.min; + } + ); + + for (size_t i = 0; i < ranges.size() - 1; i++) + { + auto& lhs = ranges[i]; + auto& rhs = ranges[i + 1]; + if (lhs.min > rhs.max || rhs.min > lhs.max) + continue; + + lhs.min = BAN::Math::min(lhs.min, rhs.min); + lhs.max = BAN::Math::max(lhs.max, rhs.max); + ranges.remove(i + 1); + + i--; + } + + i64 result = 0; + for (auto range : ranges) + result += range.max - range.min + 1; + return result; +} + +int main(int argc, char** argv) +{ + const char* file_path = "/usr/share/aoc2025/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); +}