feat(02): Solve day 2
This commit is contained in:
parent
00e1df7490
commit
75f3b04e4f
7
.gitignore
vendored
7
.gitignore
vendored
@ -1,4 +1,11 @@
|
||||
# ClangD
|
||||
.cache
|
||||
|
||||
# Nix files
|
||||
.direnv
|
||||
|
||||
# Output folders
|
||||
build/
|
||||
output/
|
||||
|
||||
# Vimspector
|
||||
|
1
02/.envrc
Normal file
1
02/.envrc
Normal file
@ -0,0 +1 @@
|
||||
use nix
|
34
02/.gitignore
vendored
34
02/.gitignore
vendored
@ -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
|
||||
|
||||
|
12
02/default.nix
Normal file
12
02/default.nix
Normal file
@ -0,0 +1,12 @@
|
||||
let
|
||||
pkgs = import <nixpkgs> {};
|
||||
in
|
||||
pkgs.mkShell {
|
||||
packages = with pkgs; [
|
||||
# Choose the build tools that you need
|
||||
gcc
|
||||
gdb
|
||||
cmake
|
||||
];
|
||||
}
|
||||
|
116
02/src/main.c
116
02/src/main.c
@ -6,20 +6,132 @@
|
||||
* @author jiriks74
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
5
02/test.input
Normal file
5
02/test.input
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user