diff --git a/.vscode/bolerplate.code-snippets b/.vscode/bolerplate.code-snippets new file mode 100644 index 0000000..611ff54 --- /dev/null +++ b/.vscode/bolerplate.code-snippets @@ -0,0 +1,36 @@ +{ + // Place your aoc-2023 workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and + // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope + // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is + // used to trigger the snippet and the body will be expanded and inserted. Possible variables are: + // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. + // Placeholders with the same ids are connected. + // Example: + // "Print to console": { + // "scope": "javascript,typescript", + // "prefix": "log", + // "body": [ + // "console.log('$1');", + // "$2" + // ], + // "description": "Log output to console" + // } + "Boilerplate": { + "scope": "c", + "prefix": "bplate", + "body": [ + "#include ", + "#include ", + "#include \"libs/fileLoader.h\"", + "", + "int main()", + "{", + " char *filename = \"input\";", + " struct OpenFile *o_file = load_file_to_mem(filename);", + "", + " return 0;", + "}", + ], + "description": "Boilder plate AOC" + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 76f1d11..a752010 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -7,11 +7,12 @@ "args": [ "-fdiagnostics-color=always", "-g", - "${file}", + "puzzle1.c", "-o", "${fileDirname}/${fileBasenameNoExtension}", "libs/fileLoader.c", - "libs/strTools.c" + "libs/strTools.c", + "parser.c" ], "options": { "cwd": "${fileDirname}" diff --git a/day1/puzzle2/main.c b/day1/puzzle2/main.c index d0f7274..6270d6f 100644 --- a/day1/puzzle2/main.c +++ b/day1/puzzle2/main.c @@ -16,20 +16,22 @@ int two_char_two_interger(int a, int b) return num; } -int is_num(char *test_string, char *num_to_match, int mem_length) { +int is_num(char *test_string, char *num_to_match, int mem_length) +{ char *input_copy = malloc(mem_length); - + memcpy(input_copy, test_string, (size_t)mem_length); - if (strcmp(input_copy, num_to_match) == 0) { - return 0; + if (strcmp(input_copy, num_to_match) == 0) + { + return 0; } return 1; } int main() { - const char *inputFile = "/home/ben/Documents/Projects/C/advent-of-code/day1/puzzle1/input"; + const char *inputFile = "input"; int fd = open(inputFile, O_RDONLY); if (fd < 0) diff --git a/day4/libs b/day4/libs index 7cb0c1a..d4bda9b 120000 --- a/day4/libs +++ b/day4/libs @@ -1 +1 @@ -/home/ben/Documents/Projects/C/advent-of-code/libs \ No newline at end of file +../libs \ No newline at end of file diff --git a/day4/puzzle2.c b/day4/puzzle2.c index 2e506dd..b8680e9 100644 --- a/day4/puzzle2.c +++ b/day4/puzzle2.c @@ -4,22 +4,25 @@ #include "libs/fileLoader.h" #include "libs/strTools.h" +#define testing + #define MAX_STR_LEN 255 +#ifdef testing #define LineCount 7 #define OUR_LEN 5 #define WIN_LEN 8 - +#else // defaults -// #define LineCount 219 -// #define OUR_LEN 10 -// #define WIN_LEN 25 - +#define LineCount 219 +#define OUR_LEN 10 +#define WIN_LEN 25 +#endif struct ScratchCards { int cards[LineCount]; }; -struct Game +typedef struct Game { int ourNumbers[OUR_LEN]; int ourIterator; @@ -28,17 +31,17 @@ struct Game int gamesWon; int game_id; -}; +} Game; -struct Games +typedef struct Games { - struct Game games[LineCount]; + Game games[LineCount]; int iterator; -}; +} Games; -struct Game *lineParser(char *line) // , struct Games *games +Game *lineParser(char *line) // , Games *games { - struct Game *game = malloc(sizeof(struct Game)); + Game *game = malloc(sizeof(Game)); game->ourIterator = 0; game->winIterator = 0; game->gamesWon = 0; @@ -127,7 +130,7 @@ int main() int total = 0; // Load the input file into my structs - struct Games *games = malloc(sizeof(struct Games)); + Games *games = malloc(sizeof(Games)); games->iterator = 0; // char *line = (char *)malloc(MAX_STR_LEN); // Start new line variable? @@ -158,7 +161,7 @@ int main() cards->cards[i] = 1; } - for (int gameId = 0; gameId <= LineCount; gameId++) + for (int gameId = 0; gameId < LineCount - 1; gameId++) { if (gameId == games->iterator) { @@ -166,12 +169,12 @@ int main() } // cards->cards[gameId] += 1; Uneeded as we default them to 1 games won int winningGames = games->games[gameId].gamesWon; - int iterations = winningGames + cards->cards[gameId]; - for (int i = 1; i <= iterations; i++) + int iterations = winningGames * cards->cards[gameId]; + for (int i = 0; i <= iterations; i++) { - if (cards->cards[gameId + i] < LineCount - 1) + if (gameId + (i + 1) <= LineCount) { - cards->cards[gameId + i]++; + cards->cards[gameId + (i + 1)]++; } } // int winningGames = games->games[gameId].gamesWon; @@ -182,7 +185,7 @@ int main() } // Finish sad loop and cry - for (int i = 0; i < LineCount; i++) + for (int i = 0; i <= LineCount; i++) { total += cards->cards[i]; } diff --git a/day5/input b/day5/input new file mode 100644 index 0000000..bd902a4 --- /dev/null +++ b/day5/input @@ -0,0 +1,33 @@ +seeds: 79 14 55 13 + +seed-to-soil map: +50 98 2 +52 50 48 + +soil-to-fertilizer map: +0 15 37 +37 52 2 +39 0 15 + +fertilizer-to-water map: +49 53 8 +0 11 42 +42 0 7 +57 7 4 + +water-to-light map: +88 18 7 +18 25 70 + +light-to-temperature map: +45 77 23 +81 45 19 +68 64 13 + +temperature-to-humidity map: +0 69 1 +1 0 69 + +humidity-to-location map: +60 56 37 +56 93 4 \ No newline at end of file diff --git a/day5/input_test b/day5/input_test new file mode 100644 index 0000000..bd902a4 --- /dev/null +++ b/day5/input_test @@ -0,0 +1,33 @@ +seeds: 79 14 55 13 + +seed-to-soil map: +50 98 2 +52 50 48 + +soil-to-fertilizer map: +0 15 37 +37 52 2 +39 0 15 + +fertilizer-to-water map: +49 53 8 +0 11 42 +42 0 7 +57 7 4 + +water-to-light map: +88 18 7 +18 25 70 + +light-to-temperature map: +45 77 23 +81 45 19 +68 64 13 + +temperature-to-humidity map: +0 69 1 +1 0 69 + +humidity-to-location map: +60 56 37 +56 93 4 \ No newline at end of file diff --git a/day5/libs b/day5/libs new file mode 120000 index 0000000..d4bda9b --- /dev/null +++ b/day5/libs @@ -0,0 +1 @@ +../libs \ No newline at end of file diff --git a/day5/out.list b/day5/out.list new file mode 100644 index 0000000..ea56782 --- /dev/null +++ b/day5/out.list @@ -0,0 +1,11 @@ +"#include ", +"#include ", +"#include "libs/fileLoader.h"", +"", +"int main()", +"{", +" char *filename = "input";", +" struct OpenFile *o_file = load_file_to_mem(filename);", +"", +" return 0;", +"}", diff --git a/day5/parser.c b/day5/parser.c new file mode 100644 index 0000000..71b9e99 --- /dev/null +++ b/day5/parser.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include "libs/fileLoader.h" +#include "structs.h" + +#define MAX_STR_LEN 1024 + +const char *SEED_TXT = "seeds"; +const char *SEEDTOSOIL_TXT = "seed-to-soil map:"; +const char *SOILTOFERI_TXT = "soil-to-fertilizer map"; +const char *FERTITOWATER_TXT = "fertilizer-to-water map"; +const char *WATERTOLIGHT_TXT = "water-to-light map"; +const char *LIGHTTOTEMP_TXT = "light-to-temperature map"; +const char *TEMPTOHUMID_TXT = "temperature-to-humidity map"; +const char *HUMIDTOLOC_TXT = "humidity-to-location map"; + +Almanac *parseFile(OpenFile *file) +{ + Almanac *data = (Almanac *)malloc(sizeof(Almanac)); + for (int iterator = 0; iterator < file->fileSize; iterator++) + { + // Initialize the data + data->seeds = (Seeds *)malloc(sizeof(Seeds)); + data->seedtosoil = (SeedToSoilMap *)malloc(sizeof(SeedToSoilMap)); + data->soiltoferti = (SoilToFertiMap *)malloc(sizeof(SoilToFertiMap)); + data->ferttowater = (FertiToWaterMap *)malloc(sizeof(FertiToWaterMap)); + data->watertplight = (WaterToLightMap *)malloc(sizeof(WaterToLightMap)); + data->lighttotemp = (LightToTempMap *)malloc(sizeof(LightToTempMap)); + data->temptohumid = (TempToHumidMap *)malloc(sizeof(TempToHumidMap)); + data->humidtoloc = (HumidToLocMap *)malloc(sizeof(HumidToLocMap)); + + const char DELIM[2] = ":"; + char *token; + char *save_ptr; + + for (int i = 0; i < file->fileSize; i++) + { + char *tmpString = (char *)malloc(MAX_STR_LEN); + if (file->fileData[i] == '\n') + { + token = strtok_r(tmpString, DELIM, &save_ptr); + continue; + } + tmpString[i] = file->fileData[i]; + } + + // const char NEWLN_DELIM[2] = "\n"; + // char *line_token; + // char *line_save_ptr; + + // line_token = strtok_r(file->fileData, NEWLN_DELIM, &line_save_ptr); + // while (line_token != NULL) + // { + // if (strcmp(line_token, SEED_TXT) == 0) + // { + // printf("We looking at seeds"); + // } + + // line_token = strtok_r(NULL, file->fileData, &line_save_ptr); + // } + } + + return data; +} \ No newline at end of file diff --git a/day5/parser.h b/day5/parser.h new file mode 100644 index 0000000..efd77b5 --- /dev/null +++ b/day5/parser.h @@ -0,0 +1,9 @@ +#ifndef fileparser +#define fileparser +#include "structs.h" +#include "parser.h" +#include "libs/fileLoader.h" + +Almanac *parseFile(OpenFile *file); + +#endif \ No newline at end of file diff --git a/day5/puzze1 b/day5/puzze1 new file mode 100755 index 0000000..7afad4c Binary files /dev/null and b/day5/puzze1 differ diff --git a/day5/puzzle1.c b/day5/puzzle1.c new file mode 100644 index 0000000..0ff09d3 --- /dev/null +++ b/day5/puzzle1.c @@ -0,0 +1,23 @@ +#include +#include +#include "structs.h" +#include "libs/fileLoader.h" +#include "parser.h" + +#define testing + +#ifdef testing +#define FNAME "input_test" +#else +#define FNAME "input" +#endif + +int main() +{ + char *filename = FNAME; + struct OpenFile *o_file = load_file_to_mem(filename); + + Almanac *data = parseFile(o_file); + + return 0; +} \ No newline at end of file diff --git a/day5/puzzle2.c b/day5/puzzle2.c new file mode 100644 index 0000000..13387a3 --- /dev/null +++ b/day5/puzzle2.c @@ -0,0 +1,11 @@ +#include +#include +#include "libs/fileLoader.h" + +int main() +{ + char *filename = "input"; + struct OpenFile *o_file = load_file_to_mem(filename); + + return 0; +} \ No newline at end of file diff --git a/day5/structs.c b/day5/structs.c new file mode 100644 index 0000000..e69de29 diff --git a/day5/structs.h b/day5/structs.h new file mode 100644 index 0000000..1fb8fe6 --- /dev/null +++ b/day5/structs.h @@ -0,0 +1,72 @@ +#ifndef structybois +#define structybois + +typedef struct Maps +{ + int *nums; +} Maps; + +typedef struct Seeds +{ + Maps *nums; +} Seeds; + +typedef struct SeedToSoilMap +{ + Maps *first; + Maps *second; +} SeedToSoilMap; + +typedef struct SoilToFertiMap +{ + Maps *first; + Maps *second; + Maps *third; +} SoilToFertiMap; + +typedef struct FertiToWaterMap +{ + Maps *first; + Maps *second; + Maps *third; + Maps *fourth; +} FertiToWaterMap; + +typedef struct WaterToLightMap +{ + Maps *irst; + Maps *second; +} WaterToLightMap; + +typedef struct LightToTempMap +{ + Maps *first; + Maps *second; + Maps *third; +} LightToTempMap; + +typedef struct TempToHumidMap +{ + Maps *first; + Maps *second; +} TempToHumidMap; + +typedef struct HumidToLocMap +{ + Maps *first; + Maps *second; +} HumidToLocMap; + +typedef struct Almanac +{ + Seeds *seeds; + SeedToSoilMap *seedtosoil; + SoilToFertiMap *soiltoferti; + FertiToWaterMap *ferttowater; + WaterToLightMap *watertplight; + LightToTempMap *lighttotemp; + TempToHumidMap *temptohumid; + HumidToLocMap *humidtoloc; +} Almanac; + +#endif \ No newline at end of file diff --git a/libs/fileLoader.h b/libs/fileLoader.h index 666f5cc..94120e6 100644 --- a/libs/fileLoader.h +++ b/libs/fileLoader.h @@ -1,10 +1,10 @@ #ifndef fileloader #define fileloader -struct OpenFile +typedef struct OpenFile { char *fileData; int fileSize; -}; +} OpenFile; struct OpenFile *load_file_to_mem(char *filename); int unmap_file(char *ptr, char *filename);