Complete Day 2, Update make files, add run all script

This commit is contained in:
Preston Baxter 2023-12-10 21:22:09 -06:00
parent 775239ee1a
commit 0007bda206
11 changed files with 350 additions and 4 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
bin/

View File

@ -1,9 +1,9 @@
BASENAME= $(shell basename `pwd`)
build:
rm $(BASENAME) || echo ""
gcc $(BASENAME).c ../libs/* -o $(BASENAME)
@rm ../bin/$(BASENAME) || echo ""
@gcc $(BASENAME).c ../libs/* -o ../bin/$(BASENAME)
run: build
./$(BASENAME) input.txt a
./$(BASENAME) input.txt b
@../bin/$(BASENAME) input.txt a
@../bin/$(BASENAME) input.txt b

9
day2/Makefile Normal file
View File

@ -0,0 +1,9 @@
BASENAME= $(shell basename `pwd`)
build:
@rm ../bin/$(BASENAME) || echo ""
@gcc $(BASENAME).c ../libs/* -o ../bin/$(BASENAME)
run: build
@../bin/$(BASENAME) input.txt a
@../bin/$(BASENAME) input.txt b

197
day2/day2.c Normal file
View File

@ -0,0 +1,197 @@
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../libs/buffered_reader.h"
#include "../libs/data_structures.h"
#define MAX_RED 12
#define MAX_GREEN 13
#define MAX_BLUE 14
int decodea(char *str);
int decodeb(char *str);
int get_color(const char *str);
bool validate_round(char *str, int dice[]);
// SEPARATORS
const char *GAME_SEP = ":";
const char *COLOR_SEP = ",";
const char *ROUND_SEP = ";";
// COLOR
const char *RED = "red";
const char *GREEN = "green";
const char *BLUE = "blue";
list *stack;
enum Color { Red = 0, Green = 1, Blue = 2 };
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("usage: day2 INPUT_FILE a|b\n");
exit(1);
}
char part = *argv[2];
filelines *lines = flines(argv[1]);
if (lines == NULL) {
printf("Failed to read file");
exit(1);
}
// init stack
initList(&stack);
int sum = 0;
for (int i = 0; i < lines->size; i++) {
int d = 0;
//based on cli input change function
switch (part) {
case 'a':
d = decodea(get(lines, i));
break;
case 'b':
d = decodeb(get(lines, i));
break;
}
/*
if (d == 0) {
printf("^^^^ Game %d ^^^^^\n", i + 1);
} else {
printf("---- Game %d -----\n", i + 1);
}
*/
sum += d;
}
printf("Sum: %d\n", sum);
}
int decodea(char *str) {
// find semicolon
int game_split = strcspn(str, GAME_SEP);
char *game_str = str + game_split + 1;
if (!*game_str || str == game_str) {
printf("Could not find game separator.\n");
exit(1);
}
// parse colors
char *token;
token = strtok(game_str, ";");
filelines *rounds;
initFileLines(&rounds);
// Get rounds
while (token) {
insert(rounds, token);
token = strtok(NULL, ";");
}
// validate rounds
for (int i = 0; i < rounds->size; i++) {
int dice[] = {0, 0, 0};
if (!validate_round(get(rounds, i), dice)) {
return 0;
}
}
freeLines(rounds);
clears(stack);
int space_index = strcspn(str, " ");
for (int i = space_index + 1; i < game_split; i++) {
pushs(stack, *(str + i));
}
return atoi(contents(stack));
}
int decodeb(char *str) {
// find semicolon
int game_split = strcspn(str, GAME_SEP);
char *game_str = str + game_split + 1;
if (!*game_str || str == game_str) {
printf("Could not find game separator.\n");
exit(1);
}
// parse colors
char *token;
token = strtok(game_str, ";");
filelines *rounds;
initFileLines(&rounds);
// Get rounds
while (token) {
insert(rounds, token);
token = strtok(NULL, ";");
}
int min[] = {0,0,0};
// validate rounds
for (int i = 0; i < rounds->size; i++) {
int dice[] = {0, 0, 0};
validate_round(get(rounds, i), dice);
min[Red] = dice[Red] >= min[Red] ? dice[Red] : min[Red];
min[Green] = dice[Green] >= min[Green] ? dice[Green] : min[Green];
min[Blue] = dice[Blue] >= min[Blue] ? dice[Blue] : min[Blue];
}
return min[Red] * min[Green] * min[Blue];
}
int get_color(const char *str) {
if (strstr(str, RED)) {
return Red;
}
if (strstr(str, GREEN)) {
return Green;
}
if (strstr(str, BLUE)) {
return Blue;
}
return -1;
}
bool validate_round(char *str, int dice[]) {
list *color_list;
initList(&color_list);
char *token;
token = strtok(str, ",");
while (token) {
// trim leading whitespace
while (isspace(*token))
token++;
// printf("%s,", token);
// actually do the stuff
clears(color_list);
char *c = token;
while (c) {
if (isspace(*c)) {
break;
}
pushs(color_list, *c);
c++;
}
enum Color color = get_color(token);
dice[color] += atoi(contents(color_list));
token = strtok(NULL, ",");
}
// printf("\n");
freelist(color_list);
return dice[Red] <= MAX_RED && dice[Green] <= MAX_GREEN &&
dice[Blue] <= MAX_BLUE;
}

