implemented d01l01 in c

This commit is contained in:
icaema 2023-12-02 15:06:24 -05:00
parent fab2bd250f
commit 7ff7127b45
8 changed files with 241 additions and 20 deletions

18
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "linux-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}

40
.vscode/launch.json vendored
View File

@ -1,15 +1,31 @@
{ {
// Use IntelliSense to learn about possible attributes. "version": "0.2.0",
// Hover to view descriptions of existing attributes. "configurations": [
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 {
"version": "0.2.0", "name": "Python: Debug Console",
"configurations": [ "type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole"
},
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": false,
"cwd": "/home/icaema/Public/advent-of-code/day-02/src",
"program": "/home/icaema/Public/advent-of-code/day-02/src/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{ {
"name": "Python: Debug Console", "description": "Enable pretty-printing for gdb",
"type": "python", "text": "-enable-pretty-printing",
"request": "launch", "ignoreFailures": true
"program": "${file}", }
"console": "internalConsole" ]
} }
] ]
} }

68
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,68 @@
{
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false,
"makefile.makefilePath": "day-02/Makefile",
"makefile.makeDirectory": "day-02",
"makefile.launchConfigurations": [
{
"cwd": "/home/icaema/Public/advent-of-code/day-02/bin",
"binaryPath": "/home/icaema/Public/advent-of-code/day-02/bin/part01",
"binaryArgs": []
}
]
}

View File

@ -1,16 +1,16 @@
CC=gcc CC=gcc
CFLAGS=-Wall CFLAGS=-Wall
run1: bin/part01 run1: clean bin/part01
cat data/p01data.txt | bin/part01 cat data/p01data.txt | bin/part01
run2: bin/part02 run2: clean bin/part02
cat data/p02data.txt | bin/part02 cat data/p02data.txt | bin/part02
test1: bin/part01 test1: clean bin/part01
cat data/p01test.txt | bin/part01 cat data/p01test.txt | bin/part01
test2: bin/part02 test2: clean bin/part02
cat data/p02test.txt | bin/part02 cat data/p02test.txt | bin/part02
bin/part01: bin/part01:

BIN
day-02/bin/part01 Executable file

Binary file not shown.

BIN
day-02/src/build/Debug/outDebug Executable file

Binary file not shown.

Binary file not shown.

View File

