From 75f3b04e4fd14e6f2878472fb941bc66bb94133a Mon Sep 17 00:00:00 2001 From: jiriks74 Date: Mon, 23 Sep 2024 03:11:36 +0200 Subject: [PATCH] feat(02): Solve day 2 --- .gitignore | 7 +++ 02/.envrc | 1 + 02/.gitignore | 34 +++++---------- 02/default.nix | 12 +++++ 02/src/main.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++- 02/test.input | 5 +++ 6 files changed, 151 insertions(+), 24 deletions(-) create mode 100644 02/.envrc create mode 100644 02/default.nix create mode 100644 02/test.input diff --git a/.gitignore b/.gitignore index 0e7fb71..7d0f294 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,11 @@ +# ClangD +.cache + +# Nix files +.direnv + # Output folders +build/ output/ # Vimspector diff --git a/02/.envrc b/02/.envrc new file mode 100644 index 0000000..1d953f4 --- /dev/null +++ b/02/.envrc @@ -0,0 +1 @@ +use nix diff --git a/02/.gitignore b/02/.gitignore index bd461b5..9ae5e67 100644 --- a/02/.gitignore +++ b/02/.gitignore @@ -1,3 +1,15 @@ +# ClangD +.cache + +# Nix files +.direnv + +# Output folders +output/ + +# Vimspector +# .vimspector.json + # Prerequisites *.d @@ -50,25 +62,3 @@ modules.order Module.symvers Mkfile.old dkms.conf - -# Vscode -.vscode/ - -# CMake -CMakeLists.txt.user -CMakeCache.txt -CMakeFiles -CMakeScripts -Testing -Makefile -cmake_install.cmake -install_manifest.txt -compile_commands.json -.cache/ -CTestTestfile.cmake -_deps -build/ -lib/ -bin/ -*.swp - diff --git a/02/default.nix b/02/default.nix new file mode 100644 index 0000000..17a2804 --- /dev/null +++ b/02/default.nix @@ -0,0 +1,12 @@ +let + pkgs = import {}; +in +pkgs.mkShell { + packages = with pkgs; [ + # Choose the build tools that you need + gcc + gdb + cmake + ]; +} + diff --git a/02/src/main.c b/02/src/main.c index 70ba1e7..efb8133 100644 --- a/02/src/main.c +++ b/02/src/main.c @@ -6,20 +6,132 @@ * @author jiriks74 */ +#include #include +#include +#include +const int MAX_RED = 12; +const int MAX_GREEN = 13; +const int MAX_BLUE = 14; + +typedef struct { + int red; + int green; + int blue; +} GameShow; + +/** + * @brief Count how many cubes of each color were in a show + * @param *show A show of cubes in a game + * @return A count of all the cubes in a struct + */ +GameShow get_max_cube_counts(char *game) { + char *curr = game; + GameShow result = {0}; + + while (curr != NULL) { + char *end_show = strchr(curr, ';'); + + if (end_show != NULL) { + *end_show = '\0'; + } + + while (curr != NULL) { + int cube_count = atoi(curr); + + curr = strchr(curr, ' '); + curr += sizeof(char); + + if (*curr == 'r') { + if (cube_count > result.red) + result.red = cube_count; + } else if (*curr == 'g') { + if (cube_count > result.green) + result.green = cube_count; + } else if (*curr == 'b') { + if (cube_count > result.blue) + result.blue = cube_count; + } else { + result.red = -1; + result.green = -1; + result.blue = -1; + return result; + } + curr = strchr(curr, ','); + if (curr != NULL) + curr += sizeof(char) * 2; + } + + if (end_show != NULL) { + *end_show = ';'; + curr = end_show + sizeof(char) * 2; + } else + curr = end_show; + } + return result; +} + +/** + * @brief Count how many times were the cubes shown + * @return Number of shows or -1 when error occurs + */ +bool is_game_possible(char *game) { + // char *curr = strchr(game, ':'); + // curr += sizeof(char) * 2; + + // if (curr == NULL) { + // return false; + // } + + GameShow show_counts = get_max_cube_counts(game); + + if (show_counts.red > MAX_RED) + return false; + else if (show_counts.green > MAX_GREEN) + return false; + else if (show_counts.blue > MAX_BLUE) + return false; + + return true; +} + +#ifndef TESTING /** * @brief Main entry point * @param argc Number of command-line arguments. * @param argv Array of command-line arguments. */ -#ifndef TESTING int main(int argc, char *argv[]) #endif #ifdef TESTING int main_test(int argc, char *argv[]) #endif { - printf("Hello world!\n"); + FILE *input = fopen(argv[1], "r"); + + int possible_game_ids_sum = 0; + int sum_of_powers = 0; + + char line[512]; + while (fgets(line, sizeof(line), input)) { + if (*line != 'G') + break; + + int curr_game_id = atoi(line + sizeof("Game")); + + // if (is_game_possible(line)) { + if (is_game_possible(strchr(line, ':') + sizeof(char) * 2)) { + possible_game_ids_sum += curr_game_id; + // printf("Game %d is possible\n", curr_game_id); + } // else + // printf("Game %d is impossible\n", curr_game_id); + GameShow min_cubes = + get_max_cube_counts(strchr(line, ':') + sizeof(char) * 2); + sum_of_powers += min_cubes.red * min_cubes.green * min_cubes.blue; + } + + printf("Sum of possible game IDs: %d\n", possible_game_ids_sum); + printf("Sum of powers: %d\n", sum_of_powers); return 0; } diff --git a/02/test.input b/02/test.input new file mode 100644 index 0000000..295c36d --- /dev/null +++ b/02/test.input @@ -0,0 +1,5 @@ +Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green +Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue +Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red +Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red +Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green