Add 2021 progress

This commit is contained in:
Alex D. 2021-12-02 08:51:35 +00:00
parent d5d7e98027
commit 49a4db6542
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
8 changed files with 3282 additions and 0 deletions

14
2021/1/1/main.c Normal file
View File

@ -0,0 +1,14 @@
#include "input.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int
main (void) {
uintmax_t cnt = 0, i;
for (i = 1; i < sizeof (input) / sizeof (*input); i++) {
if (input[i] > input[i - 1]) cnt++;
}
printf ("%ju\n", cnt);
}

19
2021/1/2/main.c Normal file
View File

@ -0,0 +1,19 @@
#include "input.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
static inline uintmax_t
last_three_sum (const uintmax_t *x) {
return (*(x - 2) + *(x - 1) + *(x));
}
int
main (void) {
uintmax_t cnt = 0, i;
for (i = 3; i < sizeof (input) / sizeof (*input); i++) {
if (last_three_sum (&input[i]) > last_three_sum (&input[i - 1])) cnt++;
}
printf ("%ju\n", cnt);
}

2003
2021/1/input.h Normal file

File diff suppressed because it is too large Load Diff

92
2021/2/1/main.c Normal file
View File

@ -0,0 +1,92 @@
#define _POSIX_C_SOURCE 200809L
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
enum command {
UNK = 0,
FORWARD,
DOWN,
UP,
};
struct act {
enum command cmd;
uintmax_t cnt;
};
static int
carray (FILE *f, struct act *res) {
char * str = NULL;
size_t bs = 0;
ssize_t b = getline (&str, &bs, f);
if (b == -1) {
fprintf (stderr, "getline returned a error\n");
return 1;
}
char *act = strtok (str, " "),
*cnt = strtok (NULL, "\n");
if (act == NULL) {
fprintf (stderr, "Action is empty.\n");
return 1;
}
if (strcmp (act, "forward") == 0) {
res->cmd = FORWARD;
} else if (strcmp (act, "down") == 0) {
res->cmd = DOWN;
} else if (strcmp (act, "up") == 0) {
res->cmd = UP;
} else {
fprintf (stderr, "Line couldn't be parsed.\n");
return 2;
}
res->cnt = strtoumax (cnt, NULL, 10);
return 0;
}
int
main (void) {
struct {
uintmax_t depth, pos;
} axis = {
.depth = 0,
.pos = 0,
};
struct act cur = {0};
while (carray (stdin, &cur) == 0) {
switch (cur.cmd) {
case FORWARD:
fprintf (stderr, "Added %zu to pos\n", cur.cnt);
axis.pos += cur.cnt;
break;
case DOWN:
fprintf (stderr, "Added %zu to depth\n", cur.cnt);
axis.depth += cur.cnt;
break;
case UP:
fprintf (stderr, "Subtracted %zu from depth\n", cur.cnt);
axis.depth -= cur.cnt;
break;
default: {
fprintf (stderr, "Invalid action was parsed.\n");
return 1;
}
}
}
printf ("D: %zu\n"
"P: %zu\n"
"C: %zu\n",
axis.depth,
axis.pos,
axis.pos * axis.depth);
return 0;
}

94
2021/2/2/main.c Normal file
View File

@ -0,0 +1,94 @@
#define _POSIX_C_SOURCE 200809L
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
enum command {
UNK = 0,
FORWARD,
DOWN,
UP,
};
struct act {
enum command cmd;
uintmax_t cnt;
};
static int
carray (FILE *f, struct act *res) {
char * str = NULL;
size_t bs = 0;
ssize_t b = getline (&str, &bs, f);
if (b == -1) {
fprintf (stderr, "getline returned a error\n");
return 1;
}
char *act = strtok (str, " "),
*cnt = strtok (NULL, "\n");
if (act == NULL) {
fprintf (stderr, "Action is empty.\n");
return 1;
}
if (strcmp (act, "forward") == 0) {
res->cmd = FORWARD;
} else if (strcmp (act, "down") == 0) {
res->cmd = DOWN;
} else if (strcmp (act, "up") == 0) {
res->cmd = UP;
} else {
fprintf (stderr, "Line couldn't be parsed.\n");
return 2;
}
res->cnt = strtoumax (cnt, NULL, 10);
return 0;
}
int
main (void) {
struct {
uintmax_t depth, pos, aim;
} axis = {
.depth = 0,
.pos = 0,
.aim = 0,
};
struct act cur = {0};
while (carray (stdin, &cur) == 0) {
switch (cur.cmd) {
case FORWARD:
fprintf (stderr, "Added %zu to pos\n", cur.cnt);
axis.pos += cur.cnt;
axis.depth += axis.aim * cur.cnt;
break;
case DOWN:
fprintf (stderr, "Added %zu to depth\n", cur.cnt);
axis.aim += cur.cnt;
break;
case UP:
fprintf (stderr, "Subtracted %zu from depth\n", cur.cnt);
axis.aim -= cur.cnt;
break;
default: {
fprintf (stderr, "Invalid action was parsed.\n");
return 1;
}
}
}
printf ("D: %zu\n"
"P: %zu\n"
"C: %zu\n",
axis.depth,
axis.pos,
axis.pos * axis.depth);
return 0;
}

1000
2021/2/input Normal file

File diff suppressed because it is too large Load Diff

18
2021/Makefile Normal file
View File

@ -0,0 +1,18 @@
include config.mk
OBJ = ${SRC:.c=.o}
all: ${OBJ} ${OUT}
%.o: %.c
${CC} -o $@ -c ${CFLAGS} $<
clean:
rm -f ${OBJ} ${OUT}
ifneq (${OUT},)
${OUT}: ${OBJ}
${CC} -o "$@" ${LDFLAGS} $^
endif
.PHONY: all clean

42
2021/config.mk Normal file
View File

@ -0,0 +1,42 @@
# Defaults
CC = clang
DEBUG = 1
STATIC = 0
LDFLAGS =
CFLAGS =\
-std=c99\
-Weverything\
-Wno-padded\
-Wno-disabled-macro-expansion\
-pedantic
# Sources / Results
SRC =\
2/2/main.c
LIBDIR =
LIB =\
c
INCDIR = 2/
OUT = bin
# Conditionals / Appends
LDFLAGS +=\
$(addprefix -L,${LIBDIR})\
$(addprefix -l,${LIB})
CFLAGS +=\
$(addprefix -I,${INCDIR})
ifeq (${DEBUG},1)
CFLAGS += -g
else
CFLAGS += -O2 -Werror
endif
ifeq (${STATIC},1)
LDFLAGS += -static
endif