commit 9a77efd40045b6698167f35d0b6a7dafb076b1b6
parent 5588ce9f309f4a64f649b189692cb17a076573d0
Author: Louis Burda <quent.burda@gmail.com>
Date: Fri, 17 Mar 2023 16:43:14 +0100
Fixup day 3
Diffstat:
3 files changed, 48 insertions(+), 51 deletions(-)
diff --git a/03/Makefile b/03/Makefile
@@ -1,12 +0,0 @@
-CFLAGS = -g -I ../../libs/include -L ../../libs/build
-LDLIBS = -laoc
-
-all: lib main
-
-clean:
- rm main
-
-lib:
- make -C ../../libs
-
-main: main.c
diff --git a/03/info.mk b/03/info.mk
@@ -0,0 +1,2 @@
+03_SRC = 03/main.c common/main.c common/aoc.c common/util.c
+03_HDR = common/aoc.h common/util.h
diff --git a/03/main.c b/03/main.c
@@ -1,9 +1,13 @@
#include "aoc.h"
+#include "util.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#define MAXA(a, b) (ABS(a) > ABS(b) ? (a) : (b))
+#define MINA(a, b) (ABS(a) > ABS(b) ? (b) : (a))
+
struct line {
int sx, sy;
int ex, ey;
@@ -12,33 +16,36 @@ struct line {
};
struct line *
-read_lines(char **input)
+parse_lines(const char **pos, const char *end)
{
- struct line *lines, **iter;
- char *tok, *next, *end;
+ char buf[2048], part[64];
+ const char *lpos, *lend;
+ struct line *line, *start, **iter;
int x, y, val;
- debug("START\n");
+ aoc_debug("START\n");
+
+ if (!readtok(buf, sizeof(buf), '\n', pos, end))
+ die("parse_lines: end of file");
- end = CHKP(strchr(*input, '\n'));
+ start = line = NULL;
+ iter = &line;
x = y = 0;
- tok = *input;
- lines = NULL;
- iter = &lines;
- while (next != end) {
- next = strchr(tok, ',');
- if (next > end || !next) next = end;
- *iter = CHKP(malloc(sizeof(struct line)));
- (*iter)->next = NULL;
-
- *next = '\0';
- val = atoi(tok+1);
-
- (*iter)->sx = x;
- (*iter)->sy = y;
-
- switch (*tok) {
+ lpos = buf;
+ lend = buf + strlen(buf);
+ while (readtok(part, sizeof(part), ',', &lpos, lend)) {
+ *iter = line = malloc(sizeof(struct line));
+ if (!line) die("parse_lines: oom");
+
+ if (!start) start = line;
+
+ line->next = NULL;
+ line->sx = x;
+ line->sy = y;
+
+ val = parsei64(part+1);
+ switch (part[0]) {
case 'L':
x -= val;
break;
@@ -52,22 +59,20 @@ read_lines(char **input)
y += val;
break;
default:
- die("Invalid move: %s\n", tok);
+ die("parse_lines: invalid move: %s", part);
}
- debug("%i %i\n", x, y);
+ aoc_debug("%i %i\n", x, y);
- (*iter)->ex = x;
- (*iter)->ey = y;
+ line->ex = x;
+ line->ey = y;
iter = &(*iter)->next;
- tok = next + 1;
}
- debug("END\n");
+ aoc_debug("END\n");
- *input = end + 1;
- return lines;
+ return start;
}
int
@@ -139,12 +144,12 @@ calc_dist(struct line *lines, struct line *end, int x, int y)
for (iter = lines; iter != end; iter = iter->next) {
dist += ABS(iter->ex - iter->sx);
dist += ABS(iter->ey - iter->sy);
- debug("D: %i\n", dist);
+ aoc_debug("D: %i\n", dist);
}
dist += ABS(x - iter->sx);
dist += ABS(y - iter->sy);
- debug("D: %i\n", dist);
+ aoc_debug("D: %i\n", dist);
return dist;
}
@@ -154,13 +159,14 @@ part1(void)
{
struct line *lines1, *lines2;
struct line *iter1, *iter2;
+ const char *pos, *end;
int nx, ny, hit;
- char *pos;
int x, y;
pos = aoc.input;
- lines1 = read_lines(&pos);
- lines2 = read_lines(&pos);
+ end = aoc.input + aoc.input_size;
+ lines1 = parse_lines(&pos, end);
+ lines2 = parse_lines(&pos, end);
x = y = 0;
for (iter1 = lines1; iter1; iter1 = iter1->next) {
@@ -175,7 +181,7 @@ part1(void)
}
}
- printf("dist(%i, %i) = %i\n", x, y, ABS(x) + ABS(y));
+ aoc_debug("dist(%i, %i) = %i\n", x, y, ABS(x) + ABS(y));
aoc.answer = aprintf("%i", ABS(x) + ABS(y));
aoc.solution = "209";
}
@@ -185,13 +191,14 @@ part2(void)
{
struct line *lines1, *lines2;
struct line *iter1, *iter2;
+ const char *pos, *end;
int nx, ny, nd, hit;
int x, y, d;
- char *pos;
pos = aoc.input;
- lines1 = read_lines(&pos);
- lines2 = read_lines(&pos);
+ end = aoc.input + aoc.input_size;
+ lines1 = parse_lines(&pos, end);
+ lines2 = parse_lines(&pos, end);
x = y = 0;
for (iter1 = lines1; iter1; iter1 = iter1->next) {
@@ -209,7 +216,7 @@ part2(void)
}
}
- printf("steps(%i, %i) = %i\n", x, y, d);
+ aoc_debug("steps(%i, %i) = %i\n", x, y, d);
aoc.answer = aprintf("%i", d);
aoc.solution = "43258";
}