Should not be commit, but I need to share it. trunk
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sat, 23 Dec 2023 11:48:47 +0000 (12:48 +0100)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sat, 23 Dec 2023 11:48:47 +0000 (12:48 +0100)
10/10b.c [new file with mode: 0644]
11/11.c [new file with mode: 0644]
8/8b.c [new file with mode: 0644]
Libft

diff --git a/10/10b.c b/10/10b.c
new file mode 100644 (file)
index 0000000..a1de521
--- /dev/null
+++ b/10/10b.c
@@ -0,0 +1,164 @@
+
+#include "libft.h"
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+struct s_matrix
+{
+       size_t  rows;
+       size_t  cols;
+       char    *array;
+};
+typedef struct s_matrix        t_matrix;
+
+char   *get_element(t_matrix matrix, size_t i, size_t j)
+{
+       if (i >= matrix.rows || j >= matrix.cols)
+               return (NULL);
+       return (matrix.array + i * matrix.cols + j);
+}
+
+int    fill_matrix_size(t_matrix *matrix, const char *file_name)
+{
+       int             fd;
+       char    c;
+       char    *line;
+
+       matrix->rows = 1;
+       matrix->cols = 0;
+       fd = open(file_name, O_RDONLY);
+       if (fd < 0)
+               return (-1);
+       c = 'a';
+       while (c != '\n' && c != '\0')
+       {
+               read(fd, &c, 1);
+               ++matrix->cols;
+       }
+       --matrix->cols;
+       line = malloc(matrix->cols + 1);
+       while (read(fd, line, matrix->cols + 1))
+               ++matrix->rows;
+       close(fd);
+       free(line);
+       return (0);
+}
+
+t_matrix       read_as_matrix(const char *file_name)
+{
+       int                     fd;
+       t_matrix        matrix;
+       size_t          i;
+       size_t          j;
+       char            *line;
+
+       fill_matrix_size(&matrix, file_name);
+       fd = open(file_name, O_RDONLY);
+       if (fd < 0)
+               return (matrix);
+       matrix.array = malloc(matrix.rows * matrix.cols * sizeof(*matrix.array));
+       if (!matrix.array)
+               return (matrix);
+       line = malloc(matrix.cols + 1);
+       i = 0;
+       while (i < matrix.rows)
+       {
+               read(fd, line, matrix.cols + 1);
+               j = 0;
+               while (j < matrix.cols)
+               {
+                       *get_element(matrix, i, j) = line[j];
+                       ++j;
+               }
+               ++i;
+       }
+       free(line);
+       return (matrix);
+}
+
+int    to_single_on(int bits)
+{
+       size_t  i;
+
+       i = 0;
+       while (bits != 0)
+               bits &= (1 << i++);
+       return (1 << (i - 1));
+}
+
+/* Connected_sides legend
+ *       -2   
+ *        |
+ *        |
+ * -1----- -----1
+ *        |
+ *        |
+ *        2
+ */
+void   next_step(t_matrix sketch, size_t *i, size_t *j)
+{
+       static int      previous = 0;
+       int                     connected_sides;
+       char            *tile;
+       const char      orig = *get_element(sketch, *i, *j);
+
+       connected_sides = 0;
+       tile = get_element(sketch, *i - 1, *j);
+       if (tile && ft_strchr("|F7S#", *tile) && ft_strchr("S|LJ", orig))
+               connected_sides += -2;
+       tile = get_element(sketch, *i + 1, *j);
+       if (tile && ft_strchr("|LJS#", *tile) && ft_strchr("S|F7", orig))
+               connected_sides += 2;
+       tile = get_element(sketch, *i, *j - 1);
+       if (tile && ft_strchr("-FLS#", *tile) && ft_strchr("S-7J", orig))
+               connected_sides += -1;
+       tile = get_element(sketch, *i, *j + 1);
+       if (tile && ft_strchr("-7JS#", *tile) && ft_strchr("S-FL", orig))
+               connected_sides += 1;
+       connected_sides += previous;
+       if (previous == 0)
+               connected_sides = to_single_on(connected_sides);
+       *i += connected_sides / 2;
+       *j += connected_sides % 2;
+       previous = connected_sides;
+       return ;
+}
+
+void   color_loop(t_matrix sketch)
+{
+       size_t  i;
+       size_t  j;
+       size_t  i_p;
+       size_t  j_p;
+
+       i = 0;
+       j = 0;
+       while (*(get_element(sketch, i, j)) != 'S')
+       {
+               ++j;
+               i += j / sketch.cols;
+               j %= sketch.cols;
+       }
+       while (*(get_element(sketch, i, j)) != '#')
+       {
+               i_p = i;
+               j_p = j;
+               next_step(sketch, &i, &j);
+               *get_element(sketch, i_p, j_p) = '#';
+       }
+       return ;
+}
+
+int    main(int argc, char **argv)
+{
+       t_matrix        sketch;
+
+       if (argc != 2)
+               return (1);
+       sketch = read_as_matrix(argv[1]);
+       color_loop(sketch);
+       ft_printf("The loop encloses %i tiles.\n", enclosed_area(sketch));
+       free(sketch.array);
+       return (0);
+}
diff --git a/11/11.c b/11/11.c
new file mode 100644 (file)
index 0000000..767c833
--- /dev/null
+++ b/11/11.c
@@ -0,0 +1,31 @@
+
+#include "libft.h"
+
+typedef struct s_mat
+{
+       size_t  rows;
+       size_t  cols;
+       t_vec   vec;
+}
+
+void   *mat_access(t_mat *mat, size_t i, size_t j)
+{
+       if (!mat || i >= mat->rows || j >= mat->cols)
+               return (NULL);
+       return (ft_vec_access(&(mat->vec), i * mat->cols + j));
+}
+
+t_vec_status   mat_init(t_mat *mat, size_t     el_size)
+{
+       if (!mat)
+               return (invalid_input);
+       mat->rows = 0;
+       mat->cols = 0;
+       return (ft_vec_init(mat->vec, el_size));
+}
+
+t_vec-status   mat_insert_row(t_mat *mat, t_vec *vec, size_t row)
+{
+       if (!mat || !vec || mat->vec.el_size != vec->el_size)
+               return (invalid_input);
+       return ft_vec_insert(&(mat->vec), vec->vec, vec->size, row * mat->cols);
diff --git a/8/8b.c b/8/8b.c
new file mode 100644 (file)
index 0000000..27e6c61
--- /dev/null
+++ b/8/8b.c
@@ -0,0 +1,167 @@
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "libft.h"
+
+#define ID_LEN 3
+#define INIT_CAP 4
+
+struct s_snode
+{
+       char    id[ID_LEN];
+       char    id_l[ID_LEN];
+       char    id_r[ID_LEN];
+};
+typedef struct s_snode t_snode;
+
+struct s_inode
+{
+       char    id[ID_LEN];
+       size_t  index_l;
+       size_t  index_r;
+};
+typedef struct s_inode t_inode;
+
+union u_node
+{
+       t_snode snode;
+       t_inode inode;
+};
+typedef union u_node   t_node;
+
+void   translate_nodes(t_vec *str_map)
+{
+       char    left[ID_LEN];
+       char    right[ID_LEN];
+       size_t  i;
+       size_t  j;
+
+       i = 0;
+       while (i < str_map->size)
+       {
+               ft_memcpy(left, (t_node *)(str_map->vec)[i].snode.id_l, ID_LEN);
+               ft_memcpy(right, (t_node *)(str_map->array)[i].snode.id_r, ID_LEN);
+               j = 0;
+               while (j < str_map->size)
+               {
+                       if (!ft_strncmp((t_node *)(str_map->array)[j].snode.id, left, ID_LEN))
+                               (t_node *)(str_map->array)[i].inode.index_l = j;
+                       if (!ft_strncmp((t_node *)(str_map->array)[j].snode.id, right, ID_LEN))
+                               (t_node *)(str_map->array)[i].inode.index_r = j;
+                       ++j;
+               }
+               ++i;
+       }
+       return ;
+}
+
+t_snode        to_node(const char *line)
+{
+       t_snode res;
+
+       ft_memcpy(&res.id, line, ID_LEN);
+       ft_memcpy(&res.id_l, line + ID_LEN + 4, ID_LEN);
+       ft_memcpy(&res.id_r, line + 2 * ID_LEN + 6, ID_LEN);
+       return (res);
+}
+
+int    parse(const char *filename, t_vec *ind_map, char **inst)
+{
+       int                     fd;
+       char            *line;
+
+       fd = open(filename, O_RDONLY);
+       if (fd < 0)
+               return (2);
+       *inst = get_next_line(fd);
+       (*inst)[ft_strlen(*inst) - 1] = '\0';
+       free(get_next_line(fd));
+       line = get_next_line(fd);
+       while (line)
+       {
+               ft_vec_append(ind_map, &to_node(line));
+               free(line);
+               line = get_next_line(fd);
+       }
+       translate_nodes(ind_map);
+       close(fd);
+       return (0);
+}
+
+ssize_t        *find_inds_ending(const t_node_vec ind_map, char wanted)
+{
+       size_t  i;
+       size_t  res_size;
+       ssize_t *res;
+
+       i = 0;
+       res_size = 0;
+       while (i < ind_map.size)
+       {
+               if (((t_node *)(ind_map.vec)[i].inode.id[ID_LEN - 1] == wanted)
+                       ++res_size;
+               ++i;
+       }
+       res = ft_calloc(res_size + 1, sizeof(size_t));
+       if (!res)
+               return (res);
+       res[res_size] = -1;
+       i = 0;
+       res_size = 0;
+       while (i < ind_map.size)
+       {
+               if (ind_map.array[i].inode.id[ID_LEN - 1] == wanted)
+                       res[res_size++] = i;
+               ++i;
+       }
+       return (res);
+}
+int    any_not_ending(const t_node_vec ind_map, const ssize_t *inds, char end)
+{
+       while (*inds >= 0)
+       {
+               if (ind_map.array[*inds].inode.id[ID_LEN - 1] != end)
+                       return (1);
+               ++inds;
+       }
+       return (0);
+}
+
+unsigned long  steps_to_traverse(const t_vec ind_map, const char *inst, char end)
+{
+       size_t  i;
+       t_vec   steps;
+       t_vec   node;
+
+       ft_vec_init(&node, sizeof(char[ID_LEN]));
+       ft_vec_init(&steps, sizeof(unsigned long));
+       i = 0;
+       while (i < ind_map.size)
+       {
+               if ((t_node *)(ind_map.vec)[i].inode.id[ID_LEN - 1] == 'A')
+               {
+                       ft_vec_append(&steps, 
+               }
+               ++i;
+       }
+       result = 0;
+       free(inds);
+       return (result);
+}
+
+int    main(int argc, char **argv)
+{
+       t_vec   ind_map;
+       char    *inst;
+
+       if (argc != 2)
+               return (1);
+       ft_vec_init(&ind_map, sizeof(t_node));
+       parse(argv[1], &ind_map, &inst);
+       ft_printf("%i steps are needed to reach ZZZ.\n", steps_to_traverse(ind_map, inst, 'Z'));
+       return (0);
+       ft_vec_free(&ind_map);
+       free(inst);
+}
+
diff --git a/Libft b/Libft
index 0f2a329d9d76ddc77d22899ab8badfedadc78527..18fb6dadcbdec2f70216850bb6b90c6b568c50df 160000 (submodule)
--- a/Libft
+++ b/Libft
@@ -1 +1 @@
-Subproject commit 0f2a329d9d76ddc77d22899ab8badfedadc78527
+Subproject commit 18fb6dadcbdec2f70216850bb6b90c6b568c50df