From 3a5a22511a78eb4d5774596a66ae30b73cf7f42f Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 3 Dec 2024 16:27:07 +0200 Subject: [PATCH] aoc2024: Implement day3 solution --- userspace/aoc2024/CMakeLists.txt | 1 + userspace/aoc2024/day3/CMakeLists.txt | 9 ++ userspace/aoc2024/day3/main.cpp | 152 ++++++++++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 userspace/aoc2024/day3/CMakeLists.txt create mode 100644 userspace/aoc2024/day3/main.cpp diff --git a/userspace/aoc2024/CMakeLists.txt b/userspace/aoc2024/CMakeLists.txt index 7bf62281..ccf5865a 100644 --- a/userspace/aoc2024/CMakeLists.txt +++ b/userspace/aoc2024/CMakeLists.txt @@ -1,6 +1,7 @@ set(AOC2024_PROJECTS day1 day2 + day3 full ) diff --git a/userspace/aoc2024/day3/CMakeLists.txt b/userspace/aoc2024/day3/CMakeLists.txt new file mode 100644 index 00000000..85dbdb4d --- /dev/null +++ b/userspace/aoc2024/day3/CMakeLists.txt @@ -0,0 +1,9 @@ +set(SOURCES + main.cpp +) + +add_executable(aoc2024_day3 ${SOURCES}) +banan_include_headers(aoc2024_day3 ban) +banan_link_library(aoc2024_day3 libc) + +install(TARGETS aoc2024_day3 OPTIONAL) diff --git a/userspace/aoc2024/day3/main.cpp b/userspace/aoc2024/day3/main.cpp new file mode 100644 index 00000000..88035f1b --- /dev/null +++ b/userspace/aoc2024/day3/main.cpp @@ -0,0 +1,152 @@ +#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) +{ + i64 result = 0; + + BAN::String full_input; + + { + char buffer[512]; + while (fgets(buffer, sizeof(buffer), fp)) + MUST(full_input.append(buffer)); + } + + result = 0; + for (size_t i = 0; i < full_input.size() - 4;) + { + if (!full_input.sv().substring(i).starts_with("mul(")) + { + i++; + continue; + } + + i += 4; + if (i >= full_input.size() || !isdigit(full_input[i])) + continue; + + i64 lhs = atoll(&full_input[i]); + while (i < full_input.size() && isdigit(full_input[i])) + i++; + + if (i >= full_input.size() || full_input[i] != ',') + continue; + + i++; + if (i >= full_input.size() || !isdigit(full_input[i])) + continue; + + int rhs = atoll(&full_input[i]); + while (i < full_input.size() && isdigit(full_input[i])) + i++; + + if (i >= full_input.size() || full_input[i] != ')') + continue; + + result += lhs * rhs; + } + + return result; +} + +i64 part2(FILE* fp) +{ + i64 result = 0; + + BAN::String full_input; + + { + char buffer[512]; + while (fgets(buffer, sizeof(buffer), fp)) + MUST(full_input.append(buffer)); + } + + bool state = true; + + result = 0; + for (size_t i = 0; i < full_input.size();) + { + if (full_input.sv().substring(i).starts_with("do()"_sv)) + { + state = true; + i += 4; + continue; + } + + if (full_input.sv().substring(i).starts_with("don't()"_sv)) + { + state = false; + i += 6; + continue; + } + + if (state == false || !full_input.sv().substring(i).starts_with("mul(")) + { + i++; + continue; + } + + i += 4; + if (i >= full_input.size() || !isdigit(full_input[i])) + continue; + + i64 lhs = atoll(&full_input[i]); + while (i < full_input.size() && isdigit(full_input[i])) + i++; + + if (i >= full_input.size() || full_input[i] != ',') + continue; + + i++; + if (i >= full_input.size() || !isdigit(full_input[i])) + continue; + + int rhs = atoll(&full_input[i]); + while (i < full_input.size() && isdigit(full_input[i])) + i++; + + if (i >= full_input.size() || full_input[i] != ')') + continue; + + result += lhs * rhs; + } + + return result; +} + +int main(int argc, char** argv) +{ + const char* file_path = "/usr/share/aoc2024/day3_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); +}