100
day2/input.txt Normal file
View File

@ -0,0 +1,100 @@
Game 1: 12 blue, 15 red, 2 green; 17 red, 8 green, 5 blue; 8 red, 17 blue; 9 green, 1 blue, 4 red
Game 2: 6 red, 6 blue, 2 green; 1 blue, 1 red; 6 green, 1 red, 10 blue
Game 3: 1 green, 2 blue; 7 blue, 4 green; 2 green, 1 blue; 10 blue, 4 green; 4 blue; 1 green, 7 blue, 1 red
Game 4: 16 red, 12 blue, 10 green; 15 red, 5 green, 6 blue; 10 green, 15 red, 12 blue
Game 5: 2 green, 2 red, 9 blue; 1 red, 5 green; 4 green, 12 blue, 1 red; 5 blue, 8 green
Game 6: 17 blue, 3 green, 4 red; 6 green, 16 blue, 3 red; 2 red, 15 blue
Game 7: 4 green, 10 red; 1 green, 4 red, 4 blue; 4 blue, 11 red
Game 8: 8 green, 4 blue; 17 green, 4 red; 10 blue, 5 green, 9 red; 9 green, 8 red, 3 blue; 9 green, 5 red, 2 blue
Game 9: 4 red, 2 green; 7 blue, 3 red, 3 green; 3 green, 7 blue, 3 red
Game 10: 3 green, 2 red, 2 blue; 3 green, 11 red, 1 blue; 16 green, 11 red
Game 11: 2 blue, 18 green; 12 blue, 1 green; 2 green, 6 blue; 1 red, 4 blue, 20 green; 14 blue, 1 red, 4 green
Game 12: 2 green, 1 blue, 7 red; 11 red, 3 green, 6 blue; 1 red, 2 blue, 3 green; 4 red, 2 green, 5 blue
Game 13: 4 red, 17 blue, 5 green; 6 blue, 2 green; 12 blue, 4 green, 2 red; 5 green, 9 blue; 5 green, 3 blue, 3 red; 4 green, 1 red, 7 blue
Game 14: 4 blue, 18 green; 3 blue, 3 red, 13 green; 5 blue, 10 green; 10 green, 2 blue; 1 blue, 14 green; 3 blue, 18 green, 2 red
Game 15: 1 green, 2 blue, 1 red; 1 green, 2 red, 1 blue; 1 green, 2 red; 1 green, 4 blue, 4 red; 6 blue, 2 red, 1 green; 3 blue, 2 red
Game 16: 3 green, 2 red; 4 green, 1 red, 8 blue; 5 blue, 9 red, 3 green; 7 blue, 19 green, 18 red
Game 17: 10 blue, 9 red, 7 green; 16 red, 11 green, 11 blue; 8 blue, 3 green; 12 red, 1 blue, 10 green
Game 18: 11 green, 11 blue, 5 red; 7 red, 11 green, 13 blue; 5 green, 9 red, 6 blue; 9 red, 16 green, 17 blue
Game 19: 8 red, 3 green, 16 blue; 13 green, 8 blue; 7 red, 8 green, 1 blue; 13 red, 3 blue, 7 green; 6 green, 14 blue, 13 red; 15 blue, 9 green, 13 red
Game 20: 1 red, 7 green, 5 blue; 14 green, 4 blue; 10 green, 11 blue, 2 red; 2 red, 3 blue, 1 green; 1 red, 5 blue, 8 green
Game 21: 10 green, 12 blue, 6 red; 17 blue, 6 red, 6 green; 12 blue, 9 green, 4 red; 5 blue, 3 red, 4 green; 6 green, 7 blue, 5 red
Game 22: 1 blue, 3 red, 16 green; 4 red, 1 blue, 3 green; 12 green, 1 blue, 2 red; 12 red
Game 23: 2 red, 6 blue, 1 green; 11 red, 13 blue, 4 green; 8 red, 3 blue, 6 green; 2 green, 8 blue, 2 red; 7 red, 11 blue, 4 green
Game 24: 4 red, 12 green, 2 blue; 8 blue, 15 red; 1 blue, 10 green, 8 red; 1 green, 2 blue, 6 red; 10 green, 8 blue, 5 red
Game 25: 2 blue, 11 green; 17 green, 1 red, 2 blue; 2 blue, 3 red, 1 green
Game 26: 16 blue, 11 green; 4 green; 9 green, 4 blue; 10 green, 5 blue; 1 red, 5 blue, 9 green; 5 green, 5 blue
Game 27: 10 green, 2 red; 5 blue, 1 red; 6 red, 5 green
Game 28: 3 red, 5 green, 10 blue; 1 red, 5 green, 2 blue; 6 blue, 2 green, 2 red; 6 red, 9 blue, 1 green; 3 red; 3 red, 2 green, 2 blue
Game 29: 8 red, 18 blue, 5 green; 1 blue, 8 red, 2 green; 2 red, 4 green, 18 blue; 6 red, 4 green, 7 blue
Game 30: 1 red, 18 green; 11 green; 4 blue, 5 red, 14 green; 3 green, 8 blue, 2 red
Game 31: 1 red, 5 blue, 17 green; 7 blue; 10 green, 8 blue, 1 red; 11 green, 4 blue
Game 32: 5 blue, 15 red, 12 green; 6 red, 8 green, 8 blue; 2 red, 14 green, 3 blue; 4 blue, 15 green; 7 blue, 12 red, 7 green; 2 blue, 9 red, 7 green
Game 33: 13 red, 2 green; 1 green, 7 red, 15 blue; 1 green, 14 blue, 13 red; 8 red, 2 green; 12 red, 14 blue, 10 green; 8 green, 16 blue, 10 red
Game 34: 11 green, 9 blue, 2 red; 4 red, 1 green, 8 blue; 4 blue, 7 green, 4 red; 7 blue, 1 red, 8 green; 9 blue, 1 red, 4 green; 2 red, 10 green, 4 blue
Game 35: 3 red, 9 blue; 11 blue, 3 red, 12 green; 7 green, 10 blue, 2 red
Game 36: 9 blue, 3 green, 3 red; 5 blue, 1 red, 3 green; 2 green, 6 red; 9 blue, 7 red
Game 37: 1 red, 7 blue; 4 red, 1 green; 1 green, 9 red, 9 blue
Game 38: 1 green, 12 red; 4 green, 12 red, 4 blue; 5 green, 10 red; 6 red, 4 green, 3 blue; 4 green, 10 red; 2 green, 5 blue, 4 red
Game 39: 2 blue; 4 red; 4 red, 5 green, 1 blue
Game 40: 7 red, 2 green, 17 blue; 12 green, 1 red, 7 blue; 9 green, 2 red, 8 blue
Game 41: 18 green, 5 red, 4 blue; 20 green, 17 blue, 5 red; 3 red, 7 blue, 7 green; 4 red, 19 green, 18 blue; 20 blue, 20 green
Game 42: 1 green, 6 blue, 1 red; 5 blue, 1 red, 3 green; 3 green, 7 blue, 1 red
Game 43: 4 blue, 6 green, 13 red; 16 red, 7 blue, 8 green; 4 green, 16 red
Game 44: 5 green, 4 red, 13 blue; 4 red, 12 blue, 3 green; 6 green
Game 45: 1 red, 17 blue, 15 green; 6 red, 3 green, 9 blue; 5 green, 1 blue, 7 red; 6 blue, 4 red, 4 green
Game 46: 1 blue, 11 red, 1 green; 2 red, 2 green, 1 blue; 4 red, 1 green, 1 blue; 2 blue, 7 red, 3 green; 11 red, 3 green
Game 47: 2 red, 1 blue; 1 green, 1 red, 1 blue; 5 green, 1 red
Game 48: 9 blue, 1 red; 1 green, 2 red, 11 blue; 2 red, 6 blue
Game 49: 5 blue, 7 red, 17 green; 5 red, 4 green, 7 blue; 1 red, 3 blue; 4 red, 12 green, 6 blue; 6 green, 4 blue, 3 red
Game 50: 11 blue, 12 green, 1 red; 8 green, 7 red, 9 blue; 13 red, 12 blue, 10 green; 5 green, 10 blue, 3 red
Game 51: 8 blue, 1 red; 5 red, 2 green; 9 blue, 6 red, 4 green; 4 green, 1 red, 13 blue; 15 blue, 3 red, 8 green; 6 red, 1 green, 4 blue
Game 52: 2 blue, 1 red; 1 red, 4 blue, 5 green; 3 red, 14 blue, 2 green
Game 53: 8 blue, 10 green, 11 red; 5 red, 4 blue, 19 green; 8 red, 3 blue; 3 red, 3 blue, 2 green; 4 red, 4 green
Game 54: 3 green, 17 red; 7 green, 13 red, 5 blue; 11 blue, 10 red, 10 green; 3 green, 19 red, 4 blue; 11 green, 6 blue, 19 red; 5 red, 4 blue, 9 green
Game 55: 3 blue, 4 red; 1 red, 1 blue, 2 green; 4 blue, 2 green, 4 red
Game 56: 10 red, 3 green, 5 blue; 2 blue, 2 red; 7 red, 3 blue, 2 green
Game 57: 12 red, 1 blue, 8 green; 1 blue, 3 green, 10 red; 5 green, 8 red
Game 58: 6 red, 4 green, 2 blue; 7 red, 6 blue, 14 green; 5 blue, 6 red, 2 green; 2 red, 4 blue; 7 blue, 12 green; 7 green, 3 blue, 8 red
Game 59: 6 red, 5 blue; 5 blue, 1 green; 1 blue, 6 red; 4 blue, 2 red, 1 green; 3 red, 2 blue; 3 blue, 5 red, 1 green
Game 60: 1 red, 12 green, 2 blue; 4 red, 5 blue; 12 green, 1 red; 5 blue, 13 red, 17 green; 15 green, 1 blue
Game 61: 10 blue, 18 red; 4 blue, 1 green, 14 red; 4 blue, 2 green; 2 green, 6 red, 10 blue
Game 62: 2 green, 13 blue, 8 red; 7 green, 5 red, 8 blue; 5 red, 8 blue; 3 red, 5 green, 4 blue; 15 blue, 5 red, 6 green
Game 63: 6 red, 7 green, 2 blue; 2 red, 6 green; 2 blue, 4 red, 5 green; 1 blue, 2 red, 5 green; 4 red, 8 green; 9 green, 2 red
Game 64: 4 red, 4 blue; 7 blue, 5 red; 8 green, 5 red, 6 blue; 2 red, 3 blue, 1 green; 7 blue, 9 green, 7 red; 11 green, 2 red, 3 blue
Game 65: 1 red, 11 green, 9 blue; 2 red, 5 green, 17 blue; 2 red, 3 blue, 6 green; 2 red, 6 green, 14 blue
Game 66: 7 green, 5 red, 2 blue; 5 red, 7 green, 2 blue; 6 green, 3 blue, 15 red; 8 green, 20 red, 4 blue; 8 red, 8 green, 3 blue; 3 blue, 11 red, 5 green
Game 67: 2 blue, 2 green; 6 blue, 1 green, 3 red; 3 red, 7 green, 4 blue; 1 red, 1 green, 8 blue
Game 68: 1 green, 3 red; 2 green, 1 blue, 5 red; 2 red, 2 green, 1 blue; 2 green, 3 red, 1 blue; 6 red, 1 blue
Game 69: 4 red, 2 green, 3 blue; 14 red; 3 blue; 11 red, 1 green; 13 red, 3 green, 2 blue
Game 70: 1 green, 1 blue, 6 red; 1 green, 4 red, 1 blue; 6 red, 1 blue
Game 71: 1 green, 8 blue, 10 red; 6 green, 3 red, 2 blue; 14 red, 3 green; 9 blue, 2 green, 2 red; 7 blue, 5 red, 1 green; 6 green, 5 blue, 10 red
Game 72: 2 red, 10 blue; 1 red, 7 blue, 4 green; 1 red, 3 green, 6 blue
Game 73: 6 red, 6 blue, 5 green; 1 blue, 11 green, 7 red; 10 red, 7 blue, 2 green
Game 74: 4 green, 2 red, 3 blue; 1 red, 6 green; 2 red, 4 blue; 1 blue; 2 blue, 1 green
Game 75: 10 red, 5 blue, 1 green; 12 blue; 2 green, 11 blue, 9 red; 1 blue, 14 red; 2 red, 2 green, 13 blue
Game 76: 9 green, 1 blue; 8 green, 2 blue, 7 red; 4 blue, 7 green, 4 red
Game 77: 14 red, 3 blue, 10 green; 3 blue, 7 green, 2 red; 5 red, 7 green, 3 blue; 14 red, 8 green, 3 blue; 9 green, 5 red; 2 blue, 7 red, 15 green
Game 78: 5 blue, 9 green, 8 red; 11 green, 9 blue, 4 red; 8 red, 2 blue, 10 green; 3 blue, 7 green
Game 79: 4 red, 6 blue, 10 green; 2 blue, 17 green, 15 red; 15 red, 6 blue, 14 green
Game 80: 2 red, 8 green; 6 blue, 6 green; 1 green, 3 red, 8 blue; 5 green, 4 blue, 3 red; 3 blue, 1 red; 7 green, 8 blue, 3 red
Game 81: 5 blue, 1 red; 10 blue, 7 red, 3 green; 4 green, 1 red, 10 blue; 8 red, 4 blue, 3 green; 11 blue, 9 green, 1 red; 6 red, 10 green
Game 82: 1 green, 2 blue, 1 red; 2 blue, 1 green, 2 red; 2 red, 8 green; 3 red, 3 blue, 5 green
Game 83: 3 blue, 4 green, 5 red; 6 blue, 5 red, 5 green; 4 red, 2 blue, 5 green; 2 green, 6 blue, 5 red; 1 red, 2 green
Game 84: 14 blue, 1 green; 9 green, 3 red, 1 blue; 5 green, 10 blue, 3 red; 9 green; 6 green, 18 blue; 2 red, 8 green
Game 85: 1 blue, 7 red; 12 green, 7 red, 5 blue; 9 blue, 6 green, 7 red; 10 red, 7 green, 1 blue; 3 green, 6 blue, 7 red; 10 red, 16 blue
Game 86: 9 red, 10 blue, 2 green; 2 red, 12 blue, 9 green; 11 green, 2 blue, 3 red
Game 87: 1 blue, 7 red, 9 green; 1 red, 6 blue; 3 blue, 12 green
Game 88: 1 blue, 4 green; 4 red, 13 blue, 1 green; 7 green, 4 blue, 3 red; 2 blue, 4 green, 5 red; 4 red, 7 green, 10 blue; 1 red, 7 green, 13 blue
Game 89: 5 blue, 8 red, 1 green; 7 blue, 2 green, 7 red; 2 green, 8 blue, 11 red
Game 90: 5 blue, 1 green, 11 red; 6 blue, 8 red; 2 red, 3 blue, 4 green; 2 green, 4 blue, 7 red; 3 blue, 8 red; 6 red, 3 blue, 1 green
Game 91: 2 green, 13 red, 9 blue; 12 blue, 6 green; 14 green, 10 blue, 3 red; 13 blue, 7 green, 5 red; 1 green, 9 blue, 14 red; 10 green, 8 blue, 17 red
Game 92: 10 green, 3 red, 17 blue; 13 red, 7 green, 15 blue; 9 blue, 8 red, 4 green; 1 blue, 8 red; 9 red, 1 green, 19 blue
Game 93: 1 blue, 7 green, 4 red; 2 blue, 8 green; 10 red, 10 green, 1 blue; 10 green, 2 blue; 3 red, 3 blue
Game 94: 5 red, 8 green, 14 blue; 4 red, 7 green, 20 blue; 11 blue, 4 red, 13 green; 18 blue, 1 red, 3 green
Game 95: 5 red, 8 green, 11 blue; 12 green, 5 blue; 4 blue, 14 green; 7 green, 9 blue, 5 red; 3 red, 4 green, 7 blue; 3 red, 12 blue, 3 green
Game 96: 10 red, 12 blue, 9 green; 4 green, 8 blue, 4 red; 8 blue, 3 red, 4 green; 6 green, 10 red; 2 blue, 3 green, 15 red; 12 red, 2 green, 2 blue
Game 97: 13 green, 1 blue; 9 green, 1 red, 1 blue; 6 blue, 10 green; 1 red, 6 blue, 11 green
Game 98: 2 blue, 14 green, 2 red; 7 green, 1 blue; 1 blue, 1 red, 3 green; 2 red, 1 blue, 15 green; 1 blue, 2 red, 10 green
Game 99: 3 green, 8 red, 7 blue; 6 red, 13 blue; 12 red, 4 green, 4 blue; 12 red, 8 green, 3 blue; 11 blue, 11 red, 4 green
Game 100: 2 red, 13 blue, 1 green; 1 green, 12 blue; 1 red, 5 blue, 1 green; 3 blue, 3 red

View File

@ -0,0 +1,5 @@
Game 10: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 20: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 30: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 40: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 50: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green

View File

@ -87,4 +87,8 @@ void clears(list *l) {
l->tail=0;
}
void freelist(list *l) {
free(l->arr);
free(l);
}

View File

@ -22,5 +22,7 @@ char* contents(list *l);
//returns copy of contents as string. But backwards :)
char* contentb(list *l);
void freelist(list *l);
#endif

View File

@ -1,5 +1,6 @@
#include "utils.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -18,3 +19,22 @@ char *strreplace(char *s, const char *s1, const char *s2) {
return s;
}
char *trimwhitespace(char *str){
char *end;
// Trim leading space
while(isspace((unsigned char)*str)) str++;
if(*str == 0) // All spaces?
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;
// Write new null terminator character
end[1] = '\0';
return str;
}

View File

@ -1,5 +1,6 @@
#ifndef utils
char *strreplace(char *s, const char *s1, const char *s2);
char *trimwhitespace(char *s);
#endif

7
run_all.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
dirs="$(ls . | tr ' ' '\n' | grep day)"
echo "$dirs" | while read -r dir; do
echo "Running $dir"
cd "$dir"; make run; cd ..
done