Add solution to first part of day 2
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sun, 3 Dec 2023 21:53:46 +0000 (22:53 +0100)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Wed, 6 Dec 2023 10:17:28 +0000 (11:17 +0100)
2/2.c [new file with mode: 0644]

diff --git a/2/2.c b/2/2.c
new file mode 100644 (file)
index 0000000..720db53
--- /dev/null
+++ b/2/2.c
@@ -0,0 +1,146 @@
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "libft.h"
+
+struct s_draw
+{
+       int     r;
+       int     g;
+       int     b;
+};
+typedef struct s_draw  t_draw;
+
+struct s_game
+{
+       int             id;
+       size_t  size;
+       t_draw  *draws;
+};
+typedef struct s_game  t_game;
+
+size_t count_semicolons(const char *str)
+{
+       size_t  res;
+
+       res = 0;
+       while (*str)
+       {
+               if (*str == ';')
+                       ++res;
+               ++str;
+       }
+       return (res);
+}
+
+void   fill_draw(t_draw *draw, const char **line)
+{
+       int     cubes;
+
+       cubes = ft_atoi(*line);
+       while (!ft_isalpha(**line))
+               ++(*line);
+       if (**line == 'r')
+       {
+               draw->r = cubes;
+               *line += 3;
+       }
+       else if (**line == 'g')
+       {
+               draw->g = cubes;
+               *line += 5;
+       }
+       else if (**line == 'b')
+       {
+               draw->b = cubes;
+               *line += 4;
+       }
+       return ;
+}
+
+void   zero_draws(t_game *game)
+{
+       size_t  i;
+
+       i = 0;
+       while (i < game->size)
+       {
+               game->draws[i] = (t_draw){.r = 0, .g = 0, .b = 0};
+               ++i;
+       }
+       return ;
+}
+
+t_game parse(const char *line)
+{
+       t_game  game;
+       size_t  i;
+
+       while (!ft_isdigit(*line))
+               ++line;
+       game.id = ft_atoi(line);
+       while (*line != ':')
+               ++line;
+       game.size = count_semicolons(line) + 1;
+       game.draws = malloc(sizeof(t_draw) * game.size);
+       if (!game.draws)
+               return (game);
+       zero_draws(&game);
+       i = 0;
+       while (*line && *line != '\n')
+       {
+               line += 2;
+               fill_draw(game.draws + i, &line);
+               if (*line == ';')
+                       ++i;
+       }
+       return (game);
+}
+
+int possible(t_game game)
+{
+       const int       max_r = 12;
+       const int       max_g = 13;
+       const int       max_b = 14;
+       size_t          i;
+       t_draw          draw;
+
+       i = 0;
+       while (i < game.size)
+       {
+               draw = game.draws[i];
+               if (max_r < draw.r || max_g < draw.g || max_b < draw.b)
+                       return (0);
+               ++i;
+       }
+       return (1);
+}
+
+int    main(int argc, char **argv)
+{
+       char    *line;
+       int             result;
+       int             fd;
+       t_game  game;
+
+       if (argc != 2)
+               return (1);
+       fd = open(argv[1], O_RDONLY);
+       if (fd <= 0)
+               return (2);
+       line = get_next_line(fd);
+       result = 0;
+       while (line)
+       {
+               game = parse(line);
+               if (possible(game))
+                       result += game.id;
+               free(line);
+               free(game.draws);
+               line = get_next_line(fd);
+       }
+       ft_printf("Resulting sum of game is %i.\n", result);
+       close(fd);
+       return (0);
+}