aoc2025: Implement day5 solution

This commit is contained in:
Bananymous 2025-12-12 06:30:03 +02:00
parent db2aa495b8
commit edeb667ead
3 changed files with 116 additions and 0 deletions

View File

@ -3,6 +3,7 @@ set(AOC2025_PROJECTS
day2 day2
day3 day3
day4 day4
day5
full full
) )

View File

@ -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)

View File

@ -0,0 +1,106 @@
#include <BAN/Vector.h>
#include <BAN/Sort.h>
#include <inttypes.h>
#include <stdio.h>
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<Range> parse_ranges(FILE* fp)
{
BAN::Vector<Range> 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);
}