From 55c8a1598369d7e3ab0862dc18bae29eaa7ead53 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 25 Dec 2024 18:13:57 +0200 Subject: [PATCH] aoc2024: Implement day25 solution This was a nice AOC season. First time I fully completed it! I may still optimize my solutions as some of them are a bit slow... --- userspace/aoc2024/CMakeLists.txt | 1 + userspace/aoc2024/day25/CMakeLists.txt | 9 +++ userspace/aoc2024/day25/main.cpp | 80 ++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 userspace/aoc2024/day25/CMakeLists.txt create mode 100644 userspace/aoc2024/day25/main.cpp diff --git a/userspace/aoc2024/CMakeLists.txt b/userspace/aoc2024/CMakeLists.txt index 92996a09..e9d1d711 100644 --- a/userspace/aoc2024/CMakeLists.txt +++ b/userspace/aoc2024/CMakeLists.txt @@ -23,6 +23,7 @@ set(AOC2024_PROJECTS day22 day23 day24 + day25 full ) diff --git a/userspace/aoc2024/day25/CMakeLists.txt b/userspace/aoc2024/day25/CMakeLists.txt new file mode 100644 index 00000000..12ddc449 --- /dev/null +++ b/userspace/aoc2024/day25/CMakeLists.txt @@ -0,0 +1,9 @@ +set(SOURCES + main.cpp +) + +add_executable(aoc2024_day25 ${SOURCES}) +banan_include_headers(aoc2024_day25 ban) +banan_link_library(aoc2024_day25 libc) + +install(TARGETS aoc2024_day25 OPTIONAL) diff --git a/userspace/aoc2024/day25/main.cpp b/userspace/aoc2024/day25/main.cpp new file mode 100644 index 00000000..e0ae1ce1 --- /dev/null +++ b/userspace/aoc2024/day25/main.cpp @@ -0,0 +1,80 @@ +#include +#include + +#include +#include + +using i8 = int8_t; +using i16 = int16_t; +using i32 = int32_t; +using i64 = int64_t; +using isize = ssize_t; + +using u8 = uint8_t; +using u16 = uint16_t; +using u32 = uint32_t; +using u64 = uint64_t; +using usize = size_t; + +i64 part1(FILE* fp) +{ + BAN::Vector> locks; + BAN::Vector> keys; + + for (;;) + { + char buffer[6*7 + 1]; + if (fread(buffer, 1, sizeof(buffer), fp) < sizeof(buffer) - 1) + break; + + BAN::Array obj; + for (usize j = 1; j <= 5; j++) + for (usize i = 0; i < 5; i++) + obj[i] += (buffer[6 * j + i] == '#'); + + MUST((buffer[0] == '#' ? locks : keys).push_back(obj)); + } + + u32 result = 0; + for (auto lock : locks) + { + for (auto key : keys) + { + bool valid = true; + for (usize i = 0; i < 5 && valid; i++) + valid = (key[i] + lock[i] <= 5); + result += valid; + } + } + + return result; +} + +i64 part2(FILE* fp) +{ + (void)fp; + return -1; +} + +int main(int argc, char** argv) +{ + const char* file_path = "/usr/share/aoc2024/day25_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); +}