diff options
author | kartofen <mladenovnasko0@gmail.com> | 2023-02-10 23:53:15 +0200 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2023-02-10 23:53:15 +0200 |
commit | 555091332de32b1d3b36c888ed21898aae540750 (patch) | |
tree | 6dd5dc02ecb4d7678d13e5f1265e61e155f33c43 | |
parent | 9f57e15a054b16d335e9d5c49ddd7be829b4e272 (diff) |
add a couple of days
-rwxr-xr-x | Advent-of-Code-2022/aoc-12/build.sh | 5 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-12/main.c | 113 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-12/sample.txt | 5 | ||||
-rwxr-xr-x | Advent-of-Code-2022/aoc-14/build.sh | 5 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-14/input.txt | 129 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-14/main.c | 122 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-14/sample.txt | 2 | ||||
-rwxr-xr-x | Advent-of-Code-2022/aoc-15/build.sh | 5 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-15/input.txt | 22 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-15/main.c | 135 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-15/sample.txt | 14 | ||||
-rwxr-xr-x | Advent-of-Code-2022/aoc-25/build.sh | 5 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-25/input.txt | 113 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-25/main.c | 89 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-25/sample.txt | 13 |
15 files changed, 777 insertions, 0 deletions
diff --git a/Advent-of-Code-2022/aoc-12/build.sh b/Advent-of-Code-2022/aoc-12/build.sh new file mode 100755 index 0000000..30f495e --- /dev/null +++ b/Advent-of-Code-2022/aoc-12/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +set -xe + +gcc -o main main.c -Wall -Wextra -pedantic diff --git a/Advent-of-Code-2022/aoc-12/main.c b/Advent-of-Code-2022/aoc-12/main.c new file mode 100644 index 0000000..1511059 --- /dev/null +++ b/Advent-of-Code-2022/aoc-12/main.c @@ -0,0 +1,113 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#if 1 + #define FILENAME "sample.txt" + #define WIDTH 8 + #define HEIGHT 5 +#else + #define FILENAME "input.txt" +#endif + +typedef struct node_t { + char val; + struct node_t *neighbours[4]; // up, down, left, right +} node_t; + +node_t graph[WIDTH][HEIGHT]; + +void part1(); +int char_index(char ch); +int in_range(char a, char b); +void print_graph(); + +void part1() +{ + print_graph(); +} + +void gen_graph(char table[WIDTH][HEIGHT]) +{ + for(int i = 0; i < HEIGHT; i++) + for(int j = 0; j < WIDTH; j++) { + for(int k = 0; k < 4; k++) graph[j][i].neighbours[k] = NULL; + graph[j][i].val = table[j][i]; + + // up + if(i > 0) + if(in_range(table[j][i], table[j][i-1])) + graph[j][i].neighbours[0] = &(graph[j][i-1]); + // down + if(i < HEIGHT - 1) { + if(in_range(table[j][i], table[j][i+1])) + graph[j][i].neighbours[1] = &(graph[j][i+1]); + } + // left + if(j > 0) { + if(in_range(table[i][j], table[j-1][i])) + graph[j][i].neighbours[2] = &(graph[j-1][i]); + } + // right + if(j < WIDTH - 1) { + if(in_range(table[j][i], table[j+1][i])) + graph[j][i].neighbours[3] = &(graph[j+1][i]); + } + } +} + +void parse() +{ + FILE *fp = fopen(FILENAME, "r"); + if(!fp) { + fprintf(stderr, "ERROR: Could not open file: %s\n", FILENAME); + exit(1); + } + + char table[WIDTH][HEIGHT] = {0}; + + char line[64]; + for(int i = 0; fgets(line, sizeof(line), fp) != NULL && i < HEIGHT; i++) + for(int j = 0; j < WIDTH; j++) + table[j][i] = char_index(line[j]); + + gen_graph(table); + fclose(fp); +} + +int main(void) +{ + parse(); + part1(); + return 0; +} + +void print_graph() +{ + for(int i = 0; i < HEIGHT; i++) { + for(int j = 0; j < WIDTH; j++) { + printf("(%2d %2d) %c -> ", j, i, graph[j][i].val + '`'); + for(int k = 0; k < 4; k++) + printf("%c ", (graph[j][i].neighbours[k] == NULL) ? '_' : + graph[j][i].neighbours[k]->val + '`'); + + printf("\n"); + } + printf("\n"); + } +} + +int char_index(char ch) +{ + if(ch == 'S') ch = '`'; + else if(ch == 'E') ch = '{'; + + return ch - '`'; +} + +int in_range(char a, char b) +{ + if(a == b || a == b-1 || a == b+1) + return 1; + return 0; +} diff --git a/Advent-of-Code-2022/aoc-12/sample.txt b/Advent-of-Code-2022/aoc-12/sample.txt new file mode 100644 index 0000000..86e9cac --- /dev/null +++ b/Advent-of-Code-2022/aoc-12/sample.txt @@ -0,0 +1,5 @@ +Sabqponm +abcryxxl +accszExk +acctuvwj +abdefghi diff --git a/Advent-of-Code-2022/aoc-14/build.sh b/Advent-of-Code-2022/aoc-14/build.sh new file mode 100755 index 0000000..9e59ef5 --- /dev/null +++ b/Advent-of-Code-2022/aoc-14/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +set -xe + +gcc -o main main.c -Wall -Wextra -pedantic -D_POSIX_C_SOURCE diff --git a/Advent-of-Code-2022/aoc-14/input.txt b/Advent-of-Code-2022/aoc-14/input.txt new file mode 100644 index 0000000..049aec7 --- /dev/null +++ b/Advent-of-Code-2022/aoc-14/input.txt @@ -0,0 +1,129 @@ +490,51 -> 490,54 -> 486,54 -> 486,59 -> 499,59 -> 499,54 -> 492,54 -> 492,51 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +484,132 -> 489,132 +501,15 -> 506,15 +485,85 -> 485,88 -> 482,88 -> 482,93 -> 499,93 -> 499,88 -> 491,88 -> 491,85 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +503,43 -> 514,43 -> 514,42 +497,13 -> 502,13 +490,51 -> 490,54 -> 486,54 -> 486,59 -> 499,59 -> 499,54 -> 492,54 -> 492,51 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +498,17 -> 503,17 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +490,51 -> 490,54 -> 486,54 -> 486,59 -> 499,59 -> 499,54 -> 492,54 -> 492,51 +467,81 -> 467,82 -> 487,82 -> 487,81 +481,135 -> 486,135 +485,85 -> 485,88 -> 482,88 -> 482,93 -> 499,93 -> 499,88 -> 491,88 -> 491,85 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +470,122 -> 470,123 -> 485,123 -> 485,122 +491,132 -> 496,132 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +491,17 -> 496,17 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +497,26 -> 501,26 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +491,22 -> 495,22 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +480,129 -> 485,129 +490,51 -> 490,54 -> 486,54 -> 486,59 -> 499,59 -> 499,54 -> 492,54 -> 492,51 +477,132 -> 482,132 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +488,20 -> 492,20 +492,48 -> 506,48 -> 506,47 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +474,135 -> 479,135 +479,64 -> 484,64 +510,29 -> 510,33 -> 506,33 -> 506,39 -> 517,39 -> 517,33 -> 512,33 -> 512,29 +486,64 -> 491,64 +490,66 -> 495,66 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +494,24 -> 498,24 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +471,109 -> 471,112 -> 467,112 -> 467,120 -> 477,120 -> 477,112 -> 475,112 -> 475,109 +503,43 -> 514,43 -> 514,42 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +485,85 -> 485,88 -> 482,88 -> 482,93 -> 499,93 -> 499,88 -> 491,88 -> 491,85 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +510,29 -> 510,33 -> 506,33 -> 506,39 -> 517,39 -> 517,33 -> 512,33 -> 512,29 +488,135 -> 493,135 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +467,81 -> 467,82 -> 487,82 -> 487,81 +483,126 -> 488,126 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +483,66 -> 488,66 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +476,66 -> 481,66 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +494,15 -> 499,15 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +485,85 -> 485,88 -> 482,88 -> 482,93 -> 499,93 -> 499,88 -> 491,88 -> 491,85 +485,22 -> 489,22 +487,129 -> 492,129 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +471,109 -> 471,112 -> 467,112 -> 467,120 -> 477,120 -> 477,112 -> 475,112 -> 475,109 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +485,85 -> 485,88 -> 482,88 -> 482,93 -> 499,93 -> 499,88 -> 491,88 -> 491,85 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +488,24 -> 492,24 +490,51 -> 490,54 -> 486,54 -> 486,59 -> 499,59 -> 499,54 -> 492,54 -> 492,51 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +485,85 -> 485,88 -> 482,88 -> 482,93 -> 499,93 -> 499,88 -> 491,88 -> 491,85 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +510,29 -> 510,33 -> 506,33 -> 506,39 -> 517,39 -> 517,33 -> 512,33 -> 512,29 +495,135 -> 500,135 +471,109 -> 471,112 -> 467,112 -> 467,120 -> 477,120 -> 477,112 -> 475,112 -> 475,109 +510,29 -> 510,33 -> 506,33 -> 506,39 -> 517,39 -> 517,33 -> 512,33 -> 512,29 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +471,109 -> 471,112 -> 467,112 -> 467,120 -> 477,120 -> 477,112 -> 475,112 -> 475,109 +482,24 -> 486,24 +471,109 -> 471,112 -> 467,112 -> 467,120 -> 477,120 -> 477,112 -> 475,112 -> 475,109 +510,29 -> 510,33 -> 506,33 -> 506,39 -> 517,39 -> 517,33 -> 512,33 -> 512,29 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +491,26 -> 495,26 +470,122 -> 470,123 -> 485,123 -> 485,122 +505,17 -> 510,17 +470,122 -> 470,123 -> 485,123 -> 485,122 +510,29 -> 510,33 -> 506,33 -> 506,39 -> 517,39 -> 517,33 -> 512,33 -> 512,29 +485,26 -> 489,26 +471,109 -> 471,112 -> 467,112 -> 467,120 -> 477,120 -> 477,112 -> 475,112 -> 475,109 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +471,109 -> 471,112 -> 467,112 -> 467,120 -> 477,120 -> 477,112 -> 475,112 -> 475,109 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +485,85 -> 485,88 -> 482,88 -> 482,93 -> 499,93 -> 499,88 -> 491,88 -> 491,85 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +510,29 -> 510,33 -> 506,33 -> 506,39 -> 517,39 -> 517,33 -> 512,33 -> 512,29 +467,81 -> 467,82 -> 487,82 -> 487,81 +479,26 -> 483,26 +492,48 -> 506,48 -> 506,47 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +490,51 -> 490,54 -> 486,54 -> 486,59 -> 499,59 -> 499,54 -> 492,54 -> 492,51 +482,62 -> 487,62 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +490,51 -> 490,54 -> 486,54 -> 486,59 -> 499,59 -> 499,54 -> 492,54 -> 492,51 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 diff --git a/Advent-of-Code-2022/aoc-14/main.c b/Advent-of-Code-2022/aoc-14/main.c new file mode 100644 index 0000000..7fcfdfa --- /dev/null +++ b/Advent-of-Code-2022/aoc-14/main.c @@ -0,0 +1,122 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <limits.h> + +#if 0 + #define PART part1 +#else + #define PART part2 +#endif + +#if 0 + #define FILENAME "sample.txt" + #define WIDTH 600 + #define HEIGHT 20 +#else + #define FILENAME "input.txt" + #define WIDTH 2000 + #define HEIGHT 500 +#endif + +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) + +int map[WIDTH][HEIGHT] = {0}; +int max_y = INT_MIN; +int sands = 0; + +int sim_sand() +{ + int sand_x = 500; + int sand_y = 0; + while(1) { + if(sand_y >= max_y) + return -1; + + if(map[sand_x][sand_y + 1] != 1) { + sand_y++; + } else if(map[sand_x - 1][sand_y + 1] != 1) { + sand_x--; sand_y++; + } else if(map[sand_x + 1][sand_y + 1] != 1) { + sand_x++; sand_y++; + } else break; + + continue; + } + + map[sand_x][sand_y] = 1; + return 0; +} + +void part1() +{ + while(sim_sand() == 0) + sands++; +} + +void draw_line(int x1, int y1, int x2, int y2); + +void part2() +{ + draw_line(0, max_y + 2, WIDTH-1, max_y + 2); + max_y = INT_MAX; + + while(map[500][0] != 1) { + sim_sand(); + sands++; + } +} + +void draw_line(int x1, int y1, int x2, int y2) +{ + if(x1 != x2 && y1 == y2) + for(int x = MIN(x1, x2); x <= MAX(x1, x2); x++) + map[x][y1] = 1; + else if(y1 != y2 && x1 == x2) + for(int y = MIN(y1, y2); y <= MAX(y1, y2); y++) + map[x1][y] = 1; +} + + +void parse() +{ + FILE *fp = fopen(FILENAME, "r"); + if(!fp) { + fprintf(stderr, "ERROR: Could not open file %s\n", FILENAME); + exit(1); + } + + char line[5000]; + while(fgets(line, sizeof(line), fp)) + { + int x1 = -1; int y1 = -1; + int x2 = -1; int y2 = -1; + + char *sprt1; + char *tok = strtok_r(line, " -> ", &sprt1); + while(tok != NULL) { + + char *sprt2; + x2 = atoi(strtok_r(tok, ",", &sprt2)); + y2 = atoi(strtok_r(NULL, ",", &sprt2)); + max_y = MAX(max_y, y2); + + if(x1 != -1 && y1 != -1) + draw_line(x1, y1, x2, y2); + x1 = x2; y1 = y2; + + tok = strtok_r(NULL, " -> ", &sprt1); + } + } + + fclose(fp); +} + +int main(void) +{ + parse(); + PART(); + printf("%d\n", sands); + return 0; +} diff --git a/Advent-of-Code-2022/aoc-14/sample.txt b/Advent-of-Code-2022/aoc-14/sample.txt new file mode 100644 index 0000000..4e87bb5 --- /dev/null +++ b/Advent-of-Code-2022/aoc-14/sample.txt @@ -0,0 +1,2 @@ +498,4 -> 498,6 -> 496,6 +503,4 -> 502,4 -> 502,9 -> 494,9 diff --git a/Advent-of-Code-2022/aoc-15/build.sh b/Advent-of-Code-2022/aoc-15/build.sh new file mode 100755 index 0000000..9e59ef5 --- /dev/null +++ b/Advent-of-Code-2022/aoc-15/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +set -xe + +gcc -o main main.c -Wall -Wextra -pedantic -D_POSIX_C_SOURCE diff --git a/Advent-of-Code-2022/aoc-15/input.txt b/Advent-of-Code-2022/aoc-15/input.txt new file mode 100644 index 0000000..49e9e5e --- /dev/null +++ b/Advent-of-Code-2022/aoc-15/input.txt @@ -0,0 +1,22 @@ +Sensor at x=1384790, y=3850432: closest beacon is at x=2674241, y=4192888 +Sensor at x=2825953, y=288046: closest beacon is at x=2154954, y=-342775 +Sensor at x=3553843, y=2822363: closest beacon is at x=3444765, y=2347460 +Sensor at x=2495377, y=3130491: closest beacon is at x=2761496, y=2831113 +Sensor at x=1329263, y=1778185: closest beacon is at x=2729595, y=2000000 +Sensor at x=2882039, y=2206085: closest beacon is at x=2729595, y=2000000 +Sensor at x=3903141, y=2510440: closest beacon is at x=4006219, y=3011198 +Sensor at x=3403454, y=3996578: closest beacon is at x=3754119, y=4475047 +Sensor at x=3630476, y=1048796: closest beacon is at x=3444765, y=2347460 +Sensor at x=16252, y=2089672: closest beacon is at x=-276514, y=2995794 +Sensor at x=428672, y=1150723: closest beacon is at x=-281319, y=668868 +Sensor at x=2939101, y=3624676: closest beacon is at x=2674241, y=4192888 +Sensor at x=3166958, y=2890076: closest beacon is at x=2761496, y=2831113 +Sensor at x=3758241, y=3546895: closest beacon is at x=4006219, y=3011198 +Sensor at x=218942, y=3011070: closest beacon is at x=-276514, y=2995794 +Sensor at x=52656, y=3484635: closest beacon is at x=-276514, y=2995794 +Sensor at x=2057106, y=405314: closest beacon is at x=2154954, y=-342775 +Sensor at x=1966905, y=2495701: closest beacon is at x=2761496, y=2831113 +Sensor at x=511976, y=2696731: closest beacon is at x=-276514, y=2995794 +Sensor at x=3094465, y=2478570: closest beacon is at x=3444765, y=2347460 +Sensor at x=806671, y=228252: closest beacon is at x=-281319, y=668868 +Sensor at x=3011731, y=1976307: closest beacon is at x=2729595, y=2000000 diff --git a/Advent-of-Code-2022/aoc-15/main.c b/Advent-of-Code-2022/aoc-15/main.c new file mode 100644 index 0000000..242b150 --- /dev/null +++ b/Advent-of-Code-2022/aoc-15/main.c @@ -0,0 +1,135 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> + +#if 0 + #define PART part1 +#else + #define PART part2 +#endif + +#if 0 + #define FILENAME "sample.txt" + #define TARGET_Y 10 + #define UPPER 20 +#else + #define FILENAME "input.txt" + #define TARGET_Y 2000000 + #define UPPER 2000000 +#endif + +#define LOWER 1000000 + +#define ENTRIES_CAP 30 +#define CAP 10000000 +#define OFFSET (CAP/2) + +int sensors[CAP][2]; +int beacons[CAP][2]; +size_t entries = 0; + +int obj[CAP]; +int row[CAP]; + +size_t sum = 0; + +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) + +int taxi_distance(int x1, int y1, int x2, int y2) +{ + return abs(x1 - x2) + abs(y1 - y2); +} + +void analyze(int target_y) +{ + memset(obj, 0, sizeof(obj)); + memset(row, 0, sizeof(row)); + + for(size_t i = 0; i < entries; i++) + { + int x1 = sensors[i][0]; int x2 = beacons[i][0]; + int y1 = sensors[i][1]; int y2 = beacons[i][1]; + + int r = taxi_distance(x1, y1, x2, y2); + int diff = abs(target_y - y1); + for(int i = x1 - (r - diff); i <= x1 + (r - diff); i++) { + row[i + OFFSET] = 1; + } + + if(y2 == target_y) obj[x2 + OFFSET] = 1; + } +} + +void part1() +{ + analyze(TARGET_Y); + + for(size_t i = 0; i < CAP; i++) + if(row[i] - obj[i] > 0) sum++; +} + +void part2() +{ + for(int y = LOWER; y <= UPPER; y++) + { + printf("%d\n", y); + analyze(y); + + for(int x = LOWER; x <= UPPER; x++) + if(row[x + OFFSET] == 0 && obj[x + OFFSET] == 0) { + sum = (x * 4000000) + y; + return; + } + } + + exit(1); +} + +void parse_xy(char *str, int *x, int *y, int nth) +{ + char *s, *sx; + char *y_str, *x_str; + + x_str = strtok_r(strtok_r(str, ",", &s), " ", &sx); + y_str = strtok_r(NULL, ",", &s); + + for(int i = 1; i < nth; i++) + x_str = strtok_r(NULL, " ", &sx); + + assert(x_str != NULL); + assert(y_str != NULL); + + *x = atoi(&(x_str[2])); + *y = atoi(&(y_str[3])); +} + +void parse() +{ + FILE *fp = fopen(FILENAME, "r"); + if(!fp) { + fprintf(stderr, "ERROR: Could not open file: %s\n", FILENAME); + exit(1); + } + + char line[512]; + while(fgets(line, sizeof(line), fp)) + { + size_t len = strlen(line); + if(line[len-1] == '\n') line[len-1] = 0; + + parse_xy(strtok(line, ":"), &sensors[entries][0], &sensors[entries][1], 3); + parse_xy(strtok(NULL, ":"), &beacons[entries][0], &beacons[entries][1], 5); + entries++; + } + + fclose(fp); +} + +int main(void) +{ + parse(); + PART(); + printf("%ld\n", sum); + return 0; +} diff --git a/Advent-of-Code-2022/aoc-15/sample.txt b/Advent-of-Code-2022/aoc-15/sample.txt new file mode 100644 index 0000000..a612424 --- /dev/null +++ b/Advent-of-Code-2022/aoc-15/sample.txt @@ -0,0 +1,14 @@ +Sensor at x=2, y=18: closest beacon is at x=-2, y=15 +Sensor at x=9, y=16: closest beacon is at x=10, y=16 +Sensor at x=13, y=2: closest beacon is at x=15, y=3 +Sensor at x=12, y=14: closest beacon is at x=10, y=16 +Sensor at x=10, y=20: closest beacon is at x=10, y=16 +Sensor at x=14, y=17: closest beacon is at x=10, y=16 +Sensor at x=8, y=7: closest beacon is at x=2, y=10 +Sensor at x=2, y=0: closest beacon is at x=2, y=10 +Sensor at x=0, y=11: closest beacon is at x=2, y=10 +Sensor at x=20, y=14: closest beacon is at x=25, y=17 +Sensor at x=17, y=20: closest beacon is at x=21, y=22 +Sensor at x=16, y=7: closest beacon is at x=15, y=3 +Sensor at x=14, y=3: closest beacon is at x=15, y=3 +Sensor at x=20, y=1: closest beacon is at x=15, y=3 diff --git a/Advent-of-Code-2022/aoc-25/build.sh b/Advent-of-Code-2022/aoc-25/build.sh new file mode 100755 index 0000000..30f495e --- /dev/null +++ b/Advent-of-Code-2022/aoc-25/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +set -xe + +gcc -o main main.c -Wall -Wextra -pedantic diff --git a/Advent-of-Code-2022/aoc-25/input.txt b/Advent-of-Code-2022/aoc-25/input.txt new file mode 100644 index 0000000..8e7a4d1 --- /dev/null +++ b/Advent-of-Code-2022/aoc-25/input.txt @@ -0,0 +1,113 @@ +1=0-12100-== +12=2=21=2-1=1122 +1=-1112=-12 +1--2210 +2-1110- +1=-1- +1112 +220--0 +20=--==0 +12-00- +21=21001-0 +1=101-=111-0==-- +2 +10-=1==-102-020== +2=12 +11=02=11-=2=2 +1=--2111010- +12210=1-=- +221=-=210-2=0=00- +11012 +22002====21 +1-202202--2-2 +1-=110-- +1021- +10-1-- +100-11=--=020 +2=01-=2-200- +10--11201011 +1-=- +10=-122-00=-0110 +122-2 +120-01--012-1202- +22=2=1=10221-=2 +1-=1 +1122-=- +1=1-02101- +1-=1=2-211 +12221-1-00 +2--10=-0-1===2 +120120=2110121020- +22=2-2=0 +1=2=0=201221101-== +20=-122 +211=1122=21=101 +1=0-2020==102=== +10-1-00 +1=221202=22 +1=20-0=12==- +1-2=0120=2001-0101 +11--000- +11 +21-00221020-12 +1--0=2==10=-=00 +121001=2000 +2=20--2=-0=-=2-11-0 +1=0=1120= +1=-=0 +10-2 +21-0=0 +2=-=-0 +1=-0-2= +1=2--112=10--1=2 +2---20=-10-00 +101020110 +102=12-1022==01-0 +10=---11 +2-=--00100=- +122=-=--- +1---==0012=110= +12= +10-20020-==0=1 +1022==2=2- +1212-02--=2=-0-210 +1121201=01-==2 +1=1==20=-1= +1=- +212=0=21110 +21==20- +1=-=-111020---21--= +111=0---=2=2 +112 +1--=11101==022202-1 +1=1=-=0-1-1=00-2 +1=01020=1 +102-220-2 +1--1=022020 +21-01= +1-0--=011 +2010 +10==1 +12=222-22--2021-21 +1=1121-2-1-201= +2211 +2=220-=2=-2=-1 +1=10-1 +1-12-022101=11-22=-2 +11200==1 +1=1-0100=--21201-0 +2=2=2-2==-- +112-= +120 +212222-0=1111=21-= +100-02---0110 +2202212-02-10 +1-=0101-1=2 +2101110111-- +1=11-21122==12- +2-0=200=011==-12= +2==-0022-20 +1==001211=22201== +12211=10-0= +1-00-101-==- +1-=1=222000=2== diff --git a/Advent-of-Code-2022/aoc-25/main.c b/Advent-of-Code-2022/aoc-25/main.c new file mode 100644 index 0000000..c88d918 --- /dev/null +++ b/Advent-of-Code-2022/aoc-25/main.c @@ -0,0 +1,89 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#if 0 + #define FILENAME "sample.txt" +#else + #define FILENAME "input.txt" +#endif + +int char_to_int[256] = { + ['1'] = 1, + ['2'] = 2, + ['0'] = 0, + ['-'] = -1, + ['='] = -2 +}; + +char int_to_char[5] = { + '=', '-', '0', '1', '2' +}; + +long long sum = 0; + +void encode() +{ + signed char num[1000] = {0}; + size_t num_sz = 0; + + for(long long n = sum; n > 0; n /= 5, num_sz++) { + num[num_sz] += n % 5; + num[num_sz+1] += num[num_sz]/5; + num[num_sz] %= 5; + + if(num[num_sz] == 3) { + num[num_sz] = -2; + num[num_sz+1] += 1; + } else if(num[num_sz] == 4) { + num[num_sz] = -1; + num[num_sz+1] += 1; + } + } + + for(int i = num_sz-1; i >= 0; i--) { + printf("%c", int_to_char[num[i]+2]); + } + + printf("\n"); +} + +long long decode(char *num) +{ + long long n = 0; + + size_t len = strlen(num); + long long power_of_5 = 1; + + for(int i = len-1; i >= 0; i--) { + n += power_of_5 * char_to_int[num[i]]; + power_of_5 *= 5; + } + + return n; +} + +void parse() +{ + FILE *fp = fopen(FILENAME, "r"); + if(!fp) { + fprintf(stderr, "ERROR: Could not open file %s\n", FILENAME); + exit(1); + } + + char line[256]; + while(fgets(line, sizeof(line), fp) != NULL) { + if(line[strlen(line)-1] == '\n') + line[strlen(line)-1] = '\0'; + sum += decode(line); + } + + fclose(fp); +} + +int main(void) +{ + parse(); + encode(); + return 0; +} diff --git a/Advent-of-Code-2022/aoc-25/sample.txt b/Advent-of-Code-2022/aoc-25/sample.txt new file mode 100644 index 0000000..027aeec --- /dev/null +++ b/Advent-of-Code-2022/aoc-25/sample.txt @@ -0,0 +1,13 @@ +1=-0-2 +12111 +2=0= +21 +2=01 +111 +20012 +112 +1=-1= +1-12 +12 +1= +122 |