This commit is contained in:
Benjamyn Love 2023-12-25 11:39:35 +12:00
parent 1ef0d29c9f
commit 4819b63b8d
17 changed files with 329 additions and 29 deletions

36
.vscode/bolerplate.code-snippets vendored Normal file
View File

@ -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 <stdio.h>",
"#include <stdlib.h>",
"#include \"libs/fileLoader.h\"",
"",
"int main()",
"{",
" char *filename = \"input\";",
" struct OpenFile *o_file = load_file_to_mem(filename);",
"",
" return 0;",
"}",
],
"description": "Boilder plate AOC"
}
}

5
.vscode/tasks.json vendored
View File

@ -7,11 +7,12 @@
"args": [ "args": [
"-fdiagnostics-color=always", "-fdiagnostics-color=always",
"-g", "-g",
"${file}", "puzzle1.c",
"-o", "-o",
"${fileDirname}/${fileBasenameNoExtension}", "${fileDirname}/${fileBasenameNoExtension}",
"libs/fileLoader.c", "libs/fileLoader.c",
"libs/strTools.c" "libs/strTools.c",
"parser.c"
], ],
"options": { "options": {
"cwd": "${fileDirname}" "cwd": "${fileDirname}"

View File

@ -16,20 +16,22 @@ int two_char_two_interger(int a, int b)
return num; 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); char *input_copy = malloc(mem_length);
memcpy(input_copy, test_string, (size_t)mem_length); memcpy(input_copy, test_string, (size_t)mem_length);
if (strcmp(input_copy, num_to_match) == 0) { if (strcmp(input_copy, num_to_match) == 0)
return 0; {
return 0;
} }
return 1; return 1;
} }
int main() 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); int fd = open(inputFile, O_RDONLY);
if (fd < 0) if (fd < 0)

View File

@ -1 +1 @@
/home/ben/Documents/Projects/C/advent-of-code/libs ../libs

View File

@ -4,22 +4,25 @@
#include "libs/fileLoader.h" #include "libs/fileLoader.h"
#include "libs/strTools.h" #include "libs/strTools.h"
#define testing
#define MAX_STR_LEN 255 #define MAX_STR_LEN 255
#ifdef testing
#define LineCount 7 #define LineCount 7
#define OUR_LEN 5 #define OUR_LEN 5
#define WIN_LEN 8 #define WIN_LEN 8
#else
// defaults // defaults
// #define LineCount 219 #define LineCount 219
// #define OUR_LEN 10 #define OUR_LEN 10
// #define WIN_LEN 25 #define WIN_LEN 25
#endif
struct ScratchCards struct ScratchCards
{ {
int cards[LineCount]; int cards[LineCount];
}; };
struct Game typedef struct Game
{ {
int ourNumbers[OUR_LEN]; int ourNumbers[OUR_LEN];
int ourIterator; int ourIterator;
@ -28,17 +31,17 @@ struct Game
int gamesWon; int gamesWon;
int game_id; int game_id;
}; } Game;
struct Games typedef struct Games
{ {
struct Game games[LineCount]; Game games[LineCount];
int iterator; 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->ourIterator = 0;
game->winIterator = 0; game->winIterator = 0;
game->gamesWon = 0; game->gamesWon = 0;
@ -127,7 +130,7 @@ int main()
int total = 0; int total = 0;
// Load the input file into my structs // Load the input file into my structs
struct Games *games = malloc(sizeof(struct Games)); Games *games = malloc(sizeof(Games));
games->iterator = 0; games->iterator = 0;
// char *line = (char *)malloc(MAX_STR_LEN); // Start new line variable? // char *line = (char *)malloc(MAX_STR_LEN); // Start new line variable?
@ -158,7 +161,7 @@ int main()
cards->cards[i] = 1; cards->cards[i] = 1;
} }
for (int gameId = 0; gameId <= LineCount; gameId++) for (int gameId = 0; gameId < LineCount - 1; gameId++)
{ {
if (gameId == games->iterator) if (gameId == games->iterator)
{ {
@ -166,12 +169,12 @@ int main()
} }
// cards->cards[gameId] += 1; Uneeded as we default them to 1 games won // cards->cards[gameId] += 1; Uneeded as we default them to 1 games won
int winningGames = games->games[gameId].gamesWon; int winningGames = games->games[gameId].gamesWon;
int iterations = winningGames + cards->cards[gameId]; int iterations = winningGames * cards->cards[gameId];
for (int i = 1; i <= iterations; i++) 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; // int winningGames = games->games[gameId].gamesWon;
@ -182,7 +185,7 @@ int main()
} }
// Finish sad loop and cry // Finish sad loop and cry
for (int i = 0; i < LineCount; i++) for (int i = 0; i <= LineCount; i++)
{ {
total += cards->cards[i]; total += cards->cards[i];
} }

33
day5/input Normal file
View File

@ -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

33
day5/input_test Normal file
View File

@ -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

1
day5/libs Symbolic link
View File

@ -0,0 +1 @@
../libs

11
day5/out.list Normal file
View File

@ -0,0 +1,11 @@
"#include <stdio.h>",
"#include <stdlib.h>",
"#include "libs/fileLoader.h"",
"",
"int main()",
"{",
" char *filename = "input";",
" struct OpenFile *o_file = load_file_to_mem(filename);",
"",
" return 0;",
"}",

65
day5/parser.c Normal file
View File

@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#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;
}

9
day5/parser.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef fileparser
#define fileparser
#include "structs.h"
#include "parser.h"
#include "libs/fileLoader.h"
Almanac *parseFile(OpenFile *file);
#endif

BIN
day5/puzze1 Executable file

Binary file not shown.

23
day5/puzzle1.c Normal file
View File

@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#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;
}

11
day5/puzzle2.c Normal file
View File

@ -0,0 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
#include "libs/fileLoader.h"
int main()
{
char *filename = "input";
struct OpenFile *o_file = load_file_to_mem(filename);
return 0;
}

0
day5/structs.c Normal file
View File

72
day5/structs.h Normal file
View File

@ -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

View File

@ -1,10 +1,10 @@
#ifndef fileloader #ifndef fileloader
#define fileloader #define fileloader
struct OpenFile typedef struct OpenFile
{ {
char *fileData; char *fileData;
int fileSize; int fileSize;
}; } OpenFile;
struct OpenFile *load_file_to_mem(char *filename); struct OpenFile *load_file_to_mem(char *filename);
int unmap_file(char *ptr, char *filename); int unmap_file(char *ptr, char *filename);