diff --git a/.gitignore b/.gitignore index cba7efc..86c5021 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ a.out +build/ +*.o diff --git a/06/_.h b/06/_.h new file mode 100644 index 0000000..c0a4ec4 --- /dev/null +++ b/06/_.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) +#define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) +#define ABS(X) ((X) >= 0 ? (X) : -(X)) + +char** str_split(char* a_str, const char a_delim); + +int str_index(char *str, char c); + +int number_of_element(char *str, char c); + +long gmc(long a, long b); +long lmc(long a, long b); +long lmcs(int array[], int n); + +long strslen(char **strs); diff --git a/06/main.txt b/06/main.txt new file mode 100644 index 0000000..e2b0649 --- /dev/null +++ b/06/main.txt @@ -0,0 +1,130 @@ +......#........#..........#.................##......................#.............#..................#............#............#.. +.....#............................................#.......................#.......##.................#..#...................#.#... +......#...................#............................#.....#.........#................................................#......... +...........................................#.......#..........#..#......#.........#.#.#.#................#.##........##........... +........#.........................................#.......#.....#..........#...#....##....#..#.......##................#......#... +...................#..#.....................................................#.................................................#... +....#..................#..........#........#.............#................................................................#....... +......#..............#..................#...........#......#.....#...........................#..#.#.....................#.....#... +......#...#...............................................#.....#.........................#..........#............................ +......#......#..........................................#..................................#....#..#................#..##...#..... +........................................................................................#......................................... +................#..........#..........................#.##......................#..........#...................................... +.............#.............................#......#..............................#....................#...........#.....#...#..... +.......#.................#..................#..........................#...........................................#.............. +.......#.....................................................#..........................................#....#.#.....#......#..... +...#......#............#........#......................#...........................#.#....#..#....................#.............## +...........................##..#..#..........#....................#................................................#......#....... +.....................#...#.......................#...#..#.....#................#...........................................#....#. +.................#................#.............#...#...........................................................#..#.#............ +#.....................#....................##.......#...................#..........#..........................#................... +.#..........##......#...#......#................#.....................................#.#.....#..............#.................... +...#...#..#..........................................................#..#..#...................................................... +..................#.#..............#...#...............................#...#....#.#.........#.........................##.......... +.......#.........................#..............................................#........#....#....#.........#.................... +...........#......#......#...........#........#.............................##.................................................#.. +............................#...................................#......#........................................#................. +......................................##...........................................#.............#..#...........#...#.....#....... +...................................................#.................................#............................................ +..........................#..........................................#..#.....#.................#.#......#........................ +..................#...........#.............................#....#.#..................#................................#.......... +...................#....#..#.........................................................#....#....................................... +..#..........................................................##..................................#......................#....#.#.. +#.........................#.....#...............................#........................#................#....................... +......................#......#...#..........#......#..............................#...#......#.................................#.. +...............#.....#............................................................................#............................... +#.....................#........#.#........................................................#.......................#.............#. +.......#...#..........#........#...........#.................#..................#....................................##.....#..... +.#......#..................................#.......................................................................#.............. +.................#.....#...........................................................#.............................................. +.........#....................................^......#..................#..............................#..#.............#...#..... +.............................................................#.....#....................................#......................... +....................#.................#........#........................................................#..#............#........# +.....#..........................................................#...........................#.................#....##............. +..............#.........#.................#............................#.............#......................#.#............#...... +.............#.................#.........................................#...#................#.............#.....#............... +....#.....##..#......#........#............................................#..........#.......#.................................## +..........................................................................#....................................................... +.......#..........#.............................................................................................##..#............. +............................#...............#...............................................................................#..... +....#......#...............................................................................................................#...... +..................................#......................#.....#............................................#.....#........#...... +...............................#..........................................#....................................................... +.....#.............#...#..............#.#............#....................................................................#....... +..............................................#...............................#......##.......#................#..........#.#..... +........#.....##...............................................#.........................#........................................ +.....#.........................#.#........#.............................................#.............#..............##........... +........................................#..##.........................#............#...........#.............#.................... +.....................#.#................#............................#.................#.....#..............................#..... +................#.....................................#..#....................#..................................#................ +...........#...................#.................#..........................................................#.......#...........#. +...........................#..................................#......#..............#............#.........#....#................# +..................................................................................................................#............... +..............#....#.#............................................................................#.....#.........#........#...... +..#...........#.................#.#......#................................#..........................#...........................# +............#...........##...............................#..................................................#..................... +..............##.#..................................................................................#.........#......#......#..... +................................................................#...............#....#....##................#..................... +.........#..#.....................#........................................................#......#................#.............. +...#..........#..........#..................#.......................#........................#...#................#.........#..... +...................................#..#...............................#..........#................................................ +.........................#........................................................................................................ +......................#.........#....................................................##............................#.....#........ +.#....#.....................#.........................................................#....#........#............................. +..........................................#....................................#........................................#......... +.................................................#.......................................#........................................ +#..#...............#......................#....#.............................................#................#........##......... +.........#....#........##.........#.....#.........#...............................................#..................#....#...#... +..#............................................................................#...............................................#.. +.......................................................#.........#......#..........................#.#............................ +.........#.............#.................##.....#.....#.........#............#.........#..................#.....#.............#... +#..........#.........#...............................................................................................#............ +.......................#....................#..................................................#.................................. +.#................#.................................#........................................#...........#........................ +.........#........................................................................................................................ +..................................................................................#..........#.....................#.............. +..#...............................#.......#.....................#......................#.....#..........................#......... +......#....#..........................................................#..........................#................................ +...........................................##..................................................##..................#...#.......... +...............#....#...........................#........................................#..............#....................##... +#......#......................................................#..........#.........................#.........#.................... +...#...................................#......#..........................................#.........................#.............# +#.....#.........#.............#.......................................#...............................#........................... +...................#.#..........................................................................#...............#................. +.......#......##..........#....................................................#..................#..........#.................... +.............................................#.##..............#......................#......#..#..................#..........#... +..##...................#...................................#.........................................#...........#................ +...................................................#.....................................#.......................#...............# +..........#.......##...................#...............#...............................................................#.......... +.......................#............#............##.............................................#......##.................#...#... +.........#.................................................................................#............#....................#.... +.......##.........#..................................................................#................#........................... +................#.............................................................................................#................... +......................#.......#....#.................................................#......#.#...............................#... +.........#.........#.#.....##.#..................................##...................................................#........... +.#......................................................#.......#......##........#..........#.........#....#...................... +........#.#......................#................................................................................................ +..................#.............................#.................#..................#......................#......##.#........... +................##.............................................#....#..........##.#.#....#........................................ +..............#...................#.......#............................#.#.....................................................#.. +............#..............................................#....#.....................#...#.......................#.....#......#.. +..............#........................................................#............................................#............. +.......#.....#.........#..............#..............................#.......................#..#................#....#..#........ +.......................#......#........................................#.................#..........#............................. +...........#..................#.............................#............................................................#........ +......#....#......#.....................#.....................#........#.....................#...#.....#.#...................#.... +#..............................................##...#.....#...........#..........................................#................ +..........#..........#.#........................................#..#....#......................................................... +..................#......................................#...#.......#............................................................ +...#.............................#..................................................#...........#................................. +................................##.#.............#........................#...#...............................................#... +...#...................................#.......................#........................#............#...............#....#.#..... +..............................................................##.......#...............#.........#..................#............. +....#............................#..................................#..#..#...............#..............##....................... +#...#.................##...............................#...........#...........#..............#.#.........#........#...#.......... +..........................#.....#....................#..............#.............#..........#....................#.........#..... +......#.......................................................#........#.....#..........................#...#.........#........... +...............................#............#.....#...........................#...............#........#.......................... +............#..#......#...#..#.....................#...............#.........#...........................................#.....#.. +.......#..#..................#............#...........#..............................................................#............ +....#......#.##..#......##..........#.......#............#...............#....#....................................#..........#.#. diff --git a/06/mylib.c b/06/mylib.c new file mode 100644 index 0000000..87fbf66 --- /dev/null +++ b/06/mylib.c @@ -0,0 +1,91 @@ +#include "_.h" + +// NOT MY CODE +// https://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c +char** str_split(char* str, const char a_delim) +{ + char *a_str = strdup(str); + char** result = 0; + size_t count = 0; + char* tmp = a_str; + char* last_comma = 0; + char delim[2]; + delim[0] = a_delim; + delim[1] = 0; + + while (*tmp) + { + if (a_delim == *tmp) + { + count++; + last_comma = tmp; + } + tmp++; + } + + count += last_comma < (a_str + strlen(a_str) - 1); + + count++; + + result = malloc(sizeof(char*) * count); + + if (result) + { + size_t idx = 0; + char* token = strtok(a_str, delim); + + while (token) + { + //assert(idx < count); + *(result + idx++) = strdup(token); + token = strtok(0, delim); + } + //assert(idx == count - 1); + *(result + idx) = 0; + } + + free(a_str); + return result; +} + +int str_index(char *str, char c) { + + int i = 0; + while (str[i]) + if (str[i++] == c) + return i; + + return -1; +} + +int number_of_element(char *str, char c) { + int nbr = 0; + for (int i = 0; str[i] != '\0'; i++) + if (str[i] == c) + nbr++; + return nbr; +} + +long gmc(long a, long b) { + if (b == 0) + return a; + return gmc(b, a % b); +} + +long lmc(long a, long b) { + return (a / gmc(a, b)) * b; +} + +long lmcs(int array[], int n) { + long result = array[0]; + for (int i = 1; i < n; i++) + result = lmc(result, array[i]); + return result; +} + +long strslen(char **strs) { + long count = 0; + while (strs[count]) + count++; + return count; +} diff --git a/06/part1.c b/06/part1.c new file mode 100644 index 0000000..ab7bebe --- /dev/null +++ b/06/part1.c @@ -0,0 +1,72 @@ +#include "_.h" + +int main(int argc, char **argv) { + + if (argc != 2) + return 1; + + char *input = argv[1]; + char **inputs = str_split(strdup(input), '\n'); + + int x = 0, y = 0; + for (int i = 0; inputs[i] != NULL; i++) + for (int j = 0; inputs[i][j] != '\0'; j++) + if (inputs[i][j] == '^') { + y = i; + x = j; + } + + int direction = 0; // 0 = top; 1 = right; 2 = down; 3 = left + while (1) { + if (direction == 0) { + if (inputs[y - 1] == NULL) + break; + if (inputs[y - 1][x] == '#') + direction++; + else { + inputs[y][x] = 'X'; + y--; + } + } + else if (direction == 1) { + if (x + 1 >= strlen(inputs[y])) + break; + if (inputs[y][x + 1] == '#') + direction++; + else { + inputs[y][x] = 'X'; + x++; + } + } + else if (direction == 2) { + if (inputs[y + 1] == NULL) + break; + if (inputs[y + 1][x] == '#') + direction++; + else { + inputs[y][x] = 'X'; + y++; + } + } + else if (direction == 3) { + if (x - 1 < 0) + break; + if (inputs[y][x - 1] == '#') + direction = 0; + else { + inputs[y][x] = 'X'; + x--; + } + } + } + + int count = 1; + for (int i = 0; inputs[i] != NULL; i++) + for (int j = 0; inputs[i][j] != '\0'; j++) + if (inputs[i][j] == 'X') + count++; + + printf("count: %d\n", count); + + return 0; +} diff --git a/template/part1.sh b/06/part1.sh old mode 100644 new mode 100755 similarity index 100% rename from template/part1.sh rename to 06/part1.sh diff --git a/06/part2.c b/06/part2.c new file mode 100644 index 0000000..82b07e0 --- /dev/null +++ b/06/part2.c @@ -0,0 +1,78 @@ +#include "_.h" + +int its_infinite(char **inputs, int x, int y) { + + int i = 0; + int direction = 0; // 0 = top; 1 = right; 2 = down; 3 = left + for (; i < 100000; i++) { + if (direction == 0) { + if (y - 1 < 0) + return 0; + if (inputs[y - 1][x] == '#') + direction++; + else + y--; + } + else if (direction == 1) { + if (x + 1 >= strlen(inputs[y])) + return 0; + if (inputs[y][x + 1] == '#') + direction++; + else + x++; + } + else if (direction == 2) { + if (inputs[y + 1] == NULL) + return 0; + if (inputs[y + 1][x] == '#') + direction++; + else + y++; + } + else if (direction == 3) { + if (x - 1 < 0) + return 0; + if (inputs[y][x - 1] == '#') + direction = 0; + else + x--; + } + } + + return 1; +} + +int main(int argc, char **argv) { + + if (argc != 2) + return 1; + + char *input = argv[1]; + char **inputs = str_split(strdup(input), '\n'); + + int x = 0, y = 0; + for (int i = 0; inputs[i] != NULL; i++) + for (int j = 0; inputs[i][j] != '\0'; j++) + if (inputs[i][j] == '^') { + y = i; + x = j; + } + + int count = 0; + for (int i = 0; inputs[i] != NULL; i++) { + for (int j = 0; inputs[i][j] != '\0'; j++) { + int replace = 0; + if (inputs[i][j] == '#') + replace = 1; + inputs[i][j] = '#'; + count += its_infinite(inputs, x, y); + inputs[i][j] = '.'; + if (replace) + inputs[i][j] = '#'; + } + } + + printf("count: %d\n", count); + + return 0; +} diff --git a/template/part2.sh b/06/part2.sh old mode 100644 new mode 100755 similarity index 100% rename from template/part2.sh rename to 06/part2.sh diff --git a/06/test.txt b/06/test.txt new file mode 100644 index 0000000..a4eb402 --- /dev/null +++ b/06/test.txt @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... diff --git a/template/Makefile b/template/Makefile new file mode 100644 index 0000000..1ad3e1c --- /dev/null +++ b/template/Makefile @@ -0,0 +1,41 @@ +CC := gcc +LD := $(CC) +LIBS := -lm +LDFLAGS := +CFLAGS := -Wall -Wextra -Wno-override-init -std=c2x -include_.h +SRC := part1.c mylib.c +ifeq ($(PART),2) + SRC := part2.c mylib.c +endif +OBJ_DIR := build +OBJ := $(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o,$(SRC))) +DIR := . +RAW_NAME:= a.out +NAME := $(RAW_NAME) + +ifeq ($(RELEASE),1) + CFLAGS += -O3 + LDFLAGS += -O3 -s +else + CFLAGS += -O0 -g3 + LDFLAGS += -O0 -g +endif + +all: $(NAME) + +$(NAME): $(OBJ) + $(LD) $(LDFLAGS) -o $(DIR)/$(NAME) $(OBJ) $(LIBS) + +$(OBJ_DIR)/%.o: %.c + @mkdir -p $(@D) + $(CC) $(CFLAGS) -c -o $@ $< + +clean: + @rm -rf $(OBJ_DIR) + @rm -rf $(RAW_NAME) + +re: + @make --no-print clean + @make --no-print all + +.PHONY: all clean re diff --git a/template/part2.c b/template/part2.c index e69de29..c3c8116 100644 --- a/template/part2.c +++ b/template/part2.c @@ -0,0 +1,16 @@ +#include "_.h" + +int main(int argc, char **argv) { + + if (argc != 2) + return 1; + + char *input = argv[1]; + char **inputs = str_split(strdup(input), '\n'); + + for (int i = 0; inputs[i] != NULL; i++) { + + } + + return 0; +}