diff options
author | kartofen <mladenovnasko0@gmail.com> | 2022-12-05 21:24:24 +0200 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2022-12-05 21:24:24 +0200 |
commit | 2140c903d78541c074113b9f683468efe547dea6 (patch) | |
tree | a18080a319a8c180c21ac87cb2ae4ec114a82a41 | |
parent | 023d216b6aecbfafaa50542796200694218b01aa (diff) |
add comments
-rw-r--r-- | Advent-of-Code-2022/aoc-5/main.c | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/Advent-of-Code-2022/aoc-5/main.c b/Advent-of-Code-2022/aoc-5/main.c index 409cf6b..579e33a 100644 --- a/Advent-of-Code-2022/aoc-5/main.c +++ b/Advent-of-Code-2022/aoc-5/main.c @@ -2,7 +2,7 @@ #include <stdlib.h> #include <string.h> -#if 0 +#if 1 #define transfer transfer_part1 #else #define transfer transfer_part2 @@ -21,14 +21,14 @@ typedef struct crate crate; crate *create_crate(char data); void push(crate *c, int i); -crate *add(crate *c, crate *last, int i); +crate *add(crate *c, crate *last, int i); // pushes an element to bottom of a given stack crate *pop(int i); void transfer_part1(int from, int to, int n); void transfer_part2(int from, int to, int n); void free_crates(crate *c); void print_crate(char *format, crate *c); -crate *top[COLUMNS+1] = {0}; +crate *top[COLUMNS+1] = {0}; // +1 for part 2 void parse() { @@ -40,11 +40,13 @@ void parse() char line[256] = {0}; crate *last[COLUMNS] = {0}; - while(fgets(line, sizeof(line), fp) != NULL && line[0] != '\n') + while(fgets(line, sizeof(line), fp) != NULL) { + if(line[0] == '\n') break; // the table has ended + for(int i = 0; i < COLUMNS; i++) { char ch = line[(i*4)+1]; - if(ch >= 65 && ch <= 90) { + if(ch >= 'A' && ch <= 'Z') { last[i] = add(create_crate(ch), last[i], i); } } @@ -52,6 +54,8 @@ void parse() while(fgets(line, sizeof(line), fp) != NULL) { + // we need to call strtok multiple times + // to get the right token (integer) strtok(line, " "); int n = STRINT(" "); int from = STRINT(" "); @@ -126,10 +130,8 @@ void transfer_part1(int from, int to, int n) void transfer_part2(int from, int to, int n) { - for(int i = 0; i < n; i++) - push(pop(from), COLUMNS); - for(int i = 0; i < n; i++) - push(pop(COLUMNS), to); + transfer_part1(from, COLUMNS, n); + transfer_part1(COLUMNS, to, n); } void free_crates(crate *c) |