1
0

solve day 2

This commit is contained in:
Rokas Puzonas 2022-12-02 22:26:28 +02:00
parent 1fbcc86c5b
commit 093c558628
6 changed files with 111 additions and 22 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
build main
input.txt input.txt

View File

@ -1,6 +1,5 @@
main: main.c main: main.c
mkdir -p build gcc -o main main.c -lcurl
gcc -o build/main main.c -lcurl
run: main run: main
./build/main ./main $(day)

27
aoc.h
View File

@ -4,25 +4,26 @@
#include <stdbool.h> #include <stdbool.h>
typedef int (*solution_cb)(void*); typedef int (*solution_cb)(void*);
typedef void* (*parse_cb)(char** lines, int count);
typedef struct { typedef struct {
int day; int day;
void* (*parse)(char** lines, int count); parse_cb parse;
int (*part1)(void* data); solution_cb part1;
int (*part2)(void* data); solution_cb part2;
} Solution; } Solution;
// Macro magic for easy of use // Macro magic for easy of use
#define ADD_SOLUTION(_day, parse, part1, part2) \ #define ADD_SOLUTION(_day, _parse, _part1, _part2) \
static Solution ptr_##parse; \ static parse_cb ptr_##_parse; \
static Solution ptr_##part1; \ static solution_cb ptr_##_part1; \
static Solution ptr_##part2 \ static Solution ptr_##_part2 \
__attribute((used, section("g_solutions"))) = { \ __attribute((used, section("g_solutions"))) = { \
.parse = parse, \ .parse = _parse, \
.part1 = part1, \ .part1 = _part1, \
.part2 = part2, \ .part2 = _part2, \
.day = _day \ .day = _day \
} }
#define SOLUTIONS ({ \ #define SOLUTIONS ({ \
extern Solution __start_##g_solutions; \ extern Solution __start_##g_solutions; \

8
day1.c
View File

@ -16,7 +16,7 @@ typedef struct {
size_t count; size_t count;
} Data; } 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_indexes[300] = { 0 };
int elf_count = 1; int elf_count = 1;
for (int i = 1; i < line_count; i++) { for (int i = 1; i < line_count; i++) {
@ -45,7 +45,7 @@ static void *parse(char **lines, int line_count) {
return data; return data;
} }
static int part1(void *p) { static int day1_part1(void *p) {
Data *data = (Data*)p; Data *data = (Data*)p;
int max_calories = 0; int max_calories = 0;
for (int i = 0; i < data->count; i++) { for (int i = 0; i < data->count; i++) {
@ -58,7 +58,7 @@ static int part1(void *p) {
return max_calories; return max_calories;
} }
static int part2(void *p) { static int day1_part2(void *p) {
Data *data = (Data*)p; Data *data = (Data*)p;
int max_calories1 = 0; int max_calories1 = 0;
int max_calories2 = 0; int max_calories2 = 0;
@ -82,4 +82,4 @@ static int part2(void *p) {
return max_calories1 + max_calories2 + max_calories3; return max_calories1 + max_calories2 + max_calories3;
} }
ADD_SOLUTION(1, parse, part1, part2); ADD_SOLUTION(1, day1_parse, day1_part1, day1_part2);

88
day2.c Normal file
View File

@ -0,0 +1,88 @@
#include <stdio.h>
#include <stdlib.h>
#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);

3
main.c
View File

@ -9,6 +9,7 @@
#include "aoc.h" #include "aoc.h"
#include "day1.c" #include "day1.c"
#include "day2.c"
Solution *find_solution(int day) Solution *find_solution(int day)
{ {
@ -132,7 +133,7 @@ int main(int argc, char** argv) {
input_file = "input.txt"; input_file = "input.txt";
} else { } else {
char* session = getenv("AOC_SESSION"); 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"; input_file = "input.txt";
} else { } else {
fprintf(stderr, "Missing input file"); fprintf(stderr, "Missing input file");