C Day 1
This commit is contained in:
commit
305908b6aa
6 changed files with 2135 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
*.exe
|
24
.vscode/launch.json
vendored
Normal file
24
.vscode/launch.json
vendored
Normal file
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "(gdb) Start",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/c/day1.exe",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}/c/",
|
||||
"environment": [],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
13
AdventOfCode.code-workspace
Normal file
13
AdventOfCode.code-workspace
Normal file
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"folders": [
|
||||
{
|
||||
"path": "."
|
||||
}
|
||||
],
|
||||
"settings": {
|
||||
"files.associations": {
|
||||
"*.lock": "yarnlock",
|
||||
"stdlib.h": "c"
|
||||
}
|
||||
}
|
||||
}
|
1
c/build-all.ps1
Normal file
1
c/build-all.ps1
Normal file
|
@ -0,0 +1 @@
|
|||
gcc day1/*.c -o day1.exe -Og -g -Wall -Werror -std=c17
|
2000
c/day1/input.txt
Normal file
2000
c/day1/input.txt
Normal file
File diff suppressed because it is too large
Load diff
96
c/day1/main.c
Normal file
96
c/day1/main.c
Normal file
|
@ -0,0 +1,96 @@
|
|||
// https://adventofcode.com/2021/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DAY 1
|
||||
#define INPUT_LENGTH 2000
|
||||
|
||||
static int parsedInput[INPUT_LENGTH];
|
||||
|
||||
void parseInput(char const *const input)
|
||||
{
|
||||
char const *curLine = input;
|
||||
unsigned int lineNum = 0;
|
||||
while (curLine)
|
||||
{
|
||||
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] = _atoi64(tempStr);
|
||||
|
||||
free(tempStr);
|
||||
|
||||
curLine = nextLine ? nextLine + 1 : NULL;
|
||||
lineNum++;
|
||||
}
|
||||
}
|
||||
|
||||
char const *const part1()
|
||||
{
|
||||
static const unsigned char MEASUREMENTS = 2;
|
||||
int result = 0;
|
||||
for (unsigned int i = MEASUREMENTS - 1; i < INPUT_LENGTH; i++)
|
||||
{
|
||||
if (parsedInput[i] > parsedInput[i - 1])
|
||||
{
|
||||
result++;
|
||||
}
|
||||
}
|
||||
char *const resultString = malloc(sizeof(char) * 10);
|
||||
sprintf(resultString, "%d", result);
|
||||
return resultString;
|
||||
};
|
||||
char const *const part2()
|
||||
{
|
||||
static const unsigned char MEASUREMENTS = 3;
|
||||
int result = 0;
|
||||
for (unsigned int i = MEASUREMENTS; i < INPUT_LENGTH; i++)
|
||||
{
|
||||
int measurementWindowA = parsedInput[i - 3] + parsedInput[i - 2] + parsedInput[i - 1];
|
||||
int measurementWindowB = parsedInput[i - 2] + parsedInput[i - 1] + parsedInput[i - 0];
|
||||
|
||||
if (measurementWindowB > measurementWindowA)
|
||||
{
|
||||
result++;
|
||||
}
|
||||
}
|
||||
char *const resultString = malloc(sizeof(char) * 10);
|
||||
sprintf(resultString, "%d", result);
|
||||
return resultString;
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
Loading…
Reference in a new issue