Add solution for first part of day 1
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sun, 3 Dec 2023 19:48:43 +0000 (20:48 +0100)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Wed, 6 Dec 2023 10:16:58 +0000 (11:16 +0100)
1/1.c [new file with mode: 0644]

diff --git a/1/1.c b/1/1.c
new file mode 100644 (file)
index 0000000..73d4c0a
--- /dev/null
+++ b/1/1.c
@@ -0,0 +1,50 @@
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "libft.h"
+
+int    line_value(char *line)
+{
+       char    two_digit[2];
+       size_t  i;
+
+       i = 0;
+       while (!ft_isdigit(line[i]))
+               ++i;
+       two_digit[0] = line[i];
+       while(line[i])
+               ++i;
+       while (!ft_isdigit(line[i]))
+               --i;
+       two_digit[1] = line[i];
+       return (ft_atoi(two_digit));
+}
+
+int    main(int argc, char **argv)
+{
+       char    *line;
+       int             fd;
+       int             result;
+
+       if (argc != 2)
+       {
+               return (1);
+       }
+       fd = open(argv[1], O_RDONLY);
+       if (fd < 0)
+       {
+               return (2);
+       }
+       result = 0;
+       line = get_next_line(fd);
+       while (line)
+       {
+               result += line_value(line);
+               free(line);
+               line = get_next_line(fd);
+       }
+       ft_printf("The calibration sum is %i.\n", result);
+       close(fd);
+       return (0);
+}