solve day 2
This commit is contained in:
parent
1fbcc86c5b
commit
093c558628
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,2 @@
|
||||
build
|
||||
main
|
||||
input.txt
|
||||
|
5
Makefile
5
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)
|
||||
|
27
aoc.h
27
aoc.h
@ -4,25 +4,26 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
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; \
|
||||
|
8
day1.c
8
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);
|
||||
|
88
day2.c
Normal file
88
day2.c
Normal 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
3
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");
|
||||
|
Loading…
Reference in New Issue
Block a user