@ -1,25 +1,144 @@
#include <ctype.h>
#include <stdatomic.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define DEBUG
#ifdef DEBUG #ifdef DEBUG
#define debug_printf(...) do{ fprintf( stderr, __VA_ARGS__ ); } while( 0 ) #define debug_printf(...) do{ fprintf( stderr, __VA_ARGS__ ); } while( 0 )
#else #else
#define debug_printf(...) do{ } while ( 0 ) #define debug_printf(...) do{ } while ( 0 )
#endif #endif
#define LINE_MAX 90 #define LINE_MAX 160
#define GAME_MAX 101
#define GAME_MAX_HANDS 10
typedef struct Hand {
int red;
int green;
int blue;
int set;
} Hand;
typedef struct Game {
int id;
int num_hands;
Hand hands[GAME_MAX_HANDS];
Hand min_cubes;
int valid;
} Game;
Game gameArray[GAME_MAX];
Hand bag_limits = {12, 13, 14, 1};
//Takes a game as a param, returns if the game is valid 0 or 1
int validateGame(Game game, Hand limit){
if (game.id == 0) return 0;
if (game.num_hands == 0) return 0;
for (int i=0; i<game.num_hands; i++){
if (game.hands[i].red > limit.red) return 0;
if (game.hands[i].green > limit.green) return 0;
if (game.hands[i].blue > limit.blue) return 0;
}
return 1;
}
void genHandString(char* dest, Game game){
int offset = 0;
for (int i=0; i<game.num_hands; i++){
offset += sprintf(dest+offset, "{r:%02d, g:%02d, b:%02d}, ", game.hands[i].red, game.hands[i].green, game.hands[i].blue);
}
}
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
int linesProcessed = 0; int linesProcessed = 0;
char line[LINE_MAX]; char line[LINE_MAX];
//debug_printf("size of game: %dbytes\n", (int)sizeof(Game));
while (fgets(line, LINE_MAX, stdin) != NULL) { while (fgets(line, LINE_MAX, stdin) != NULL) {
int game_id;
//debug_printf("raw line: %s\n", line);
char id_buf[4];
int colon_index = (int)(strchr(line, ':') - line);
strncpy(id_buf, &line[5], colon_index-5);
game_id = atoi(id_buf);
//debug_printf("id_buf: -%s- game_id: %d\n", id_buf, game_id);
gameArray[linesProcessed] = (Game){game_id, 0, {(Hand){0,0,0,0}}, (Hand){0,0,0,0}, 1};
char raw_hand_buffer[LINE_MAX]; //make a buffer to hold the rest of the game info for parsing later
memset(raw_hand_buffer, '\0', sizeof(raw_hand_buffer));
int end_index = (int)(strchr(line, '\n') - line); //debug_printf("endIndex: %d\n", end_index);
strncpy(raw_hand_buffer, &line[colon_index+2], end_index-(colon_index+2));
//debug_printf("game_id: %d; raw_hand_buffer: ~~%s~~\n", game_id, raw_hand_buffer);
debug_printf("Reconstruct: ~~Game %d: %s~~\n", game_id, raw_hand_buffer);
int hand_index = 0;
char count_buf[10] = "";
memset(count_buf, '\0', sizeof(count_buf));
for (int i = 0; i<=strlen(raw_hand_buffer); i++){
char current_char = raw_hand_buffer[i];
//debug_printf("i: %d.%d, hand_index: %d; count_buf:~%s~; current_char: %c\n", i, strlen(raw_hand_buffer), hand_index, count_buf, current_char);
if (current_char == ';' || i == strlen(raw_hand_buffer)){
memset(count_buf, '\0', sizeof(count_buf));
gameArray[linesProcessed].hands[hand_index].set = 1;
gameArray[linesProcessed].num_hands = hand_index+1;
debug_printf("Commited game %d hand %d: {r:%d g:%d b:%d}\n", game_id, hand_index, gameArray[linesProcessed].hands[hand_index].red, gameArray[linesProcessed].hands[hand_index].green, gameArray[linesProcessed].hands[hand_index].blue);
hand_index++;
} else if (isdigit(current_char)){
strncat(count_buf, &current_char, 1);
} else if (isalpha(current_char)){
debug_printf("count_buf: %s; setting hand %c: %d\n", count_buf, current_char, atoi(count_buf));
if (current_char == 'r'){
gameArray[linesProcessed].hands[hand_index].red = atoi(count_buf);
i+=2;
} else if (current_char == 'g'){
gameArray[linesProcessed].hands[hand_index].green = atoi(count_buf);
i+=4;
} else if (current_char == 'b'){
gameArray[linesProcessed].hands[hand_index].blue = atoi(count_buf);
i+=3;
}
memset(count_buf, '\0', sizeof(count_buf));
}
}
linesProcessed++; linesProcessed++;
} }
printf("\n Ingested %d Lines\n", linesProcessed);
printf("\n Processed %d Lines\n", linesProcessed);
printf("===================================\n"); printf("===================================\n");
printf("Answer: \nXXXX\n");
int gamesProcessed = 0;
int sum_of_valid_games = 0;
for (int i=0; i<100; i++){
if (gameArray[i].id != 0){
gameArray[i].valid = validateGame(gameArray[i], bag_limits);
char hands_stringify[22*GAME_MAX_HANDS];
memset(hands_stringify, '\0', sizeof(hands_stringify));
genHandString(hands_stringify, gameArray[i]);
printf("Game( id=%03d, num_hands=%d, valid=%d, hands[]={ %s} )\n", gameArray[i].id, gameArray[i].num_hands, gameArray[i].valid, hands_stringify);
if (gameArray[i].valid == 1){
sum_of_valid_games+=gameArray[i].id;
}
gamesProcessed++;
}
}
printf("\n Processed %d Games\n", gamesProcessed);
printf("===================================\n");
printf("Answer: \n%d\n", sum_of_valid_games);
} }