2022: Day 5 complete

This commit is contained in:
Andrew Scott 2022-12-23 05:59:10 -05:00
parent 4288f36403
commit 9bded90d7b
Signed by: a
GPG key ID: 7CD5A5977E4931C1
2 changed files with 732 additions and 0 deletions

220
2022/5.c Normal file
View file

@ -0,0 +1,220 @@
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 512
#define NUM_LINES 512
#define NUM_STACKS 9
struct Stack {
int top;
char items[MAX_LINE];
};
typedef struct Stack stack;
// Push onto a stack
void push(stack *st, char crate) {
if (st->top == MAX_LINE) {
fprintf(stderr, "Stack is full\n");
} else {
st->top++;
st->items[st->top] = crate;
}
}
// Pop from a stack
char pop(stack *st) {
char c = '\0';
if (st->top == -1) {
fprintf(stderr, "Stack is empty\n");
return -1;
} else {
c = st->items[st->top];
st->items[st->top] = '\0';
st->top--;
return c;
}
}
// Prints top index, top item, and each stack
void print_crates(stack **crates) {
for (size_t i = 0; i < NUM_STACKS; i++) {
printf("\nStack %lu - Top: %d"
"\n Top Crate: %c"
"\n Crates: %s\n",
i + 1, crates[i]->top, crates[i]->items[crates[i]->top],
crates[i]->items);
fflush(stdout);
}
}
// Allocate memeory and initialize stacks of crates with their initial order
stack **crates_init() {
stack **crates = calloc(NUM_STACKS, sizeof(stack *));
if (crates == NULL) {
return NULL;
}
for (int i = 0; i < NUM_STACKS; i++) {
crates[i] = (stack *)malloc(sizeof(stack));
if (crates[i] == NULL) {
return NULL;
}
crates[i]->top = -1;
memset(crates[i]->items, 0, MAX_LINE);
}
crates[0]->top = 7;
strcpy(crates[0]->items, "PFMQWGRT");
crates[1]->top = 2;
strcpy(crates[1]->items, "HFR");
crates[2]->top = 7;
strcpy(crates[2]->items, "PZRVGHSD");
crates[3]->top = 6;
strcpy(crates[3]->items, "QHPBFWG");
crates[4]->top = 4;
strcpy(crates[4]->items, "PSMJH");
crates[5]->top = 7;
strcpy(crates[5]->items, "MZTHSRPL");
crates[6]->top = 5;
strcpy(crates[6]->items, "PTHNML");
crates[7]->top = 3;
strcpy(crates[7]->items, "FDQR");
crates[8]->top = 6;
strcpy(crates[8]->items, "DSCNLPH");
return crates;
}
// Parses moves from input file and stores in an array
void parse_moves(FILE *file, int moves[NUM_LINES][3], size_t *line_num) {
int idx = 0;
char line[MAX_LINE] = {0};
while (fgets(line, MAX_LINE, file) != NULL) {
if (line[0] != 'm') {
memset(line, 0, MAX_LINE);
continue;
}
for (size_t i = 0; i < strlen(line); i++) {
if (isdigit(line[i])) {
if (isdigit(line[i + 1])) {
char temp[2];
temp[0] = line[i];
temp[1] = line[i + 1];
moves[*line_num][idx] = atoi(temp);
i++;
} else {
moves[*line_num][idx] = atoi(&line[i]);
}
idx++;
if (idx > 2) {
*line_num += 1;
idx = 0;
}
}
}
memset(line, 0, MAX_LINE);
}
}
// Part 1 - Processes each move stored in the moves array one crate at a time
void part1_moves(stack **crates, int moves[NUM_LINES][3], size_t move_count) {
size_t idx = 0;
while (idx <= move_count) {
for (int i = 0; i < moves[idx][0]; i++) {
char c = pop(crates[moves[idx][1] - 1]);
if (c == -1) {
fprintf(stderr, "part1_moves: move num: %lu", idx);
continue;
}
push(crates[moves[idx][2] - 1], c);
}
idx++;
}
}
void crates_free(stack **crates) {
for (size_t i = 0; i < NUM_STACKS; i++) {
free(crates[i]);
}
free(crates);
}
// Part 2 - Processes the moves array multiple crates at a time
void part2_moves(stack **crates, int moves[NUM_LINES][3], size_t move_count) {
size_t idx = 0;
while (idx <= move_count) {
char c_arr[moves[idx][0]];
memset(c_arr, 0, moves[idx][0]);
for (int i = 0; i < moves[idx][0]; i++) {
c_arr[i] = pop(crates[moves[idx][1] - 1]);
if (c_arr[i] == -1) {
fprintf(stderr, "part2_moves: move num: %lu", idx);
continue;
}
}
for (int j = moves[idx][0] - 1; j >= 0; j--) {
push(crates[moves[idx][2] - 1], c_arr[j]);
}
idx++;
}
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "USAGE: %s input_file\n", argv[0]);
return 1;
}
FILE *file = fopen(argv[1], "r");
if (!file) {
perror("fopen");
return 1;
}
size_t move_count = 0;
int moves[NUM_LINES][3] = {0};
stack **crates = crates_init();
if (crates == NULL) {
fprintf(stderr, "Part 2: unable to allocate memory for crates\n");
fclose(file);
return 1;
}
printf("\n--- PART 1 ---\n");
parse_moves(file, moves, &move_count);
part1_moves(crates, moves, move_count);
print_crates(crates);
printf("\n--- PART 2 ---\n");
crates_free(crates);
crates = crates_init();
if (crates == NULL) {
fprintf(stderr, "Part 2: unable to allocate memory for crates\n");
fclose(file);
return 1;
}
part2_moves(crates, moves, move_count);
print_crates(crates);
crates_free(crates);
fclose(file);
return 0;
}

