aoc2025: Implement day2 solution
This commit is contained in:
parent
19c4f34ccb
commit
e8f1ba3722
|
|
@ -1,5 +1,6 @@
|
||||||
set(AOC2025_PROJECTS
|
set(AOC2025_PROJECTS
|
||||||
day1
|
day1
|
||||||
|
day2
|
||||||
full
|
full
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
set(SOURCES
|
||||||
|
main.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
add_executable(aoc2025_day2 ${SOURCES})
|
||||||
|
banan_include_headers(aoc2025_day2 ban)
|
||||||
|
banan_link_library(aoc2025_day2 libc)
|
||||||
|
|
||||||
|
install(TARGETS aoc2025_day2 OPTIONAL)
|
||||||
|
|
@ -0,0 +1,106 @@
|
||||||
|
#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;
|
||||||
|
|
||||||
|
i64 part1(FILE* fp)
|
||||||
|
{
|
||||||
|
const auto is_invalid_id =
|
||||||
|
[](u64 id) -> bool
|
||||||
|
{
|
||||||
|
u64 temp_id = id;
|
||||||
|
u64 repeat = 0;
|
||||||
|
u64 mult = 1;
|
||||||
|
|
||||||
|
while (repeat * mult + repeat < id)
|
||||||
|
{
|
||||||
|
repeat += (temp_id % 10) * mult;
|
||||||
|
temp_id /= 10;
|
||||||
|
mult *= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (temp_id < mult / 10)
|
||||||
|
return false;
|
||||||
|
return repeat * mult + repeat == id;
|
||||||
|
};
|
||||||
|
|
||||||
|
u64 result = 0;
|
||||||
|
|
||||||
|
u64 start, end;
|
||||||
|
while (fscanf(fp, "%" SCNu64 "-%" SCNu64 ",", &start, &end) >= 2)
|
||||||
|
for (u64 id = start; id <= end; id++)
|
||||||
|
if (is_invalid_id(id))
|
||||||
|
result += id;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
i64 part2(FILE* fp)
|
||||||
|
{
|
||||||
|
const auto is_invalid_id =
|
||||||
|
[](u64 id) -> bool
|
||||||
|
{
|
||||||
|
u64 temp_id = id;
|
||||||
|
u64 repeat = 0;
|
||||||
|
u64 mult = 1;
|
||||||
|
|
||||||
|
while (repeat * mult + repeat < id)
|
||||||
|
{
|
||||||
|
repeat += (temp_id % 10) * mult;
|
||||||
|
temp_id /= 10;
|
||||||
|
mult *= 10;
|
||||||
|
|
||||||
|
if (repeat < mult / 10)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
u64 attempt = repeat * mult + repeat;
|
||||||
|
while (attempt < id)
|
||||||
|
attempt = attempt * mult + repeat;
|
||||||
|
if (attempt == id)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
u64 result = 0;
|
||||||
|
|
||||||
|
u64 start, end;
|
||||||
|
while (fscanf(fp, "%" SCNu64 "-%" SCNu64 ",", &start, &end) >= 2)
|
||||||
|
for (u64 id = start; id <= end; id++)
|
||||||
|
if (is_invalid_id(id))
|
||||||
|
result += id;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
const char* file_path = "/usr/share/aoc2025/day2_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);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue