C Day 3 (Part 1 only)

Signed-off-by: Tobias Berger <tobi.berger13@gmail.com>
This commit is contained in:
Tobias Berger 2021-12-03 19:04:32 +01:00
parent d9efc9e8d5
commit dddf368391
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
5 changed files with 1140 additions and 3 deletions

2
.vscode/launch.json vendored
View file

@ -5,7 +5,7 @@
"name": "(gdb) Start",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/c/day2.exe",
"program": "${workspaceFolder}/c/day3.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/c/",

17
.vscode/tasks.json vendored Normal file
View file

@ -0,0 +1,17 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Run build script",
"command": "./build-all.ps1",
"options": {
"cwd": "${workspaceFolder}/c/"
},
"group": {
"kind": "build",
"isDefault": true
},
}
]
}

5
c/build-all.ps1 Normal file → Executable file
View file

@ -1,2 +1,3 @@
gcc day1/*.c -o day1.exe -Og -g -Wall -Werror -std=c17
gcc day2/*.c -o day2.exe -Og -g -Wall -Werror -std=c17
# gcc day1/*.c -o day1.exe -Og -g -Wall -Werror -std=c17
# gcc day2/*.c -o day2.exe -Og -g -Wall -Werror -std=c17
gcc day3/*.c -o day3.exe -Og -g -Wall -Werror -std=c17

1000
c/day3/input.txt Normal file

File diff suppressed because it is too large Load diff

119
c/day3/main.c Normal file
View file

@ -0,0 +1,119 @@
// https://adventofcode.com/2021/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DAY 3
#define INPUT_LENGTH 1000
#define INPUT_BIT_SIZE 12
unsigned int parsedInput[INPUT_LENGTH];
void parseInput(char *const input)
{
char const *curLine = input;
unsigned int lineNum = 0;
while (lineNum < INPUT_LENGTH)
{
char const *nextLine = strchr(curLine, '\n');
int curLineLen = nextLine ? nextLine - curLine : strlen(curLine);
char *const tempStr = malloc(curLineLen + 1);
memcpy(tempStr, curLine, curLineLen);
tempStr[curLineLen] = '\0';
parsedInput[lineNum] = strtol(tempStr, NULL, 2);
free(tempStr);
curLine = nextLine ? nextLine + 1 : NULL;
lineNum++;
}
}
char const *const part1()
{
unsigned int ones[INPUT_BIT_SIZE] = {0};
for (unsigned int input_i = 0; input_i < INPUT_LENGTH; input_i++)
{
for (unsigned char i = 0; i < INPUT_BIT_SIZE; i++)
{
ones[i] += (parsedInput[input_i] >> i) & 1;
}
}
char gammaStr[INPUT_BIT_SIZE + 1];
sprintf(gammaStr, "%c%c%c%c%c%c%c%c%c%c%c%c",
ones[INPUT_BIT_SIZE - 1] > INPUT_LENGTH / 2 ? '1' : '0',
ones[INPUT_BIT_SIZE - 2] > INPUT_LENGTH / 2 ? '1' : '0',
ones[INPUT_BIT_SIZE - 3] > INPUT_LENGTH / 2 ? '1' : '0',
ones[INPUT_BIT_SIZE - 4] > INPUT_LENGTH / 2 ? '1' : '0',
ones[INPUT_BIT_SIZE - 5] > INPUT_LENGTH / 2 ? '1' : '0',
ones[INPUT_BIT_SIZE - 6] > INPUT_LENGTH / 2 ? '1' : '0',
ones[INPUT_BIT_SIZE - 7] > INPUT_LENGTH / 2 ? '1' : '0',
ones[INPUT_BIT_SIZE - 8] > INPUT_LENGTH / 2 ? '1' : '0',
ones[INPUT_BIT_SIZE - 9] > INPUT_LENGTH / 2 ? '1' : '0',
ones[INPUT_BIT_SIZE - 10] > INPUT_LENGTH / 2 ? '1' : '0',
ones[INPUT_BIT_SIZE - 11] > INPUT_LENGTH / 2 ? '1' : '0',
ones[INPUT_BIT_SIZE - 12] > INPUT_LENGTH / 2 ? '1' : '0');
char epsilonStr[INPUT_BIT_SIZE + 1];
sprintf(epsilonStr, "%c%c%c%c%c%c%c%c%c%c%c%c",
ones[INPUT_BIT_SIZE - 1] > INPUT_LENGTH / 2 ? '0' : '1',
ones[INPUT_BIT_SIZE - 2] > INPUT_LENGTH / 2 ? '0' : '1',
ones[INPUT_BIT_SIZE - 3] > INPUT_LENGTH / 2 ? '0' : '1',
ones[INPUT_BIT_SIZE - 4] > INPUT_LENGTH / 2 ? '0' : '1',
ones[INPUT_BIT_SIZE - 5] > INPUT_LENGTH / 2 ? '0' : '1',
ones[INPUT_BIT_SIZE - 6] > INPUT_LENGTH / 2 ? '0' : '1',
ones[INPUT_BIT_SIZE - 7] > INPUT_LENGTH / 2 ? '0' : '1',
ones[INPUT_BIT_SIZE - 8] > INPUT_LENGTH / 2 ? '0' : '1',
ones[INPUT_BIT_SIZE - 9] > INPUT_LENGTH / 2 ? '0' : '1',
ones[INPUT_BIT_SIZE - 10] > INPUT_LENGTH / 2 ? '0' : '1',
ones[INPUT_BIT_SIZE - 11] > INPUT_LENGTH / 2 ? '0' : '1',
ones[INPUT_BIT_SIZE - 12] > INPUT_LENGTH / 2 ? '0' : '1');
long int gamma = strtol(gammaStr, NULL, 2);
long int epsilon = strtol(epsilonStr, NULL, 2);
char *const result = malloc(32);
sprintf(result, "%ld", gamma * epsilon);
return result;
};
char const *const part2()
{
return "UNSOLVED";
};
int main(void)
{
char *const fileName = malloc(sizeof(char) * sizeof("dayXX/input.txt"));
sprintf(fileName, "day%d/input.txt", DAY);
FILE *inputFile = fopen(fileName, "r");
if (inputFile == NULL)
{
printf("Failed to open input file %s.\n", fileName);
return 1;
}
fseek(inputFile, 0, SEEK_END);
long length = ftell(inputFile);
fseek(inputFile, 0, SEEK_SET);
char *const input = malloc(length);
if (input != NULL)
{
fread(input, 1, length, inputFile);
}
fclose(inputFile);
parseInput(input);
printf("Day %d\n", DAY);
printf("Part 1:\n%s\n", part1());
printf("--------------\n");
printf("Part 2:\n%s\n\n", part2());
return 0;
}