From 093c558628e53ae1d22610f45e86345f3099f2cc Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Fri, 2 Dec 2022 22:26:28 +0200 Subject: [PATCH] solve day 2 --- .gitignore | 2 +- Makefile | 5 ++-- aoc.h | 27 +++++++++-------- day1.c | 8 ++--- day2.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 3 +- 6 files changed, 111 insertions(+), 22 deletions(-) create mode 100644 day2.c diff --git a/.gitignore b/.gitignore index 58d53e9..303df3a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -build +main input.txt diff --git a/Makefile b/Makefile index f09daaf..6735525 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,5 @@ main: main.c - mkdir -p build - gcc -o build/main main.c -lcurl + gcc -o main main.c -lcurl run: main - ./build/main + ./main $(day) diff --git a/aoc.h b/aoc.h index 6eac67c..66b28fa 100644 --- a/aoc.h +++ b/aoc.h @@ -4,25 +4,26 @@ #include typedef int (*solution_cb)(void*); +typedef void* (*parse_cb)(char** lines, int count); typedef struct { int day; - void* (*parse)(char** lines, int count); - int (*part1)(void* data); - int (*part2)(void* data); + parse_cb parse; + solution_cb part1; + solution_cb part2; } Solution; // Macro magic for easy of use -#define ADD_SOLUTION(_day, parse, part1, part2) \ - static Solution ptr_##parse; \ - static Solution ptr_##part1; \ - static Solution ptr_##part2 \ - __attribute((used, section("g_solutions"))) = { \ - .parse = parse, \ - .part1 = part1, \ - .part2 = part2, \ - .day = _day \ - } +#define ADD_SOLUTION(_day, _parse, _part1, _part2) \ + static parse_cb ptr_##_parse; \ + static solution_cb ptr_##_part1; \ + static Solution ptr_##_part2 \ + __attribute((used, section("g_solutions"))) = { \ + .parse = _parse, \ + .part1 = _part1, \ + .part2 = _part2, \ + .day = _day \ + } #define SOLUTIONS ({ \ extern Solution __start_##g_solutions; \ diff --git a/day1.c b/day1.c index 8fffa68..bbe46c1 100644 --- a/day1.c +++ b/day1.c @@ -16,7 +16,7 @@ typedef struct { size_t count; } Data; -static void *parse(char **lines, int line_count) { +static void *day1_parse(char **lines, int line_count) { int elf_indexes[300] = { 0 }; int elf_count = 1; for (int i = 1; i < line_count; i++) { @@ -45,7 +45,7 @@ static void *parse(char **lines, int line_count) { return data; } -static int part1(void *p) { +static int day1_part1(void *p) { Data *data = (Data*)p; int max_calories = 0; for (int i = 0; i < data->count; i++) { @@ -58,7 +58,7 @@ static int part1(void *p) { return max_calories; } -static int part2(void *p) { +static int day1_part2(void *p) { Data *data = (Data*)p; int max_calories1 = 0; int max_calories2 = 0; @@ -82,4 +82,4 @@ static int part2(void *p) { return max_calories1 + max_calories2 + max_calories3; } -ADD_SOLUTION(1, parse, part1, part2); +ADD_SOLUTION(1, day1_parse, day1_part1, day1_part2); diff --git a/day2.c b/day2.c new file mode 100644 index 0000000..8a0ec4b --- /dev/null +++ b/day2.c @@ -0,0 +1,88 @@ +#include +#include + +#include "aoc.h" + +typedef struct { + char you, opponent; +} Round; + +typedef struct { + Round *rounds; + size_t count; +} day2_Data; + +static void *day2_parse(char **lines, int line_count) { + day2_Data *data = calloc(1, sizeof(day2_Data)); + data->count = line_count; + data->rounds = calloc(line_count, sizeof(Round)); + + for (int i = 0; i < line_count; i++) { + data->rounds[i].opponent = lines[i][0]; + data->rounds[i].you = lines[i][2]; + } + + return data; +} + +static int day2_part1(void *p) { + day2_Data *data = (day2_Data*)p; + int result = 0; + for (int i = 0; i < data->count; i++) { + Round *round = &data->rounds[i]; + char you = round->you; + char opponent = round->opponent; + + if (you == 'X') { + result += 1; + } else if (you == 'Y') { + result += 2; + } else if (you == 'Z') { + result += 3; + } + + if ((you == 'X' && opponent == 'A') || + (you == 'Y' && opponent == 'B') || + (you == 'Z' && opponent == 'C')) { + result += 3; + } else if ((you == 'X' && opponent == 'C') || + (you == 'Y' && opponent == 'A') || + (you == 'Z' && opponent == 'B')) { + result += 6; + } + } + return result; +} + +static int day2_part2(void *p) { + day2_Data *data = (day2_Data*)p; + int result = 0; + for (int i = 0; i < data->count; i++) { + Round *round = &data->rounds[i]; + char you = round->you; + char opponent = round->opponent; + + if (you == 'X') { // lose + if (opponent == 'A') result += 3; // opponent = rock , me = scissors + if (opponent == 'B') result += 1; // opponent = paper , me = rock + if (opponent == 'C') result += 2; // opponent = scissors, me = paper + } else if (you == 'Y') { // draw + if (opponent == 'A') result += 1; + if (opponent == 'B') result += 2; + if (opponent == 'C') result += 3; + } else if (you == 'Z') { // win + if (opponent == 'A') result += 2; // opponent = rock , me = paper + if (opponent == 'B') result += 3; // opponent = paper , me = scissors + if (opponent == 'C') result += 1; // opponent = scissors, me = rock + } + + if (you == 'Y') { + result += 3; + } else if (you == 'Z') { + result += 6; + } + } + return result; +} + +ADD_SOLUTION(2, day2_parse, day2_part1, day2_part2); diff --git a/main.c b/main.c index a6be887..8827f4f 100644 --- a/main.c +++ b/main.c @@ -9,6 +9,7 @@ #include "aoc.h" #include "day1.c" +#include "day2.c" Solution *find_solution(int day) { @@ -132,7 +133,7 @@ int main(int argc, char** argv) { input_file = "input.txt"; } else { char* session = getenv("AOC_SESSION"); - if (session && !download_input(1, "input.txt", session)) { + if (session && !download_input(day, "input.txt", session)) { input_file = "input.txt"; } else { fprintf(stderr, "Missing input file");