summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaa <kaa@disroot.org>2025-06-18 03:01:46 -0700
committerkaa <kaa@disroot.org>2025-06-18 03:01:46 -0700
commiteb1be90b94a1449e0444e7d912d5ccf314b5f10f (patch)
tree7a4be272df182576a6b70486a4d90bdac0ada5ec
parentf7c2841cdd3d5854ad4a1a53cb9627f4ba8dc805 (diff)
AOC 2024/10
-rwxr-xr-x2024/10/1bin0 -> 21872 bytes
-rw-r--r--2024/10/1.c177
-rw-r--r--2024/10/input40
-rw-r--r--2024/10/makefile1
-rw-r--r--2024/10/prompt68
-rw-r--r--2024/10/sample8
-rw-r--r--2024/10/sample_small4
-rw-r--r--2024/template/makefile1
8 files changed, 299 insertions, 0 deletions
diff --git a/2024/10/1 b/2024/10/1
new file mode 100755
index 0000000..e1f3adc
--- /dev/null
+++ b/2024/10/1
Binary files differ
diff --git a/2024/10/1.c b/2024/10/1.c
new file mode 100644
index 0000000..a8112a6
--- /dev/null
+++ b/2024/10/1.c
@@ -0,0 +1,177 @@
+#include <stdio.h>
+#include <stdbool.h>
+#include "../../d.h"
+#include "map.h"
+
+bool debug = true;
+
+typedef struct {
+ int width;
+ int height;
+ char** e;
+ bool** visited;
+} Graph2D;
+
+enum {
+ NORTH = 0, EAST, SOUTH, WEST, DIRECTIONS,
+};
+
+/* Reads from the input until exhausted. Returns memory which must be
+freed by the caller. */
+Graph2D*
+ingest(FILE* in)
+{
+ char *t = getall(in);
+ char *s = t;
+
+ /* Two passes. One to count width and height.
+ Another to store to allocated memory. */
+ int height = 0, width = -1, i = 0;
+ while (s[i] != '\0') {
+ if (s[i] == '\n') {
+ if (width == -1) {
+ width = i + 1;
+ }
+ height++;
+ }
+ i++;
+ }
+ char** buffer = calloc(height, sizeof(char*));
+ bool** visited = calloc(height, sizeof(bool*));
+
+ Graph2D* g = calloc(1, sizeof(Graph2D));
+ g->width = width;
+ g->height = height;
+ g->e = buffer;
+ g->visited = visited;
+
+ i = height;
+ while (i > 0) {
+ buffer[i-1] = calloc(width, sizeof(char));
+ visited[i-1] = calloc(width, sizeof(bool));
+ i--;
+ }
+
+ i = 0;
+ int j = 0;
+ while (i < height) {
+ if (j < width - 1) {
+ buffer[i][j] = *s;
+ j++;
+ } else {
+ i++;
+ j = 0;
+ }
+ s++;
+ }
+
+ free(t);
+ return g;
+}
+
+typedef struct {
+ int x;
+ int y;
+} Coordinates;
+
+typedef struct Stack {
+ void* value;
+ struct Stack* below;
+} Stack;
+
+Stack*
+pop(Stack* s) {
+ free(s->value);
+ Stack* tmp = s->below;
+ free(s);
+ return tmp;
+}
+Stack*
+push(Stack* s, void* value) {
+ Stack* t = calloc(1, sizeof(Stack));
+ t->below = s;
+ t->value = value;
+ return t;
+}
+
+bool
+valid(Graph2D* g, Coordinates c)
+{
+ return c.x >= 0 && c.x < g->width && c.y >= 0 && c.y < g->height;
+}
+
+int score = 0;
+void
+travel(Graph2D* g, Coordinates c)
+{
+ // g->visited[c.y][c.x] = true;
+
+ if (debug) {
+ printf("%d,%d\n", c.x, c.y);
+ printf("%c\n", g->e[c.y][c.x]);
+ }
+
+ Coordinates cardinal[DIRECTIONS];
+ cardinal[SOUTH] = (Coordinates) {c.x, c.y + 1};
+ cardinal[NORTH] = (Coordinates) {c.x, c.y - 1};
+ cardinal[EAST] = (Coordinates) {c.x + 1, c.y};
+ cardinal[WEST] = (Coordinates) {c.x - 1, c.y};
+
+ if (g->e[c.y][c.x] == '9') {
+ score++;
+ return;
+ }
+ for (int i = 0; i < DIRECTIONS; i++) {
+ if (valid(g, cardinal[i])) {
+ if (debug) {
+ printf("%d,%d\n", cardinal[i].x, cardinal[i].y);
+ printf("%d\n", g->e[cardinal[i].y][cardinal[i].x]);
+ printf("-\n");
+ printf("%d\n", g->e[c.y][c.x]);
+ printf("%d\n", g->e[cardinal[i].y][cardinal[i].x] - g->e[c.y][c.x]);
+ }
+
+ if (g->e[cardinal[i].y][cardinal[i].x] - g->e[c.y][c.x] == 1) {
+ if (debug) {
+ printf("Valid: %d,%d\n", cardinal[i].x, cardinal[i].y);
+ }
+ travel(g, cardinal[i]);
+ }
+ }
+ }
+ return;
+}
+
+int
+main()
+{
+ FILE* in = fopen("sample_small", "r");
+ if (in == NULL) {
+ exit(1);
+ }
+
+ Graph2D* g = ingest(in);
+
+ for (int i = 0; i < g->height; i++) {
+ for (int j = 0; j < g->width; j++) {
+ if (g->e[i][j] == '0') {
+ travel(g, (Coordinates) {j, i});
+ // Score is a global
+ printf("%d\n", score);
+ }
+ }
+ }
+
+ for (int i = 0; i < g->height; i++) {
+ for (int j = 0; j < g->width; j++) {
+ printf("%c", g->e[i][j]);
+ }
+ putchar('\n');
+ free(g->e[i]);
+ free(g->visited[i]);
+ }
+ free(g->e);
+ free(g->visited);
+ free(g);
+ return 0;
+}
diff --git a/2024/10/input b/2024/10/input
new file mode 100644
index 0000000..ebf45d3
--- /dev/null
+++ b/2024/10/input
@@ -0,0 +1,40 @@
+6541001098012789610347890107654656710323
+7832102127643898701256521218323465891410
+8996543034556789650987434309012534892565
+3887689678965876501874345892105621763676
+4305678563456903416765676756898760654980
+5214107852107812321254382347872108901221
+6543236943056921010341291078963457654338
+7896545987045430010980012569454968983549
+3217830656189899121676101430356879892678
+2106921043210778234585232321267898761432
+3478854430345665056798743410456901050501
+4569763521012552143895654501345012347670
+3654012678903443212104309690432167898981
+2783656987654874908765218781201254012567
+1092347897893965889034765670387063013498
+1001298756102456776121874989496122110901
+2310891043201307655430923876565434325892
+3456780103011218967649810189410145456743
+2561078212320989858236702107320236787654
+1232569343423874749145893678741199899873
+0343454358514565632098704569632087684562
+0456789969609034501347612189323456893001
+1499876878798123101256543079012548762110
+2387905462687678871212344568187659450223
+3456012301056549960305650127691098321054
+3456732102345832154454781034540107650169
+2369847898738981023763692321121256743278
+1078456654567670119832103400012349894361
+0012387763456543208041076510123412765010
+7650196892565454589107889623296503854321
+8943256781074303673236908774387654983432
+8912965890985210984365219985345015676541
+7607834187866789875434308776236723498650
+6506543045679012766923105698109894567743
+5410432134988703457810014567056210754892
+0322345028767845893456723459847349889701
+1201276719454936712679801210738256776545
+2450989805103221604589752345629145480230
+2347823456012120113298943238710076591121
+1056910147893012320107654109656789432012
diff --git a/2024/10/makefile b/2024/10/makefile
new file mode 100644
index 0000000..2c76089
--- /dev/null
+++ b/2024/10/makefile
@@ -0,0 +1 @@
+include ../../common.mk
diff --git a/2024/10/prompt b/2024/10/prompt
new file mode 100644
index 0000000..dfd456b
--- /dev/null
+++ b/2024/10/prompt
@@ -0,0 +1,68 @@
+--- Day 10: Hoof It ---
+
+You all arrive at a Lava Production Facility on a floating island in the sky. As the others begin to search the massive industrial complex, you feel a small nose boop your leg and look down to discover a reindeer wearing a hard hat.
+
+The reindeer is holding a book titled "Lava Island Hiking Guide". However, when you open the book, you discover that most of it seems to have been scorched by lava! As you're about to ask how you can help, the reindeer brings you a blank topographic map of the surrounding area (your puzzle input) and looks up at you excitedly.
+
+Perhaps you can help fill in the missing hiking trails?
+
+The topographic map indicates the height at each position using a scale from 0 (lowest) to 9 (highest). For example:
+
+0123
+1234
+8765
+9876
+
+Based on un-scorched scraps of the book, you determine that a good hiking trail is as long as possible and has an even, gradual, uphill slope. For all practical purposes, this means that a hiking trail is any path that starts at height 0, ends at height 9, and always increases by a height of exactly 1 at each step. Hiking trails never include diagonal steps - only up, down, left, or right (from the perspective of the map).
+
+You look up from the map and notice that the reindeer has helpfully begun to construct a small pile of pencils, markers, rulers, compasses, stickers, and other equipment you might need to update the map with hiking trails.
+
+A trailhead is any position that starts one or more hiking trails - here, these positions will always have height 0. Assembling more fragments of pages, you establish that a trailhead's score is the number of 9-height positions reachable from that trailhead via a hiking trail. In the above example, the single trailhead in the top left corner has a score of 1 because it can reach a single 9 (the one in the bottom left).
+
+This trailhead has a score of 2:
+
+...0...
+...1...
+...2...
+6543456
+7.....7
+8.....8
+9.....9
+
+(The positions marked . are impassable tiles to simplify these examples; they do not appear on your actual topographic map.)
+
+This trailhead has a score of 4 because every 9 is reachable via a hiking trail except the one immediately to the left of the trailhead:
+
+..90..9
+...1.98
+...2..7
+6543456
+765.987
+876....
+987....
+
+This topographic map contains two trailheads; the trailhead at the top has a score of 1, while the trailhead at the bottom has a score of 2:
+
+10..9..
+2...8..
+3...7..
+4567654
+...8..3
+...9..2
+.....01
+
+Here's a larger example:
+
+89010123
+78121874
+87430965
+96549874
+45678903
+32019012
+01329801
+10456732
+
+This larger example has 9 trailheads. Considering the trailheads in reading order, they have scores of 5, 6, 5, 3, 1, 3, 5, 3, and 5. Adding these scores together, the sum of the scores of all trailheads is 36.
+
+The reindeer gleefully carries over a protractor and adds it to the pile. What is the sum of the scores of all trailheads on your topographic map?
+
diff --git a/2024/10/sample b/2024/10/sample
new file mode 100644
index 0000000..cada9b3
--- /dev/null
+++ b/2024/10/sample
@@ -0,0 +1,8 @@
+89010123
+78121874
+87430965
+96549874
+45678903
+32019012
+01329801
+10456732
diff --git a/2024/10/sample_small b/2024/10/sample_small
new file mode 100644
index 0000000..a305b9d
--- /dev/null
+++ b/2024/10/sample_small
@@ -0,0 +1,4 @@
+0123
+1234
+8765
+9876
diff --git a/2024/template/makefile b/2024/template/makefile
new file mode 100644
index 0000000..2c76089
--- /dev/null
+++ b/2024/template/makefile
@@ -0,0 +1 @@
+include ../../common.mk