diff --git a/Makefile b/Makefile index d85c7df..ebcda30 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ day1: day1/p1.c day1/p2.c day2: day2/p1.c day2/p2.c ${CC} ${CFLAGS} $@/p1.c -o bin/$@p1 -# ${CC} ${CFLAGS} $@/p2.c -o bin/$@p2 + ${CC} ${CFLAGS} $@/p2.c -o bin/$@p2 clean: diff --git a/day2/p2.c b/day2/p2.c new file mode 100644 index 0000000..56dfd85 --- /dev/null +++ b/day2/p2.c @@ -0,0 +1,95 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include + + +// parse_number takes a position of a last digit in a number +// and returns a resulting number. +int32_t parse_number(char *end); + + +#define STR_E(tok) #tok +#define STR(tok) STR_E(tok) + + +#define SETS_STR_MAX_LEN 200 + + +#define CUBE_RED 0 +#define CUBE_GREEN 1 +#define CUBE_BLUE 2 + + +int main(int argc, char **argv) { + FILE *input; + char *line = NULL; + size_t line_length = 0; + + if (argc == 1 || argv[1][0] == '-') + input = stdin; + else if ((input = fopen(argv[1], "r")) == NULL) { + printf("Cannot open file %s\n", argv[1]); + return -1; + } + + int32_t power_sum = 0; + + while (getline(&line, &line_length, input) != -1) { + char *colon_pos = strchr(line, ':'); + + char *sets = colon_pos + 2; + char *space_pos = NULL; + + int8_t cubes[3] = {0}; + + while (NULL != (space_pos = strchr(sets, ' '))) { + if (*(space_pos-1) == ',' || *(space_pos-1) == ';') { + sets = space_pos + 1; + continue; + } + + int8_t n = parse_number(space_pos-1); + + switch (*(space_pos+1)) { + case 'r': + if (cubes[CUBE_RED] < n) + cubes[CUBE_RED] = n; + break; + case 'g': + if (cubes[CUBE_GREEN] < n) + cubes[CUBE_GREEN] = n; + break; + case 'b': + if (cubes[CUBE_BLUE] < n) + cubes[CUBE_BLUE] = n; + break; + } + + sets = space_pos + 1; + } + + power_sum += cubes[CUBE_RED] * cubes[CUBE_GREEN] * cubes[CUBE_BLUE]; + + memset(line, 0, line_length); + } + + printf("%d\n", power_sum); + + return 0; +} + +int32_t parse_number(char *end) { + int32_t num = 0; + + for (size_t n = 1;; n *= 10) { + if (*end>>4 != 0x3) + break; + num += (*end&0xf) * n; + --end; + } + + return num; +} \ No newline at end of file diff --git a/test b/test index 70674e0..8b5eec8 100755 --- a/test +++ b/test @@ -1,8 +1,13 @@ #!/usr/bin/sh +set -e + test_case() { [[ $(bin/day$1p$2 < day$1/in.$3) = "$4" ]] && { echo -ne "\e[32mpassed\e[0m"; } || { echo -ne "\e[31mfailed\e[0m"; } } -echo "Day $1 with test data part 1 $(test_case $1 1 "test" "142"), and part 2 $(test_case $1 2 "test2" "281")."; -echo "Day $1 with my data part 1 $(test_case $1 1 "my" "55621"), and part 2 $(test_case $1 2 "my" "53592")."; \ No newline at end of file +echo "Day 1 with test data part 1 $(test_case 1 1 "test" "142"), and part 2 $(test_case 1 2 "test2" "281")."; +echo "Day 1 with my data part 1 $(test_case 1 1 "my" "55621"), and part 2 $(test_case 1 2 "my" "53592")."; + +echo "Day 2 with test data part 1 $(test_case 2 1 "test" "8"), and part 2 $(test_case 2 2 "test" "2286")."; +echo "Day 2 with my data part 1 $(test_case 2 1 "my" "2727"), and part 2 $(test_case 2 2 "my" "56580")."; \ No newline at end of file