diff --git a/.vscode/launch.json b/.vscode/launch.json index dadc49d..57ec6fe 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "name": "(gdb) Start", "type": "cppdbg", "request": "launch", - "program": "${workspaceFolder}/c/day1.exe", + "program": "${workspaceFolder}/c/day2.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}/c/", diff --git a/c/build-all.ps1 b/c/build-all.ps1 index 44f1a43..d533e67 100644 --- a/c/build-all.ps1 +++ b/c/build-all.ps1 @@ -1 +1,2 @@ -gcc day1/*.c -o day1.exe -Og -g -Wall -Werror -std=c17 \ No newline at end of file +gcc day1/*.c -o day1.exe -Og -g -Wall -Werror -std=c17 +gcc day2/*.c -o day2.exe -Og -g -Wall -Werror -std=c17 \ No newline at end of file diff --git a/c/day2/input.txt b/c/day2/input.txt new file mode 100644 index 0000000..671a4d0 --- /dev/null +++ b/c/day2/input.txt @@ -0,0 +1,1000 @@ +forward 6 +down 2 +forward 2 +down 8 +forward 3 +down 6 +down 8 +down 9 +forward 7 +forward 8 +down 9 +down 8 +down 9 +up 8 +forward 1 +down 7 +down 3 +forward 3 +forward 1 +down 3 +forward 3 +forward 1 +up 8 +down 5 +down 1 +forward 6 +forward 2 +up 9 +down 3 +down 8 +down 3 +down 3 +up 2 +down 7 +down 3 +up 5 +forward 4 +down 9 +forward 6 +forward 3 +forward 1 +forward 3 +down 2 +up 9 +down 4 +forward 6 +down 3 +forward 2 +down 2 +up 5 +up 1 +forward 3 +forward 6 +down 6 +forward 7 +forward 1 +down 3 +down 8 +forward 2 +down 7 +up 1 +up 2 +forward 5 +down 8 +down 8 +forward 9 +forward 7 +forward 2 +forward 7 +up 6 +up 9 +down 4 +forward 4 +forward 4 +up 1 +down 7 +forward 9 +forward 3 +down 6 +down 9 +forward 7 +forward 4 +up 7 +up 6 +up 8 +down 9 +forward 1 +down 1 +forward 8 +down 7 +forward 5 +down 3 +down 3 +down 8 +down 8 +down 4 +up 4 +forward 3 +down 8 +down 9 +up 3 +up 8 +down 9 +up 5 +forward 2 +forward 5 +forward 5 +down 8 +forward 9 +forward 8 +down 5 +down 9 +forward 6 +forward 2 +forward 3 +up 1 +forward 1 +up 2 +up 2 +forward 4 +forward 8 +forward 5 +down 1 +up 4 +forward 5 +up 7 +down 5 +down 5 +forward 8 +up 2 +down 7 +down 6 +down 5 +down 5 +down 1 +down 8 +forward 9 +forward 2 +up 6 +up 4 +down 8 +forward 1 +forward 2 +down 2 +forward 7 +forward 7 +forward 3 +forward 6 +forward 8 +down 3 +forward 6 +up 5 +down 3 +down 8 +up 1 +forward 1 +down 7 +down 3 +up 5 +forward 6 +forward 8 +forward 9 +up 5 +up 5 +up 5 +forward 8 +up 5 +down 6 +down 7 +down 5 +up 7 +up 1 +up 3 +forward 8 +up 9 +down 7 +down 4 +up 6 +up 8 +up 9 +up 9 +forward 5 +up 5 +forward 2 +forward 2 +forward 6 +up 2 +down 8 +up 2 +forward 5 +down 9 +up 7 +down 9 +forward 1 +forward 8 +up 1 +forward 7 +forward 2 +down 3 +forward 3 +forward 2 +up 9 +forward 4 +forward 9 +down 9 +forward 5 +forward 1 +forward 5 +forward 8 +up 5 +forward 1 +down 4 +up 8 +up 4 +up 7 +forward 4 +down 1 +up 6 +forward 6 +down 2 +down 7 +forward 4 +up 7 +forward 7 +forward 9 +down 5 +up 5 +forward 4 +down 6 +forward 1 +up 8 +up 8 +down 8 +down 7 +forward 7 +down 3 +forward 7 +down 3 +down 5 +down 4 +up 8 +down 2 +down 2 +up 5 +forward 9 +up 9 +forward 2 +up 4 +forward 4 +down 2 +down 7 +forward 7 +down 1 +down 6 +down 4 +forward 6 +up 4 +forward 4 +down 6 +down 8 +down 3 +forward 7 +down 3 +forward 7 +down 7 +forward 4 +up 9 +down 5 +forward 7 +forward 7 +up 6 +down 3 +forward 9 +down 1 +forward 4 +up 9 +down 3 +up 9 +down 5 +up 6 +forward 1 +forward 9 +up 4 +down 3 +forward 1 +down 7 +down 2 +forward 2 +down 6 +up 4 +down 4 +up 9 +down 3 +down 9 +down 4 +down 1 +up 8 +down 2 +up 1 +forward 5 +forward 9 +forward 1 +up 4 +forward 5 +down 7 +up 6 +down 3 +forward 8 +down 1 +down 5 +forward 5 +down 5 +down 7 +down 8 +down 7 +up 6 +forward 8 +down 8 +forward 6 +down 6 +down 7 +down 3 +forward 2 +down 6 +down 8 +down 7 +down 3 +up 1 +down 7 +forward 8 +forward 2 +forward 5 +down 4 +up 4 +forward 9 +down 9 +forward 6 +down 7 +down 4 +down 8 +up 9 +forward 7 +down 4 +forward 7 +forward 1 +forward 7 +down 9 +down 7 +forward 3 +forward 3 +forward 2 +down 5 +up 5 +forward 5 +down 2 +forward 7 +forward 9 +forward 7 +down 7 +down 9 +down 5 +forward 2 +up 5 +down 3 +forward 7 +down 4 +down 3 +up 5 +down 6 +down 3 +up 4 +forward 3 +down 1 +forward 6 +forward 6 +down 8 +forward 9 +down 2 +up 3 +down 4 +down 5 +forward 3 +down 9 +forward 2 +up 3 +up 4 +forward 9 +down 2 +forward 9 +forward 3 +down 4 +down 2 +down 5 +down 4 +forward 4 +down 1 +down 9 +down 2 +forward 8 +down 5 +forward 5 +up 7 +down 5 +down 2 +forward 5 +up 4 +down 5 +up 3 +forward 7 +down 9 +forward 5 +forward 2 +forward 1 +down 7 +down 9 +down 2 +up 2 +up 2 +up 4 +down 4 +down 7 +down 3 +forward 5 +forward 3 +up 6 +down 6 +up 6 +up 9 +forward 8 +forward 4 +up 3 +forward 1 +forward 2 +up 5 +forward 5 +forward 8 +forward 7 +forward 4 +down 1 +down 8 +down 1 +forward 3 +up 1 +forward 7 +forward 4 +down 8 +forward 7 +forward 9 +forward 3 +down 9 +down 9 +down 3 +up 6 +up 1 +down 4 +forward 5 +forward 4 +forward 6 +forward 8 +down 6 +down 3 +forward 5 +forward 6 +down 4 +down 2 +up 3 +down 3 +down 7 +down 5 +down 5 +forward 6 +down 4 +forward 1 +up 2 +forward 3 +down 1 +down 4 +down 9 +down 7 +down 9 +forward 9 +down 6 +down 3 +down 2 +down 5 +up 8 +forward 5 +forward 5 +forward 4 +up 5 +forward 1 +down 9 +down 1 +up 5 +forward 8 +forward 6 +forward 5 +down 1 +up 5 +down 8 +up 7 +down 8 +down 2 +down 3 +forward 2 +up 4 +down 6 +up 6 +down 3 +down 7 +up 3 +forward 4 +down 3 +forward 4 +up 9 +forward 5 +down 2 +forward 7 +forward 5 +up 3 +up 2 +forward 2 +down 8 +down 1 +down 3 +up 5 +down 4 +forward 4 +down 1 +forward 9 +down 3 +down 7 +down 4 +down 4 +forward 7 +up 5 +forward 4 +down 8 +up 4 +forward 6 +down 1 +up 4 +forward 4 +down 6 +up 5 +up 1 +forward 2 +down 5 +forward 8 +forward 6 +down 8 +down 7 +down 7 +down 1 +forward 5 +forward 7 +forward 7 +forward 7 +up 3 +forward 9 +forward 1 +down 9 +forward 4 +up 8 +forward 1 +forward 5 +forward 4 +down 2 +forward 4 +forward 9 +forward 3 +down 1 +forward 4 +forward 9 +forward 5 +down 5 +down 5 +forward 7 +down 3 +forward 4 +down 6 +forward 7 +down 2 +down 1 +down 5 +forward 4 +forward 9 +down 4 +forward 2 +down 8 +up 5 +down 9 +forward 8 +down 3 +up 6 +down 2 +down 4 +forward 4 +up 2 +down 4 +down 4 +up 7 +down 6 +forward 4 +down 7 +forward 3 +down 1 +up 1 +down 2 +down 6 +down 4 +up 3 +down 6 +up 2 +down 6 +forward 3 +down 9 +forward 5 +down 5 +down 9 +down 9 +down 7 +forward 9 +forward 8 +forward 9 +up 9 +forward 7 +forward 4 +forward 4 +up 5 +forward 2 +down 1 +up 9 +forward 2 +forward 7 +forward 1 +down 9 +forward 9 +up 8 +up 1 +up 7 +up 7 +down 5 +forward 2 +forward 8 +forward 6 +down 7 +forward 1 +down 9 +down 4 +down 4 +down 1 +up 7 +forward 4 +forward 6 +up 5 +forward 2 +down 9 +down 7 +forward 1 +forward 2 +down 5 +forward 3 +forward 8 +forward 6 +forward 3 +forward 2 +down 1 +forward 1 +forward 1 +forward 3 +down 9 +up 9 +down 9 +down 6 +forward 7 +down 6 +forward 9 +down 9 +down 7 +down 1 +down 9 +up 9 +down 6 +forward 9 +down 6 +forward 3 +down 8 +up 5 +forward 5 +forward 8 +up 3 +down 8 +up 6 +forward 4 +down 2 +forward 6 +down 9 +forward 6 +forward 4 +forward 9 +forward 3 +down 2 +down 4 +forward 5 +down 9 +up 7 +forward 4 +up 1 +forward 1 +down 6 +forward 3 +forward 7 +forward 2 +forward 2 +down 5 +down 9 +down 3 +down 5 +up 3 +forward 1 +down 2 +down 4 +down 1 +up 9 +up 5 +up 1 +down 1 +up 9 +down 5 +up 3 +up 3 +down 7 +forward 4 +down 6 +forward 2 +forward 7 +forward 4 +down 2 +forward 6 +forward 2 +down 3 +up 3 +up 9 +forward 9 +forward 9 +forward 6 +down 8 +down 1 +forward 9 +up 1 +down 6 +forward 6 +up 5 +forward 2 +forward 6 +down 9 +forward 1 +forward 8 +down 8 +forward 4 +forward 7 +up 6 +up 1 +forward 7 +forward 3 +forward 2 +down 4 +down 7 +down 7 +down 1 +down 6 +forward 1 +down 9 +up 9 +up 9 +down 2 +down 2 +forward 5 +up 2 +forward 7 +up 5 +down 9 +forward 7 +forward 2 +down 8 +up 1 +down 5 +forward 6 +down 8 +down 7 +forward 4 +up 2 +down 8 +forward 2 +down 5 +down 4 +down 9 +down 1 +down 9 +down 6 +down 3 +forward 1 +forward 6 +up 1 +up 1 +up 9 +down 2 +down 2 +forward 5 +down 3 +forward 4 +down 3 +down 7 +down 7 +forward 4 +up 3 +forward 4 +down 3 +forward 8 +forward 1 +up 2 +up 1 +forward 1 +down 6 +down 1 +down 3 +forward 7 +down 7 +forward 4 +forward 5 +forward 3 +down 5 +forward 9 +forward 5 +down 7 +forward 6 +down 4 +down 4 +down 9 +down 3 +up 9 +forward 7 +down 7 +forward 6 +down 2 +down 9 +forward 4 +forward 1 +forward 4 +down 5 +forward 7 +down 9 +down 8 +forward 9 +forward 1 +down 9 +forward 6 +up 5 +forward 9 +down 1 +down 5 +forward 4 +forward 5 +forward 8 +down 5 +forward 9 +down 6 +down 2 +up 4 +up 8 +forward 3 +forward 4 +down 3 +forward 4 +up 6 +forward 3 +forward 8 +forward 7 +down 1 +down 9 +down 8 +down 8 +down 1 +forward 9 +up 4 +down 5 +forward 7 +down 8 +down 3 +forward 9 +down 5 +forward 7 +forward 2 +down 4 +forward 2 +forward 7 +down 6 +forward 7 +down 2 +forward 9 +down 9 +forward 8 +forward 8 +down 6 +forward 7 +down 8 +forward 7 +forward 3 +down 1 +up 8 +down 5 +down 6 +up 5 +forward 5 +forward 5 +up 5 +up 3 +up 7 +down 6 +forward 8 +forward 4 +down 2 +up 5 +forward 8 +down 6 +forward 4 +forward 2 +up 8 +down 8 +down 5 +down 4 +forward 9 +forward 9 +forward 6 +forward 6 +down 3 +up 1 +down 4 +down 8 +down 9 +down 1 +forward 3 +forward 1 +down 9 +down 3 +down 7 +forward 6 +forward 9 +down 8 +down 8 +forward 6 +forward 1 +down 3 +forward 1 +down 8 +down 3 +down 9 +up 1 +forward 6 +up 2 +down 3 +forward 4 +forward 2 +up 2 +down 5 +forward 1 +down 3 +forward 9 +forward 4 +forward 6 +down 3 +forward 7 +down 6 +up 3 +up 7 +up 5 +down 4 +forward 4 +up 1 +forward 7 +up 9 +forward 3 +up 1 +down 3 +down 4 +forward 4 +up 3 +down 6 +down 9 +down 6 +forward 4 +down 9 +down 6 +forward 4 +forward 3 +down 3 +up 7 +down 9 +forward 8 \ No newline at end of file diff --git a/c/day2/main.c b/c/day2/main.c new file mode 100644 index 0000000..a4661f4 --- /dev/null +++ b/c/day2/main.c @@ -0,0 +1,132 @@ +// https://adventofcode.com/2021/ +#include +#include +#include + +#define DAY 2 +#define INPUT_LENGTH 1000 + +struct Order +{ + enum Direction + { + FORWARD, + DOWN, + UP, + } direction; + int amount; +} parsedInput[INPUT_LENGTH]; + +void parseInput(char *const input) +{ + char *pch = strtok(input, " \n"); + int orderNum = 0; + while (pch != NULL) + { + switch (pch[0]) + { + case 'f': + parsedInput[orderNum].direction = FORWARD; + break; + case 'd': + parsedInput[orderNum].direction = DOWN; + break; + case 'u': + parsedInput[orderNum].direction = UP; + break; + default: + printf("Unknown direction: %s\n", pch); + exit(EXIT_FAILURE); + } + + pch = strtok(NULL, " \n"); + parsedInput[orderNum].amount = _atoi64(pch); + + pch = strtok(NULL, " \n"); + orderNum++; + } +} + +char const *const part1() +{ + static unsigned int horizontal = 0; + static unsigned int depth = 0; + + for (unsigned int i = 0; i < INPUT_LENGTH; i++) + { + switch (parsedInput[i].direction) + { + case FORWARD: + horizontal += parsedInput[i].amount; + break; + case DOWN: + depth += parsedInput[i].amount; + break; + case UP: + depth -= parsedInput[i].amount; + break; + } + } + char *const result = malloc(sizeof(char) * 50); + sprintf(result, "X: %d, Y: %d; Result: %d", horizontal, depth, horizontal * depth); + return result; +}; + +char const *const part2() +{ + static unsigned int horizontal = 0; + static unsigned int depth = 0; + static unsigned int aim = 0; + + for (unsigned int i = 0; i < INPUT_LENGTH; i++) + { + switch (parsedInput[i].direction) + { + case FORWARD: + horizontal += parsedInput[i].amount; + depth += parsedInput[i].amount * aim; + break; + case DOWN: + aim += parsedInput[i].amount; + break; + case UP: + aim -= parsedInput[i].amount; + break; + } + } + char *const result = malloc(sizeof(char) * 50); + sprintf(result, "X: %d, Y: %d; Result: %d", horizontal, depth, horizontal * depth); + return result; +}; + +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; +} \ No newline at end of file