aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-12-02 16:33:21 +0100
committerMarvin Borner2020-12-02 16:33:21 +0100
commit2215847cd4a8e686b78253b059775368e05fcaf2 (patch)
tree14220926a3169dd5c5b672fe6057f27b310ed779
parentd812f7787f7394adbd69407e63aaab400ea2b56c (diff)
Removed redundant code
-rw-r--r--2020/2/solve.c35
1 files changed, 13 insertions, 22 deletions
diff --git a/2020/2/solve.c b/2020/2/solve.c
index 66f5577..afcf22f 100644
--- a/2020/2/solve.c
+++ b/2020/2/solve.c
@@ -3,19 +3,13 @@
#include <stdlib.h>
#include <string.h>
-int part_one()
+int part_one(FILE *fp)
{
- FILE *fp = fopen("input", "r");
- if (!fp)
- exit(EXIT_FAILURE);
-
int res = 0;
char *line = NULL;
size_t len = 0;
- ssize_t read = 0;
- while ((read = getline(&line, &len, fp)) != -1) {
- (void)read;
+ while (getline(&line, &len, fp) != -1) {
int low, high, cnt = 0;
char ch;
char str[256];
@@ -29,24 +23,16 @@ int part_one()
res++;
}
- fclose(fp);
-
return res;
}
-int part_two()
+int part_two(FILE *fp)
{
- FILE *fp = fopen("input", "r");
- if (!fp)
- exit(EXIT_FAILURE);
-
int res = 0;
char *line = NULL;
size_t len = 0;
- ssize_t read = 0;
- while ((read = getline(&line, &len, fp)) != -1) {
- (void)read;
+ while (getline(&line, &len, fp) != -1) {
int low, high, cnt = 0;
char ch, first, second;
char str[256];
@@ -57,14 +43,19 @@ int part_two()
res++;
}
- fclose(fp);
-
return res;
}
int main(int argc, char *argv[])
{
- printf("%d\n", part_one());
- printf("%d\n", part_two());
+ FILE *fp = fopen("input", "r");
+ if (!fp)
+ exit(EXIT_FAILURE);
+
+ printf("%d\n", part_one(fp));
+ rewind(fp);
+ printf("%d\n", part_two(fp));
+
+ fclose(fp);
return 0;
}