512
2022/5.txt Normal file
View file

@ -0,0 +1,512 @@
[T] [D] [L]
[R] [S] [G] [P] [H]
[G] [H] [W] [R] [L] [P]
[W] [G] [F] [H] [S] [M] [L]
[Q] [V] [B] [J] [H] [N] [R] [N]
[M] [R] [R] [P] [M] [T] [H] [Q] [C]
[F] [F] [Z] [H] [S] [Z] [T] [D] [S]
[P] [H] [P] [Q] [P] [M] [P] [F] [D]
1 2 3 4 5 6 7 8 9
move 3 from 8 to 9
move 2 from 2 to 8
move 5 from 4 to 2
move 7 from 1 to 4
move 3 from 8 to 2
move 3 from 2 to 7
move 1 from 7 to 4
move 3 from 2 to 9
move 4 from 7 to 9
move 1 from 5 to 2
move 2 from 3 to 4
move 5 from 9 to 5
move 6 from 9 to 3
move 5 from 9 to 5
move 1 from 9 to 7
move 2 from 3 to 1
move 7 from 3 to 9
move 2 from 7 to 2
move 5 from 2 to 4
move 1 from 2 to 9
move 2 from 1 to 9
move 7 from 6 to 1
move 2 from 7 to 3
move 2 from 3 to 9
move 1 from 7 to 4
move 1 from 9 to 2
move 3 from 1 to 8
move 2 from 3 to 4
move 5 from 9 to 2
move 1 from 3 to 9
move 8 from 5 to 7
move 1 from 6 to 1
move 15 from 4 to 1
move 4 from 2 to 5
move 5 from 9 to 7
move 1 from 9 to 5
move 5 from 1 to 2
move 3 from 8 to 9
move 1 from 7 to 6
move 11 from 1 to 2
move 7 from 5 to 3
move 4 from 2 to 6
move 7 from 3 to 4
move 3 from 5 to 9
move 2 from 2 to 5
move 5 from 1 to 8
move 2 from 6 to 8
move 3 from 8 to 9
move 9 from 4 to 9
move 9 from 7 to 4
move 2 from 8 to 1
move 1 from 8 to 7
move 6 from 2 to 7
move 5 from 2 to 4
move 5 from 7 to 2
move 2 from 1 to 7
move 2 from 6 to 4
move 7 from 7 to 1
move 3 from 2 to 6
move 1 from 8 to 7
move 2 from 9 to 3
move 2 from 3 to 1
move 1 from 2 to 5
move 4 from 6 to 5
move 2 from 2 to 3
move 3 from 5 to 7
move 1 from 5 to 3
move 9 from 1 to 7
move 2 from 9 to 5
move 13 from 4 to 1
move 5 from 7 to 2
move 3 from 3 to 1
move 2 from 2 to 9
move 1 from 2 to 7
move 5 from 5 to 6
move 2 from 2 to 4
move 5 from 1 to 3
move 9 from 7 to 8
move 2 from 9 to 5
move 3 from 5 to 4
move 5 from 9 to 2
move 10 from 4 to 8
move 1 from 4 to 1
move 2 from 8 to 4
move 4 from 8 to 2
move 3 from 6 to 8
move 7 from 8 to 7
move 10 from 9 to 3
move 7 from 3 to 2
move 11 from 2 to 3
move 13 from 3 to 9
move 1 from 6 to 3
move 1 from 1 to 2
move 1 from 2 to 8
move 3 from 3 to 4
move 1 from 2 to 9
move 1 from 4 to 1
move 10 from 8 to 3
move 11 from 9 to 7
move 1 from 6 to 2
move 14 from 7 to 1
move 2 from 2 to 9
move 4 from 7 to 6
move 1 from 2 to 4
move 3 from 4 to 2
move 4 from 2 to 9
move 10 from 3 to 4
move 3 from 6 to 1
move 5 from 9 to 5
move 5 from 5 to 8
move 1 from 9 to 7
move 2 from 9 to 6
move 1 from 9 to 8
move 2 from 4 to 8
move 1 from 4 to 5
move 2 from 3 to 1
move 2 from 3 to 7
move 27 from 1 to 2
move 2 from 7 to 1
move 9 from 4 to 6
move 9 from 6 to 5
move 5 from 8 to 6
move 26 from 2 to 3
move 1 from 2 to 5
move 1 from 2 to 7
move 1 from 8 to 4
move 1 from 7 to 8
move 24 from 3 to 5
move 1 from 8 to 5
move 1 from 4 to 3
move 1 from 7 to 1
move 1 from 8 to 9
move 7 from 1 to 7
move 8 from 6 to 4
move 4 from 7 to 6
move 1 from 3 to 9
move 2 from 9 to 1
move 3 from 7 to 9
move 8 from 4 to 6
move 3 from 9 to 1
move 1 from 3 to 6
move 1 from 8 to 2
move 10 from 5 to 4
move 1 from 3 to 8
move 13 from 5 to 3
move 1 from 2 to 9
move 1 from 8 to 9
move 1 from 3 to 8
move 1 from 9 to 2
move 3 from 6 to 9
move 7 from 4 to 9
move 4 from 3 to 9
move 2 from 6 to 8
move 2 from 4 to 5
move 10 from 9 to 3
move 1 from 1 to 9
move 1 from 4 to 8
move 1 from 1 to 4
move 1 from 4 to 5
move 4 from 6 to 3
move 1 from 9 to 5
move 1 from 6 to 9
move 2 from 6 to 5
move 1 from 9 to 2
move 1 from 6 to 7
move 18 from 5 to 2
move 22 from 3 to 7
move 19 from 7 to 1
move 3 from 8 to 5
move 4 from 9 to 3
move 2 from 7 to 2
move 1 from 8 to 1
move 19 from 1 to 3
move 2 from 7 to 5
move 13 from 3 to 9
move 4 from 1 to 2
move 3 from 5 to 1
move 11 from 9 to 1
move 11 from 2 to 8
move 3 from 9 to 3
move 3 from 5 to 2
move 2 from 1 to 4
move 5 from 2 to 7
move 12 from 1 to 5
move 2 from 4 to 5
move 9 from 5 to 8
move 1 from 5 to 3
move 4 from 2 to 3
move 2 from 7 to 5
move 6 from 2 to 8
move 17 from 8 to 9
move 2 from 9 to 6
move 2 from 7 to 1
move 15 from 9 to 6
move 2 from 2 to 4
move 9 from 8 to 5
move 2 from 1 to 3
move 12 from 6 to 2
move 2 from 3 to 9
move 5 from 6 to 3
move 4 from 5 to 3
move 11 from 3 to 4
move 2 from 9 to 4
move 6 from 5 to 2
move 13 from 4 to 3
move 1 from 4 to 5
move 1 from 4 to 8
move 18 from 2 to 6
move 2 from 5 to 3
move 1 from 8 to 3
move 1 from 2 to 5
move 1 from 7 to 8
move 28 from 3 to 6
move 2 from 3 to 4
move 3 from 5 to 9
move 2 from 5 to 9
move 3 from 9 to 3
move 5 from 3 to 4
move 1 from 9 to 3
move 1 from 9 to 1
move 1 from 3 to 4
move 45 from 6 to 2
move 1 from 8 to 3
move 2 from 4 to 6
move 5 from 4 to 2
move 1 from 3 to 7
move 3 from 2 to 9
move 1 from 4 to 8
move 3 from 6 to 1
move 42 from 2 to 8
move 2 from 9 to 2
move 4 from 2 to 6
move 2 from 2 to 7
move 1 from 9 to 6
move 2 from 8 to 9
move 4 from 1 to 8
move 1 from 6 to 4
move 1 from 4 to 8
move 1 from 2 to 5
move 3 from 7 to 4
move 39 from 8 to 3
move 7 from 8 to 5
move 8 from 5 to 7
move 35 from 3 to 1
move 4 from 3 to 7
move 10 from 7 to 2
move 2 from 9 to 6
move 3 from 4 to 2
move 1 from 7 to 5
move 1 from 7 to 8
move 1 from 5 to 4
move 12 from 1 to 6
move 1 from 8 to 1
move 1 from 4 to 5
move 14 from 6 to 8
move 9 from 8 to 6
move 5 from 6 to 1
move 11 from 2 to 9
move 1 from 9 to 8
move 6 from 8 to 3
move 6 from 9 to 2
move 8 from 1 to 9
move 3 from 3 to 6
move 7 from 1 to 4
move 1 from 5 to 9
move 8 from 9 to 8
move 7 from 6 to 8
move 1 from 9 to 3
move 3 from 6 to 4
move 3 from 9 to 1
move 4 from 3 to 2
move 1 from 6 to 7
move 1 from 4 to 2
move 13 from 1 to 7
move 6 from 4 to 8
move 1 from 7 to 3
move 1 from 4 to 6
move 1 from 9 to 5
move 1 from 3 to 5
move 19 from 8 to 9
move 1 from 6 to 5
move 6 from 9 to 2
move 2 from 5 to 8
move 1 from 5 to 2
move 4 from 1 to 4
move 8 from 9 to 4
move 3 from 9 to 8
move 2 from 9 to 1
move 6 from 7 to 5
move 12 from 4 to 2
move 6 from 8 to 3
move 1 from 4 to 1
move 1 from 3 to 1
move 13 from 2 to 3
move 4 from 5 to 3
move 1 from 4 to 9
move 1 from 8 to 9
move 12 from 3 to 2
move 1 from 9 to 1
move 2 from 5 to 9
move 3 from 9 to 5
move 1 from 7 to 5
move 3 from 7 to 3
move 1 from 5 to 4
move 1 from 5 to 8
move 9 from 2 to 3
move 2 from 2 to 3
move 3 from 1 to 9
move 1 from 8 to 9
move 3 from 9 to 1
move 9 from 2 to 6
move 1 from 9 to 5
move 6 from 2 to 3
move 2 from 6 to 9
move 3 from 6 to 3
move 1 from 4 to 3
move 2 from 9 to 6
move 2 from 7 to 2
move 2 from 2 to 8
move 24 from 3 to 7
move 2 from 5 to 6
move 2 from 8 to 2
move 7 from 2 to 8
move 8 from 3 to 6
move 2 from 1 to 3
move 1 from 1 to 2
move 1 from 5 to 2
move 15 from 7 to 4
move 9 from 7 to 9
move 7 from 9 to 1
move 5 from 8 to 1
move 4 from 1 to 4
move 19 from 4 to 3
move 22 from 3 to 5
move 1 from 7 to 5
move 9 from 5 to 4
move 6 from 1 to 3
move 6 from 3 to 1
move 4 from 5 to 4
move 1 from 2 to 1
move 1 from 2 to 6
move 4 from 6 to 1
move 1 from 3 to 6
move 3 from 6 to 3
move 2 from 9 to 8
move 2 from 5 to 3
move 2 from 5 to 1
move 10 from 6 to 4
move 4 from 4 to 9
move 7 from 4 to 3
move 2 from 8 to 7
move 4 from 9 to 3
move 5 from 5 to 7
move 1 from 5 to 1
move 1 from 6 to 3
move 1 from 8 to 4
move 1 from 8 to 3
move 13 from 4 to 5
move 1 from 1 to 8
move 6 from 5 to 3
move 1 from 7 to 6
move 5 from 7 to 6
move 9 from 1 to 8
move 1 from 8 to 4
move 1 from 7 to 1
move 1 from 4 to 1
move 5 from 3 to 7
move 3 from 7 to 9
move 1 from 5 to 4
move 6 from 8 to 6
move 1 from 9 to 3
move 2 from 9 to 5
move 7 from 5 to 9
move 1 from 7 to 5
move 2 from 5 to 3
move 10 from 6 to 8
move 2 from 6 to 1
move 1 from 4 to 9
move 1 from 7 to 5
move 8 from 8 to 2
move 1 from 1 to 7
move 1 from 9 to 7
move 1 from 5 to 1
move 3 from 9 to 8
move 7 from 8 to 7
move 6 from 7 to 1
move 1 from 8 to 7
move 4 from 7 to 1
move 16 from 3 to 7
move 4 from 3 to 1
move 5 from 7 to 8
move 16 from 1 to 4
move 9 from 1 to 7
move 1 from 3 to 4
move 15 from 4 to 8
move 1 from 3 to 1
move 2 from 1 to 6
move 2 from 4 to 9
move 17 from 8 to 2
move 6 from 9 to 5
move 8 from 7 to 8
move 2 from 6 to 9
move 4 from 5 to 7
move 2 from 8 to 5
move 1 from 5 to 9
move 11 from 2 to 6
move 4 from 6 to 1
move 5 from 2 to 8
move 2 from 9 to 2
move 1 from 9 to 3
move 3 from 1 to 8
move 1 from 3 to 6
move 7 from 6 to 9
move 2 from 5 to 4
move 6 from 7 to 4
move 4 from 8 to 1
move 1 from 5 to 2
move 1 from 6 to 1
move 7 from 9 to 8
move 2 from 7 to 9
move 9 from 2 to 9
move 5 from 9 to 3
move 3 from 2 to 8
move 4 from 8 to 7
move 9 from 7 to 2
move 3 from 1 to 3
move 14 from 8 to 1
move 2 from 8 to 3
move 1 from 9 to 4
move 3 from 7 to 9
move 8 from 3 to 9
move 2 from 2 to 7
move 12 from 1 to 8
move 4 from 1 to 6
move 2 from 6 to 7
move 1 from 6 to 7
move 9 from 4 to 7
move 9 from 7 to 4
move 1 from 1 to 6
move 2 from 3 to 6
move 2 from 6 to 8
move 12 from 9 to 8
move 2 from 6 to 9
move 2 from 9 to 7
move 1 from 8 to 5
move 5 from 7 to 5
move 1 from 9 to 1
move 3 from 4 to 1
move 5 from 4 to 8
move 4 from 1 to 7
move 1 from 4 to 2
move 19 from 8 to 4
move 2 from 7 to 5
move 14 from 8 to 5
move 2 from 7 to 8
move 3 from 9 to 8
move 19 from 4 to 2
move 9 from 2 to 4
move 2 from 7 to 8
move 15 from 5 to 9
move 15 from 9 to 8
move 1 from 5 to 9
move 11 from 8 to 7
move 4 from 5 to 8
move 1 from 5 to 9
move 2 from 9 to 5
move 2 from 2 to 6
move 14 from 2 to 9
move 12 from 8 to 9
move 3 from 8 to 4
move 7 from 9 to 2
move 4 from 7 to 9
move 1 from 6 to 9
move 1 from 7 to 5
move 1 from 6 to 2
move 3 from 5 to 4
move 19 from 9 to 4
move 1 from 5 to 1
move 1 from 9 to 8
move 1 from 1 to 7
move 1 from 8 to 9
move 4 from 7 to 2
move 3 from 7 to 6
move 18 from 4 to 2
move 17 from 2 to 3
move 2 from 6 to 8
move 17 from 3 to 6
move 13 from 2 to 1
move 2 from 8 to 3
move 2 from 2 to 9
move 6 from 1 to 9
move 1 from 3 to 4
move 1 from 3 to 9
move 8 from 6 to 4
move 20 from 4 to 8
move 3 from 4 to 8
move 15 from 8 to 2
move 11 from 2 to 6
move 2 from 1 to 7
move 7 from 9 to 8
move 6 from 9 to 3
move 1 from 6 to 5