From 49f8c4268fa710d285990134bf908d1e7a127e52 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 2 Dec 2023 16:10:37 +0200 Subject: [PATCH] AOC2023: implement day2 --- userspace/aoc2023/day2/CMakeLists.txt | 22 +++++ userspace/aoc2023/day2/main.cpp | 116 ++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 userspace/aoc2023/day2/CMakeLists.txt create mode 100644 userspace/aoc2023/day2/main.cpp diff --git a/userspace/aoc2023/day2/CMakeLists.txt b/userspace/aoc2023/day2/CMakeLists.txt new file mode 100644 index 0000000000..91a76458d1 --- /dev/null +++ b/userspace/aoc2023/day2/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.26) + +project(aoc2023_day2 CXX) + +set(SOURCES + main.cpp +) + +add_executable(aoc2023_day2 ${SOURCES}) +target_compile_options(aoc2023_day2 PUBLIC -O2 -g) +target_link_libraries(aoc2023_day2 PUBLIC libc) + +add_dependencies(aoc2023_day2 libc-install ban-install) + +add_custom_target(aoc2023_day2-install + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/aoc2023_day2 ${BANAN_AOC2023_BIN}/day2 + DEPENDS aoc2023_day2 + DEPENDS aoc2023_always +) + +add_dependencies(aoc2023 aoc2023_day2) +add_dependencies(aoc2023-install aoc2023_day2-install) diff --git a/userspace/aoc2023/day2/main.cpp b/userspace/aoc2023/day2/main.cpp new file mode 100644 index 0000000000..957dd157cd --- /dev/null +++ b/userspace/aoc2023/day2/main.cpp @@ -0,0 +1,116 @@ +#include +#include +#include + +#define MAX(a, b) ((a) < (b)) ? (b) : (a) + +int parse_int_and_advance(const char*& ptr) +{ + int result = 0; + while (isdigit(*ptr)) + result = (result * 10) + (*ptr++ - '0'); + return result; +} + +int puzzle1(FILE* fp) +{ + int result = 0; + + char buffer[256]; + while (fgets(buffer, sizeof(buffer), fp)) + { + const char* ptr = buffer; + + if (strncmp("Game ", ptr, 5)) + continue; + ptr += 5; + + int id = parse_int_and_advance(ptr); + ptr += 2; + + bool valid = true; + while (*ptr && valid) + { + int count = parse_int_and_advance(ptr); + + ptr++; + if (strncmp(ptr, "red", 3) == 0) + valid = (count <= 12); + if (strncmp(ptr, "green", 5) == 0) + valid = (count <= 13); + if (strncmp(ptr, "blue", 4) == 0) + valid = (count <= 14); + + while (*ptr && !isdigit(*ptr)) + ptr++; + } + + if (valid) + result += id; + } + + return result; +} + +int puzzle2(FILE* fp) +{ int result = 0; + + char buffer[256]; + while (fgets(buffer, sizeof(buffer), fp)) + { + const char* ptr = buffer; + + if (strncmp("Game ", ptr, 5)) + continue; + ptr += 5; + + int id = parse_int_and_advance(ptr); + ptr += 2; + + int needed_red = 0; + int needed_green = 0; + int needed_blue = 0; + + while (*ptr) + { + int count = parse_int_and_advance(ptr); + + ptr++; + if (strncmp(ptr, "red", 3) == 0) + needed_red = MAX(needed_red, count); + if (strncmp(ptr, "green", 5) == 0) + needed_green = MAX(needed_green, count); + if (strncmp(ptr, "blue", 4) == 0) + needed_blue = MAX(needed_blue, count); + + while (*ptr && !isdigit(*ptr)) + ptr++; + } + + result += needed_red * needed_green * needed_blue; + } + + return result; +} + +int main(int argc, char** argv) +{ + const char* file_path = "/usr/share/aoc2023/day2_input.txt"; + if (argc >= 2) + file_path = argv[1]; + + FILE* fp = fopen(file_path, "r"); + if (fp == nullptr) + { + perror("fopen"); + return 1; + } + + printf("puzzle1: %d\n", puzzle1(fp)); + + fseek(fp, 0, SEEK_SET); + + printf("puzzle2: %d\n", puzzle2(fp)); + + fclose(fp); +}