Skip to content

Commit

Permalink
Completed day 02
Browse files Browse the repository at this point in the history
  • Loading branch information
aforgiel committed Dec 2, 2023
1 parent 59f789e commit 98224a0
Show file tree
Hide file tree
Showing 9 changed files with 1,578 additions and 97 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"args": [],
"stopAtEntry": false,
"externalConsole": true,
"cwd": "c:/Users/aforgiel/source/repos/AdventOfCode2023/AdventOfCodeDay01",
"program": "c:/Users/aforgiel/source/repos/AdventOfCode2023/AdventOfCodeDay01/build/Debug/outDebug",
"cwd": "c:/Users/aforgiel/source/AdventOfCodeDay14",
"program": "c:/Users/aforgiel/source/AdventOfCodeDay14/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
Expand Down
2 changes: 1 addition & 1 deletion AdventOfCodeDay01/AdventOfCodeDay01.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// AdventOfCodeDay15.cpp : This file contains the 'main' function. Program execution begins and ends there.
// AdventOfCodeDay01.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
Expand Down
239 changes: 145 additions & 94 deletions AdventOfCodeDay02/AdventOfCodeDay02.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// AdventOfCodeDay15.cpp : This file contains the 'main' function. Program execution begins and ends there.
// AdventOfCodeDay02.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
Expand All @@ -7,44 +7,31 @@
#include <vector>
#include <algorithm>

#define TEST_MODE true
#define TEST_MODE false
#define COMMENT false

#if( TEST_MODE == true)
const char* fileName = "C:/Users/aforgiel/source/repos/AdventOfCode2023/AdventOfCodeDay02/sample2.txt";
const char* fileName = "C:/Users/aforgiel/source/repos/AdventOfCode2023/AdventOfCodeDay02/sample.txt";
#else
const char* fileName = "C:/Users/aforgiel/source/repos/AdventOfCode2023/AdventOfCodeDay02/input.txt";
#endif

const char* digits[] =
{
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine"
};

bool FindPattern(char** buffer, const char* pattern)
bool MatchPattern(char* buffer, const char* pattern)
{
char* ptrPattern;
bool result;

ptrPattern = const_cast<char*>(pattern);
result = false;

while ((**buffer) != '\0' && *ptrPattern != '\0')
while ((*buffer) != '\0' && *ptrPattern != '\0')
{
result = (*ptrPattern == **buffer);
result = (*ptrPattern == *buffer);
if (result == false)
return false;

ptrPattern++;
(*buffer)++;
buffer++;
}

if (*ptrPattern == '\0')
Expand All @@ -53,23 +40,28 @@ bool FindPattern(char** buffer, const char* pattern)
return false;
}

int FindTextDigit(char** buffer)
bool FindPattern(char** buffer, const char* pattern)
{
char* tmp;
int index;
char* ptrPattern;
bool result;

index = 0;
for( index = 0; index < 9; index++ )
ptrPattern = const_cast<char*>(pattern);
result = false;

while (**buffer != '\0' && *ptrPattern != '\0')
{
tmp = *buffer;
if (FindPattern(&tmp, digits[index]))
result = (*ptrPattern == **buffer);
if (result == true)
ptrPattern++;
else
{
*buffer = tmp;
return index + 1;
*buffer -= ptrPattern - pattern;
ptrPattern = const_cast<char*>(pattern);
}
(*buffer)++;
}

return 0;
return result;
}

int ReadLine(FILE* file, char* buffer)
Expand All @@ -95,114 +87,173 @@ int ReadLine(FILE* file, char* buffer)
return result;
}

const char* kXXXPattern = "dfdf=";
const char* header = "Game ";
const char* startGame = ": ";
const char* nextColor = ", ";
const char* nextSubset = "; ";
const char* space = " ";

inline bool IsDigit( char c )
{
return (c >= '0' && c <= '9');
}
#define R 0
#define G 1
#define B 2

const char* colors[] = {
"red",
"green",
"blue"
};

const int maxs[] = { 12, 13, 14 };

