commit41c5829f4861951e0a948dd208a4e211201af0dbparent6fad1ecdde0cd28dcf7c98994b4dbd1709727a55Author:Jake Bauer <jbauer@paritybit.ca>Date:Mon, 5 Dec 2022 17:58:43 -0500 Day 5 challenge completeDiffstat:

A | day5/input.txt | | | 503 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |

A | day5/part1.c | | | 113 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |

A | day5/part2.c | | | 116 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |

3 files changed, 732 insertions(+), 0 deletions(-)diff --git a/day5/input.txt b/day5/input.txt@@ -0,0 +1,503 @@ +move 1 from 3 to 5 +move 5 from 5 to 4 +move 6 from 7 to 3 +move 6 from 1 to 3 +move 1 from 1 to 9 +move 1 from 1 to 4 +move 3 from 6 to 9 +move 2 from 7 to 5 +move 1 from 5 to 7 +move 1 from 7 to 2 +move 2 from 2 to 5 +move 2 from 6 to 3 +move 6 from 8 to 9 +move 7 from 3 to 9 +move 1 from 8 to 7 +move 8 from 9 to 7 +move 5 from 4 to 8 +move 1 from 6 to 2 +move 2 from 8 to 4 +move 9 from 9 to 1 +move 2 from 8 to 5 +move 1 from 8 to 5 +move 5 from 9 to 2 +move 1 from 6 to 8 +move 5 from 1 to 7 +move 1 from 8 to 2 +move 2 from 1 to 7 +move 1 from 2 to 6 +move 4 from 5 to 4 +move 2 from 1 to 4 +move 13 from 7 to 8 +move 3 from 8 to 6 +move 2 from 6 to 8 +move 10 from 3 to 5 +move 2 from 7 to 6 +move 3 from 5 to 6 +move 10 from 8 to 1 +move 1 from 8 to 6 +move 6 from 2 to 4 +move 1 from 5 to 8 +move 5 from 6 to 3 +move 2 from 8 to 6 +move 1 from 7 to 9 +move 2 from 2 to 7 +move 3 from 5 to 1 +move 2 from 7 to 2 +move 6 from 6 to 3 +move 7 from 5 to 6 +move 5 from 3 to 2 +move 10 from 1 to 8 +move 2 from 1 to 3 +move 8 from 3 to 7 +move 9 from 4 to 8 +move 1 from 9 to 2 +move 2 from 7 to 8 +move 4 from 6 to 9 +move 1 from 4 to 9 +move 5 from 7 to 4 +move 3 from 6 to 5 +move 1 from 1 to 5 +move 14 from 4 to 8 +move 3 from 9 to 7 +move 4 from 5 to 9 +move 2 from 4 to 1 +move 27 from 8 to 6 +move 2 from 7 to 2 +move 2 from 7 to 4 +move 4 from 2 to 9 +move 7 from 8 to 4 +move 10 from 4 to 1 +move 18 from 6 to 5 +move 6 from 9 to 2 +move 1 from 9 to 5 +move 11 from 2 to 6 +move 2 from 5 to 4 +move 1 from 2 to 8 +move 2 from 4 to 9 +move 2 from 8 to 3 +move 1 from 6 to 8 +move 4 from 9 to 7 +move 4 from 7 to 8 +move 7 from 5 to 1 +move 4 from 6 to 3 +move 2 from 3 to 7 +move 6 from 5 to 3 +move 2 from 8 to 2 +move 14 from 6 to 2 +move 3 from 8 to 1 +move 15 from 2 to 3 +move 1 from 6 to 1 +move 14 from 3 to 2 +move 2 from 2 to 5 +move 1 from 9 to 3 +move 13 from 1 to 3 +move 4 from 2 to 6 +move 10 from 1 to 3 +move 2 from 6 to 9 +move 6 from 2 to 9 +move 6 from 5 to 2 +move 2 from 6 to 8 +move 7 from 9 to 5 +move 1 from 5 to 8 +move 2 from 7 to 6 +move 34 from 3 to 6 +move 19 from 6 to 2 +move 12 from 6 to 9 +move 3 from 6 to 3 +move 2 from 3 to 2 +move 1 from 6 to 5 +move 17 from 2 to 8 +move 2 from 3 to 2 +move 8 from 9 to 4 +move 7 from 5 to 2 +move 5 from 4 to 1 +move 4 from 1 to 6 +move 1 from 1 to 6 +move 6 from 6 to 8 +move 2 from 8 to 4 +move 17 from 8 to 6 +move 2 from 4 to 5 +move 17 from 6 to 9 +move 22 from 9 to 7 +move 1 from 5 to 2 +move 20 from 2 to 7 +move 29 from 7 to 9 +move 1 from 4 to 7 +move 3 from 8 to 3 +move 1 from 8 to 5 +move 3 from 8 to 2 +move 2 from 2 to 4 +move 27 from 9 to 7 +move 2 from 3 to 2 +move 1 from 5 to 2 +move 18 from 7 to 5 +move 1 from 3 to 2 +move 1 from 5 to 6 +move 18 from 5 to 3 +move 1 from 6 to 3 +move 2 from 9 to 5 +move 10 from 3 to 5 +move 4 from 3 to 6 +move 1 from 7 to 1 +move 1 from 5 to 1 +move 6 from 7 to 6 +move 1 from 6 to 2 +move 4 from 4 to 8 +move 5 from 5 to 4 +move 1 from 3 to 8 +move 2 from 1 to 8 +move 2 from 2 to 5 +move 3 from 3 to 8 +move 6 from 8 to 2 +move 1 from 3 to 9 +move 1 from 6 to 3 +move 6 from 2 to 8 +move 7 from 8 to 4 +move 8 from 5 to 2 +move 5 from 4 to 6 +move 2 from 8 to 3 +move 2 from 3 to 9 +move 1 from 3 to 9 +move 2 from 7 to 1 +move 2 from 1 to 2 +move 12 from 2 to 4 +move 1 from 9 to 7 +move 1 from 6 to 2 +move 9 from 7 to 9 +move 1 from 8 to 2 +move 9 from 9 to 8 +move 6 from 7 to 8 +move 4 from 4 to 1 +move 6 from 2 to 5 +move 1 from 4 to 9 +move 3 from 1 to 9 +move 6 from 4 to 5 +move 5 from 8 to 9 +move 8 from 4 to 6 +move 3 from 9 to 8 +move 1 from 9 to 3 +move 3 from 8 to 3 +move 5 from 9 to 2 +move 3 from 2 to 6 +move 3 from 6 to 9 +move 3 from 6 to 2 +move 4 from 2 to 6 +move 6 from 9 to 7 +move 1 from 1 to 8 +move 8 from 8 to 5 +move 20 from 5 to 3 +move 2 from 2 to 8 +move 6 from 7 to 1 +move 10 from 6 to 3 +move 4 from 6 to 7 +move 4 from 1 to 9 +move 2 from 1 to 2 +move 3 from 6 to 9 +move 5 from 8 to 3 +move 3 from 7 to 9 +move 17 from 3 to 2 +move 1 from 6 to 2 +move 2 from 6 to 9 +move 1 from 6 to 4 +move 12 from 9 to 2 +move 1 from 4 to 7 +move 8 from 3 to 8 +move 8 from 8 to 9 +move 7 from 9 to 2 +move 1 from 9 to 7 +move 18 from 2 to 9 +move 1 from 7 to 2 +move 2 from 7 to 1 +move 1 from 1 to 2 +move 4 from 2 to 7 +move 15 from 9 to 3 +move 1 from 9 to 1 +move 2 from 1 to 8 +move 6 from 2 to 4 +move 8 from 2 to 1 +move 2 from 8 to 5 +move 2 from 9 to 3 +move 4 from 4 to 1 +move 2 from 5 to 8 +move 2 from 8 to 9 +move 14 from 3 to 1 +move 2 from 9 to 7 +move 2 from 4 to 3 +move 1 from 2 to 9 +move 5 from 7 to 9 +move 21 from 1 to 9 +move 2 from 1 to 6 +move 3 from 2 to 4 +move 1 from 7 to 3 +move 19 from 9 to 5 +move 1 from 2 to 7 +move 1 from 7 to 2 +move 3 from 4 to 2 +move 19 from 5 to 7 +move 2 from 2 to 5 +move 1 from 5 to 3 +move 1 from 3 to 4 +move 8 from 9 to 4 +move 1 from 6 to 3 +move 1 from 2 to 6 +move 1 from 2 to 1 +move 8 from 7 to 3 +move 5 from 4 to 7 +move 2 from 6 to 4 +move 1 from 5 to 9 +move 1 from 1 to 6 +move 1 from 1 to 2 +move 2 from 4 to 7 +move 1 from 4 to 2 +move 2 from 4 to 9 +move 1 from 6 to 8 +move 1 from 1 to 5 +move 1 from 8 to 6 +move 1 from 1 to 4 +move 25 from 3 to 1 +move 1 from 4 to 2 +move 2 from 3 to 6 +move 3 from 1 to 9 +move 6 from 9 to 8 +move 1 from 6 to 3 +move 1 from 2 to 9 +move 15 from 7 to 6 +move 2 from 2 to 6 +move 1 from 3 to 8 +move 1 from 1 to 4 +move 6 from 8 to 4 +move 1 from 3 to 8 +move 1 from 8 to 5 +move 2 from 5 to 2 +move 8 from 6 to 7 +move 1 from 8 to 7 +move 1 from 9 to 4 +move 9 from 4 to 5 +move 19 from 1 to 3 +move 9 from 3 to 5 +move 6 from 7 to 2 +move 2 from 1 to 7 +move 7 from 2 to 4 +move 7 from 5 to 6 +move 5 from 4 to 3 +move 3 from 5 to 8 +move 1 from 2 to 4 +move 2 from 4 to 8 +move 14 from 6 to 1 +move 6 from 5 to 6 +move 1 from 5 to 2 +move 7 from 1 to 6 +move 1 from 2 to 4 +move 4 from 6 to 4 +move 1 from 5 to 4 +move 2 from 1 to 9 +move 2 from 9 to 4 +move 2 from 1 to 8 +move 9 from 3 to 6 +move 3 from 7 to 4 +move 4 from 8 to 6 +move 3 from 7 to 6 +move 1 from 7 to 2 +move 1 from 7 to 5 +move 3 from 8 to 4 +move 26 from 6 to 1 +move 8 from 1 to 2 +move 1 from 6 to 4 +move 5 from 2 to 7 +move 2 from 2 to 4 +move 10 from 4 to 7 +move 1 from 6 to 1 +move 22 from 1 to 2 +move 1 from 6 to 1 +move 6 from 4 to 7 +move 1 from 5 to 1 +move 1 from 1 to 2 +move 21 from 7 to 2 +move 38 from 2 to 3 +move 8 from 2 to 6 +move 2 from 4 to 8 +move 2 from 8 to 2 +move 1 from 1 to 3 +move 1 from 2 to 8 +move 1 from 2 to 5 +move 6 from 6 to 4 +move 2 from 4 to 2 +move 2 from 2 to 6 +move 1 from 8 to 2 +move 28 from 3 to 1 +move 11 from 1 to 2 +move 8 from 1 to 7 +move 4 from 6 to 4 +move 8 from 3 to 1 +move 8 from 2 to 5 +move 6 from 5 to 4 +move 2 from 5 to 4 +move 8 from 3 to 4 +move 22 from 4 to 1 +move 2 from 3 to 5 +move 33 from 1 to 5 +move 26 from 5 to 6 +move 4 from 5 to 7 +move 2 from 2 to 7 +move 2 from 7 to 2 +move 2 from 7 to 8 +move 2 from 8 to 3 +move 6 from 1 to 3 +move 5 from 5 to 1 +move 1 from 5 to 7 +move 7 from 7 to 5 +move 4 from 5 to 6 +move 5 from 1 to 8 +move 4 from 2 to 4 +move 2 from 7 to 4 +move 2 from 7 to 3 +move 5 from 4 to 6 +move 1 from 8 to 2 +move 1 from 2 to 4 +move 10 from 3 to 6 +move 44 from 6 to 9 +move 2 from 5 to 7 +move 1 from 5 to 8 +move 41 from 9 to 1 +move 1 from 6 to 4 +move 2 from 8 to 1 +move 1 from 7 to 3 +move 1 from 3 to 8 +move 2 from 9 to 8 +move 29 from 1 to 9 +move 2 from 1 to 5 +move 2 from 8 to 3 +move 1 from 3 to 5 +move 2 from 5 to 9 +move 1 from 5 to 7 +move 25 from 9 to 2 +move 10 from 2 to 1 +move 1 from 7 to 8 +move 2 from 4 to 1 +move 2 from 8 to 9 +move 1 from 8 to 6 +move 4 from 2 to 4 +move 4 from 2 to 5 +move 1 from 6 to 5 +move 1 from 2 to 7 +move 2 from 4 to 1 +move 18 from 1 to 3 +move 8 from 9 to 4 +move 15 from 3 to 9 +move 3 from 4 to 8 +move 4 from 5 to 8 +move 4 from 2 to 4 +move 10 from 9 to 4 +move 4 from 8 to 5 +move 2 from 7 to 2 +move 11 from 4 to 9 +move 12 from 4 to 9 +move 2 from 5 to 7 +move 4 from 2 to 4 +move 5 from 8 to 1 +move 1 from 5 to 6 +move 1 from 4 to 6 +move 1 from 3 to 9 +move 1 from 5 to 7 +move 4 from 1 to 6 +move 6 from 1 to 5 +move 6 from 5 to 9 +move 3 from 7 to 6 +move 9 from 6 to 5 +move 8 from 5 to 2 +move 7 from 2 to 3 +move 1 from 3 to 1 +move 7 from 3 to 5 +move 2 from 4 to 1 +move 1 from 2 to 6 +move 2 from 1 to 3 +move 8 from 5 to 9 +move 3 from 1 to 3 +move 1 from 6 to 1 +move 2 from 4 to 1 +move 1 from 5 to 2 +move 2 from 1 to 6 +move 2 from 6 to 3 +move 2 from 3 to 2 +move 2 from 2 to 4 +move 1 from 2 to 6 +move 3 from 3 to 9 +move 2 from 4 to 8 +move 3 from 3 to 1 +move 4 from 1 to 7 +move 2 from 8 to 4 +move 7 from 9 to 6 +move 1 from 1 to 4 +move 11 from 9 to 7 +move 3 from 9 to 3 +move 14 from 9 to 5 +move 6 from 6 to 5 +move 4 from 5 to 9 +move 10 from 7 to 6 +move 1 from 3 to 7 +move 2 from 4 to 1 +move 4 from 7 to 9 +move 9 from 6 to 1 +move 3 from 6 to 5 +move 15 from 9 to 1 +move 1 from 4 to 7 +move 4 from 9 to 7 +move 12 from 5 to 1 +move 3 from 7 to 3 +move 4 from 7 to 2 +move 1 from 9 to 3 +move 22 from 1 to 2 +move 21 from 2 to 6 +move 3 from 1 to 9 +move 1 from 3 to 7 +move 1 from 7 to 3 +move 1 from 3 to 2 +move 8 from 1 to 4 +move 1 from 9 to 2 +move 7 from 4 to 8 +move 3 from 3 to 9 +move 3 from 3 to 5 +move 4 from 2 to 3 +move 1 from 1 to 3 +move 4 from 8 to 5 +move 2 from 8 to 3 +move 5 from 3 to 2 +move 6 from 5 to 3 +move 2 from 5 to 8 +move 2 from 1 to 7 +move 2 from 7 to 4 +move 15 from 6 to 9 +move 8 from 3 to 1 +move 3 from 5 to 9 +move 2 from 4 to 9 +move 8 from 1 to 3 +move 8 from 9 to 8 +move 1 from 1 to 4 +move 3 from 5 to 9 +move 4 from 8 to 1 +move 1 from 3 to 9 +move 2 from 4 to 3 +move 2 from 8 to 6 +move 3 from 8 to 7 +move 8 from 2 to 5 +move 3 from 5 to 2 +move 4 from 3 to 4 +move 3 from 6 to 1 +move 2 from 5 to 9 +move 4 from 4 to 1 +move 2 from 5 to 6 +move 1 from 5 to 4 +move 2 from 2 to 1 +move 4 from 3 to 9 +move 1 from 7 to 3 +move 2 from 7 to 4 +move 2 from 4 to 7 +move 1 from 6 to 7 +move 1 from 2 to 8 +move 2 from 3 to 9 +move 14 from 1 to 8 +move 1 from 6 to 2 +move 2 from 7 to 1 +move 3 from 8 to 3 +move 6 from 8 to 5diff --git a/day5/part1.c b/day5/part1.c@@ -0,0 +1,113 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <err.h> + +struct stack { + char contents[64]; + int size; +}; + +char +pop(struct stack *stack) +{ + stack->size--; + return stack->contents[stack->size]; +} + +void +push(struct stack *stack, char item) +{ + stack->contents[stack->size++] = item; +} + +void +splitString(char *string, char delim, char **result) +{ + int index = 0; + for (int i = 0; i < strlen(string); i++) + { + if (string[i] == delim) + index++; + else + strncat(result[index], &string[i], 1); + } +} + +int +main (void) +{ + FILE *fp = fopen("input.txt", "r"); + if (fp == NULL) + { + err(1, "Failed to open input.txt"); + exit(EXIT_FAILURE); + } + + struct stack *stacks[9]; + for (int i = 0; i < 9; i++) + { + stacks[i] = (struct stack*)malloc(1*sizeof(struct stack)); + stacks[i]->size = 0; + } + + // This is way easier than trying to parse the drawing + for (int i = 0; i < strlen("DTRBJLWG"); i++) + push(stacks[0], "DTRBJLWG"[i]); + for (int i = 0; i < strlen("SWC"); i++) + push(stacks[1], "SWC"[i]); + for (int i = 0; i < strlen("RZTM"); i++) + push(stacks[2], "RZTM"[i]); + for (int i = 0; i < strlen("DTCHSPV"); i++) + push(stacks[3], "DTCHSPV"[i]); + for (int i = 0; i < strlen("GPTLDZ"); i++) + push(stacks[4], "GPTLDZ"[i]); + for (int i = 0; i < strlen("FBRZJQCD"); i++) + push(stacks[5], "FBRZJQCD"[i]); + for (int i = 0; i < strlen("SBDJMFTR"); i++) + push(stacks[6], "SBDJMFTR"[i]); + for (int i = 0; i < strlen("LHRBTVM"); i++) + push(stacks[7], "LHRBTVM"[i]); + for (int i = 0; i < strlen("QPDSV"); i++) + push(stacks[8], "QPDSV"[i]); + + char *command[6]; + for (int i = 0; i < 6; i++) + command[i] = (char *)malloc(8); + + char *line = NULL; + size_t linesize = 0; + ssize_t linelen = 0; + while ((linelen = getline(&line, &linesize, fp)) != -1) + { + line[linelen-1] = '\0'; // cut off the newline + + for (int i = 0; i < 6; i++) + memset(command[i], 0, 8); + + splitString(line, ' ', command); + + int count = atoi(command[1]); + int fromStack = atoi(command[3])-1; + int toStack = atoi(command[5])-1; + + for (int i = 0; i < count; i++) + push(stacks[toStack], pop(stacks[fromStack])); + } + free(line); + if (ferror(fp)) + err(1, "getline"); + fclose(fp); + + printf("Crates on top of each stack: "); + for (int i = 0; i < 9; i++) + printf("%c", pop(stacks[i])); + printf("\n"); + + for (int i = 0; i < 6; i++) + free(command[i]); + for (int i = 0; i < 9; i++) + free(stacks[i]); + + exit(EXIT_SUCCESS); +}diff --git a/day5/part2.c b/day5/part2.c@@ -0,0 +1,116 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <err.h> + +struct stack { + char contents[64]; + int size; +}; + +char +pop(struct stack *stack) +{ + stack->size--; + return stack->contents[stack->size]; +} + +void +push(struct stack *stack, char item) +{ + stack->contents[stack->size++] = item; +} + +void +splitString(char *string, char delim, char **result) +{ + int index = 0; + for (int i = 0; i < strlen(string); i++) + { + if (string[i] == delim) + index++; + else + strncat(result[index], &string[i], 1); + } +} + +int +main (void) +{ + FILE *fp = fopen("input.txt", "r"); + if (fp == NULL) + { + err(1, "Failed to open input.txt"); + exit(EXIT_FAILURE); + } + + struct stack *stacks[9]; + for (int i = 0; i < 9; i++) + { + stacks[i] = (struct stack*)malloc(1*sizeof(struct stack)); + stacks[i]->size = 0; + } + + // This is way easier than trying to parse the drawing + for (int i = 0; i < strlen("DTRBJLWG"); i++) + push(stacks[0], "DTRBJLWG"[i]); + for (int i = 0; i < strlen("SWC"); i++) + push(stacks[1], "SWC"[i]); + for (int i = 0; i < strlen("RZTM"); i++) + push(stacks[2], "RZTM"[i]); + for (int i = 0; i < strlen("DTCHSPV"); i++) + push(stacks[3], "DTCHSPV"[i]); + for (int i = 0; i < strlen("GPTLDZ"); i++) + push(stacks[4], "GPTLDZ"[i]); + for (int i = 0; i < strlen("FBRZJQCD"); i++) + push(stacks[5], "FBRZJQCD"[i]); + for (int i = 0; i < strlen("SBDJMFTR"); i++) + push(stacks[6], "SBDJMFTR"[i]); + for (int i = 0; i < strlen("LHRBTVM"); i++) + push(stacks[7], "LHRBTVM"[i]); + for (int i = 0; i < strlen("QPDSV"); i++) + push(stacks[8], "QPDSV"[i]); + + char *command[6]; + for (int i = 0; i < 6; i++) + command[i] = (char *)malloc(8); + + char *line = NULL; + size_t linesize = 0; + ssize_t linelen = 0; + while ((linelen = getline(&line, &linesize, fp)) != -1) + { + line[linelen-1] = '\0'; // cut off the newline + + for (int i = 0; i < 6; i++) + memset(command[i], 0, 8); + + splitString(line, ' ', command); + + int count = atoi(command[1]); + int fromStack = atoi(command[3])-1; + int toStack = atoi(command[5])-1; + + for (int i = 0; i < count; i++) + push(stacks[toStack], stacks[fromStack]->contents[stacks[fromStack]->size-count+i]); + + for (int i = 0; i < count; i ++) + pop(stacks[fromStack]); + } + free(line); + if (ferror(fp)) + err(1, "getline"); + fclose(fp); + + printf("Crates on top of each stack: "); + for (int i = 0; i < 9; i++) + printf("%c", pop(stacks[i])); + printf("\n"); + + for (int i = 0; i < 6; i++) + free(command[i]); + for (int i = 0; i < 9; i++) + free(stacks[i]); + + exit(EXIT_SUCCESS); +}