Code cleanup

This commit is contained in:
Evan Burkey 2021-09-11 16:47:06 -07:00
parent 0a7aaec503
commit fc4cacf492
10 changed files with 125 additions and 81 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ compile_commands.json
.cache .cache
build build
.clangd .clangd
cmake-build-*

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

2
.idea/advent.iml generated Normal file
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

4
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/advent.iml" filepath="$PROJECT_DIR$/.idea/advent.iml" />
</modules>
</component>
</project>

7
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/lib/libflint" vcs="Git" />
</component>
</project>

View File

@ -1,29 +1,28 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "input.h" #include "input.h"
void advent2015day01(void) { void advent2015day01(void) {
char *input = get_input("input/2015/01"); char *input = get_input("input/2015/01");
char *c = input; char *c = input;
int f = 0, b = 0, s = 1; int f = 0, b = 0, s = 1;
while (*c != '\0') { while (*c != '\0') {
if (*c == '(') { if (*c == '(') {
++f; ++f;
} else { } else {
--f; --f;
}
if (f == -1 && b == 0) {
b = s;
}
++c;
++s;
} }
if (f == -1 && b == 0) { printf("%d\n%d\n", f, b);
b = s; free(input);
}
++c;
++s;
}
printf("%d\n%d\n", f, b);
free(input);
} }

View File

@ -6,22 +6,22 @@
#include "advent_math.h" #include "advent_math.h"
void advent2015day02(void) { void advent2015day02(void) {
size_t sz = 0; size_t sz = 0;
char **lines = get_lines("input/2015/02", &sz); char **lines = get_lines("input/2015/02", &sz);
int paper = 0, ribbon = 0; int paper = 0, ribbon = 0;
for (size_t i = 0; i < sz; i++) { for (size_t i = 0; i < sz; i++) {
char *t = strtok(lines[i], "x"); char *t = strtok(lines[i], "x");
int w = atoi(t); int w = atoi(t);
t = strtok(NULL, "x"); t = strtok(NULL, "x");
int l = atoi(t); int l = atoi(t);
t = strtok(NULL, "x"); t = strtok(NULL, "x");
int h = atoi(t); int h = atoi(t);
paper += 2*l*w + 2*w*h + 2*h*l + int_min(w*l, int_min(l*h, h*w)); paper += 2 * l * w + 2 * w * h + 2 * h * l + int_min(w * l, int_min(l * h, h * w));
ribbon += l*w*h + int_min(w+w+h+h, int_min(h+h+l+l, l+l+w+w)); ribbon += l * w * h + int_min(w + w + h + h, int_min(h + h + l + l, l + l + w + w));
} }
printf("%d\n%d\n", paper, ribbon); printf("%d\n%d\n", paper, ribbon);
del_lines(lines); del_lines(lines);
} }

View File

@ -1,6 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "set.h" #include "set.h"
#include "input.h" #include "input.h"
@ -11,7 +10,7 @@ static int part_two(char *input) {
int *x, *y; int *x, *y;
char *c = input; char *c = input;
Set* houses = malloc(sizeof(Set)); Set *houses = malloc(sizeof(Set));
set_init(houses, same_Point_v, free); set_init(houses, same_Point_v, free);
while (*c != '\0') { while (*c != '\0') {
@ -25,16 +24,24 @@ static int part_two(char *input) {
is_santa = is_santa == 1 ? 0 : 1; is_santa = is_santa == 1 ? 0 : 1;
switch (*c) { switch (*c) {
case '^': ++(*y); break; case '^':
case 'v': --(*y); break; ++(*y);
case '>': ++(*x); break; break;
case '<': --(*x); break; case 'v':
--(*y);
break;
case '>':
++(*x);
break;
case '<':
--(*x);
break;
} }
set_insert(houses, (void *) new_Point_p(*x, *y)); set_insert(houses, (void *) new_Point_p(*x, *y));
++c; ++c;
} }
int sz = (int)houses->size; int sz = (int) houses->size;
set_destroy(houses); set_destroy(houses);
return sz; return sz;
@ -44,31 +51,39 @@ static int part_one(char *input) {
int x = 0, y = 0; int x = 0, y = 0;
char *c = input; char *c = input;
Set* houses = malloc(sizeof(Set)); Set *houses = malloc(sizeof(Set));
set_init(houses, same_Point_v, free); set_init(houses, same_Point_v, free);
while (*c != '\0') { while (*c != '\0') {
switch (*c) { switch (*c) {
case '^': ++y; break; case '^':
case 'v': --y; break; ++y;
case '>': ++x; break; break;
case '<': --x; break; case 'v':
--y;
break;
case '>':
++x;
break;
case '<':
--x;
break;
} }
set_insert(houses, (void *) new_Point_p(x, y)); set_insert(houses, (void *) new_Point_p(x, y));
++c; ++c;
} }
int sz = (int)houses->size; int sz = (int) houses->size;
set_destroy(houses); set_destroy(houses);
return sz; return sz;
} }
void advent2015day03(void) { void advent2015day03(void) {
char *input = get_input("input/2015/03"); char *input = get_input("input/2015/03");
printf("%d\n", part_one(input)); printf("%d\n", part_one(input));
printf("%d\n", part_two(input)); printf("%d\n", part_two(input));
free(input); free(input);
} }

View File

@ -6,46 +6,46 @@
#include "advent_utility.h" #include "advent_utility.h"
static int is_five(char *test) { static int is_five(char *test) {
char *t = test; char *t = test;
for (int i = 0; i < 5; ++i, ++t) { for (int i = 0; i < 5; ++i, ++t) {
if (*t != '0') { if (*t != '0') {
return 0; return 0;
}
} }
} return 1;
return 1;
} }
static int is_six(char *test) { static int is_six(char *test) {
char *t = test; char *t = test;
for (int i = 0; i < 6; ++i, ++t) { for (int i = 0; i < 6; ++i, ++t) {
if (*t != '0') { if (*t != '0') {
return 0; return 0;
}
} }
} return 1;
return 1;
} }
void advent2015day04(void) { void advent2015day04(void) {
char *input = get_input("input/2015/04"); char *input = get_input("input/2015/04");
int c = 0, five = 0, six = 0; int c = 0, five = 0, six = 0;
while (!five || !six) { while (!five || !six) {
char test[strlen(input) + 8]; char test[strlen(input) + 8];
sprintf(test, "%s%d", input, c); sprintf(test, "%s%d", input, c);
char *md5 = md5_str(test); char *md5 = md5_str(test);
if (!five && is_five(md5)) { if (!five && is_five(md5)) {
five = c; five = c;
printf("%d\n", five); printf("%d\n", five);
} }
if (is_six(md5)) { if (is_six(md5)) {
six = c; six = c;
}
free(md5);
++c;
} }
free(md5); printf("%d\n", six);
++c; free(input);
}
printf("%d\n", six);
free(input);
} }