1
0

solve day 3

This commit is contained in:
Rokas Puzonas 2022-12-03 23:56:43 +02:00
parent 093c558628
commit 750ada0c1d
5 changed files with 117 additions and 7 deletions

View File

@ -1,4 +1,4 @@
main: main.c
main: main.c day*.c
gcc -o main main.c -lcurl
run: main

9
day1.c
View File

@ -16,7 +16,8 @@ typedef struct {
size_t count;
} Data;
static void *day1_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 +46,8 @@ static void *day1_parse(char **lines, int line_count) {
return data;
}
static int day1_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 +60,8 @@ static int day1_part1(void *p) {
return max_calories;
}
static int day1_part2(void *p) {
static int day1_part2(void *p)
{
Data *data = (Data*)p;
int max_calories1 = 0;
int max_calories2 = 0;

9
day2.c
View File

@ -12,7 +12,8 @@ typedef struct {
size_t count;
} day2_Data;
static void *day2_parse(char **lines, int line_count) {
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));
@ -25,7 +26,8 @@ static void *day2_parse(char **lines, int line_count) {
return data;
}
static int day2_part1(void *p) {
static int day2_part1(void *p)
{
day2_Data *data = (day2_Data*)p;
int result = 0;
for (int i = 0; i < data->count; i++) {
@ -54,7 +56,8 @@ static int day2_part1(void *p) {
return result;
}
static int day2_part2(void *p) {
static int day2_part2(void *p)
{
day2_Data *data = (day2_Data*)p;
int result = 0;
for (int i = 0; i < data->count; i++) {

103
day3.c Normal file
View File

@ -0,0 +1,103 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aoc.h"
typedef struct {
char *contents;
size_t size;
} Backpack;
typedef struct {
Backpack *backpacks;
size_t count;
} day3_Data;
static void *day3_parse(char **lines, int line_count)
{
day3_Data *data = calloc(1, sizeof(day3_Data));
Backpack *backpacks = calloc(line_count, sizeof(Backpack));
data->backpacks = backpacks;
data->count = line_count;
for (size_t i = 0; i < line_count; i++) {
backpacks[i].size = strlen(lines[i]);
backpacks[i].contents = lines[i];
}
return data;
}
static bool contains(char needle, char* haystack, size_t len)
{
for (size_t i = 0; i < len; i++) {
if (haystack[i] == needle) {
return true;
}
}
return false;
}
static char find_common(char *str1, char* str2, size_t len)
{
for (size_t i = 0; i < len; i++) {
if (contains(str1[i], str2, len)) {
return str1[i];
}
}
return 0;
}
static int get_priority(char c)
{
if ('a' <= c && c <= 'z') {
return (c - 'a') + 1;
} else if ('A' <= c && c <= 'Z') {
return (c - 'A') + 27;
}
return 0;
}
static int day3_part1(void *p)
{
day3_Data *data = (day3_Data*)p;
int result = 0;
for (size_t i = 0; i < data->count; i++) {
Backpack *b = &data->backpacks[i];
char common = find_common(b->contents, b->contents+b->size/2, b->size/2);
if (common) {
result += get_priority(common);
} else {
fprintf(stderr, "Unknown common char at line: %zu\n", i+1);
}
}
return result;
}
static int day3_part2(void *p)
{
day3_Data *data = (day3_Data*)p;
int result = 0;
for (size_t i = 0; i < data->count; i+=3) {
Backpack *b1 = &data->backpacks[i+0];
Backpack *b2 = &data->backpacks[i+1];
Backpack *b3 = &data->backpacks[i+2];
bool found = false;
for (size_t j = 0; j < b1->size; j++) {
char c = b1->contents[j];
if (contains(c, b2->contents, b2->size) && contains(c, b3->contents, b3->size)) {
result += get_priority(c);
found = true;
break;
}
}
if (!found) {
fprintf(stderr, "Unknown common char at line: %zu-%zu\n", i+1, i+3);
}
}
return result;
}
ADD_SOLUTION(3, day3_parse, day3_part1, day3_part2);

1
main.c
View File

@ -10,6 +10,7 @@
#include "day1.c"
#include "day2.c"
#include "day3.c"
Solution *find_solution(int day)
{