bool ReadData1(FILE* file, int64_t * result )
bool ReadData1(FILE* file, int64_t* result)
{
char buffer[256];
char * tmp;
int firstDigit;
int aDigit;
int calibration;
char* tmp;
char* ncolor;
char* nsubset;
int index;
int digit;
int textDigit;
int subset;

int color[3];
int i;

int game;
int cube;
bool possible;

index = 0;
while (ReadLine(file, buffer) > 0)
{
firstDigit = 0;
aDigit = 0;
textDigit = 0;
tmp = buffer;
printf("[%004d] buffer: %s,", index, buffer);
while( * tmp != '\0' )
printf("[%004d] input: \"%s\"", index,buffer);

FindPattern(&tmp, header);
sscanf_s(tmp, "%d", &game);
printf(" game: %d\n", game);
FindPattern(&tmp, startGame );
possible = true;
subset = 0;
while (*tmp != '\0')
{
if (IsDigit(*tmp))
color[R] = color[G] = color[B] = 0;

// Read subset:
while (*tmp != '\0')
{
sscanf_s(tmp, "%d", &cube);
FindPattern(&tmp, space);
for (i = 0; i < 3; i++)
if (MatchPattern(tmp, colors[i]))
color[i] = cube;
ncolor = tmp;
nsubset = tmp;
FindPattern(&ncolor, nextColor);
FindPattern(&nsubset, nextSubset);
if (ncolor >= nsubset)
{
tmp = nsubset;
break;
}
tmp = ncolor;
}

printf("\tsubset %d: {%d,%d,%d}", subset, color[R], color[G], color[B]);

if (color[R] > maxs[R] ||
color[G] > maxs[G] ||
color[B] > maxs[B])
{
digit = *tmp - '0';
printf(" => d: %d", digit);
if (firstDigit == 0)
firstDigit = digit;
aDigit = digit;
printf(" => impossible\n");
possible = false;
}
tmp++;
else
printf("\n");
subset++;
}

calibration = firstDigit * 10 + aDigit;
if (possible)
{
printf("\t=> possible\n");
*result += game;
}

printf( " calibration: %d\n", calibration );
*result += calibration;
index++;
}

return true;
}

bool ReadData2(FILE* file, int64_t * result )
bool ReadData2(FILE* file, int64_t* result)
{
char buffer[256];
char * tmp;
int firstDigit;
int aDigit;
int calibration;
char* tmp;
char* ncolor;
char* nsubset;
int index;
int digit;
int textDigit;
char* textTmp;
int subset;

int maxColor[3];
int color[3];
int i;

int game;
int cube;
int score;

index = 0;
while (ReadLine(file, buffer) > 0)
{
firstDigit = 0;
aDigit = 0;
textDigit = 0;
tmp = buffer;
printf("[%004d] buffer: %s,", index, buffer);
while( * tmp != '\0' )
printf("[%004d] input: \"%s\"", index,buffer);

FindPattern(&tmp, header);
sscanf_s(tmp, "%d", &game);
printf(" game: %d\n", game);
FindPattern(&tmp, startGame );
maxColor[R] = maxColor[G] = maxColor[B] = 1;
subset = 0;
while (*tmp != '\0')
{
if (IsDigit(*tmp))
{
digit = *tmp - '0';
printf(" => d: %d", digit);
if (firstDigit == 0)
firstDigit = digit;
aDigit = digit;
}
else
color[R] = color[G] = color[B] = 0;

// Read subset:
while (*tmp != '\0')
{
textTmp = tmp;
textDigit = FindTextDigit(&textTmp);
if (textDigit > 0)
sscanf_s(tmp, "%d", &cube);
FindPattern(&tmp, space);
for (i = 0; i < 3; i++)
if (MatchPattern(tmp, colors[i]))
color[i] = cube;
ncolor = tmp;
nsubset = tmp;
FindPattern(&ncolor, nextColor);
FindPattern(&nsubset, nextSubset);
if (ncolor >= nsubset)
{
printf(" => t: %d", textDigit);
if (firstDigit == 0)
firstDigit = textDigit;
aDigit = textDigit;
tmp = nsubset;
break;
}
tmp = ncolor;
}
tmp++;

printf("\tsubset %d: {%d,%d,%d}\n", subset, color[R], color[G], color[B]);

for (i = 0; i < 3; i++)
if (color[i] > maxColor[i])
maxColor[i] = color[i];
subset++;
}

calibration = firstDigit * 10 + aDigit;
score = maxColor[R] * maxColor[G] * maxColor[B];
printf("\tminimum set {%d,%d,%d} => score: %d\n", maxColor[R], maxColor[G], maxColor[B],score);
*result += score;

printf( " calibration: %d\n", calibration );
*result += calibration;
index++;
}

return true;
}

void PrintData( void )
{
}

int main()
{
FILE* input;
Expand Down
Loading

0 comments on commit 98224a0

Please sign in to comment.