advent-of-code

My (attempts at) solutions for Advent of Code
git clone https://git.jaderune.net/jbauer/advent-of-code
Log | Files | Refs | README | LICENSE

commit 0d3abfbdf3d334740a18e56d6f5ed49ec15e4872
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Tue, 11 Apr 2023 17:59:46 -0400

Merge AoC repos into one

Diffstat:
A2021/day1/main1.c | 46++++++++++++++++++++++++++++++++++++++++++++++
A2021/day1/main2.c | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2021/day2/main1.c | 49+++++++++++++++++++++++++++++++++++++++++++++++++
A2021/day2/main2.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
A2021/day3/main1.c | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2021/day3/main2.c | 133+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2021/day4/input.txt | 19+++++++++++++++++++
A2021/day4/main.c | 104+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2021/day5/input.txt | 10++++++++++
A2021/day5/main.c | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2021/day6/main.c | 157+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2021/day7/main.c | 134+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2021/day8/input.txt | 10++++++++++
A2021/day8/main.c | 115+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day1/input.txt | 2251+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day1/main.c | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day10/input.txt | 146+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day10/main.c | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day11/input.txt | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day11/main.c | 154+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day12/input.txt | 41+++++++++++++++++++++++++++++++++++++++++
A2022/day12/main.c | 188+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day13/input.txt | 449+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day13/main.clj | 3+++
A2022/day2/input.txt | 2500+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day2/main.c | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day3/input.txt | 300+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day3/main-bitmap.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day3/main.c | 140+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day4/input.txt | 1000+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day4/main.c | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day5/input.txt | 503+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day5/part1.c | 113+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day5/part2.c | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day6/input.txt | 1+
A2022/day6/main.c | 47+++++++++++++++++++++++++++++++++++++++++++++++
A2022/day7/input.txt | 1013+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day7/main.c | 161+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day8/input.txt | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day8/main.c | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day9/input.txt | 2000+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day9/main.c | 124+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ALICENSE | 15+++++++++++++++
AREADME.md | 5+++++
44 files changed, 13014 insertions(+), 0 deletions(-)

diff --git a/2021/day1/main1.c b/2021/day1/main1.c @@ -0,0 +1,46 @@ +#include <stdio.h> +#include <stdlib.h> + +FILE * +openInputFile(void) +{ + FILE *fp = fopen("input.txt","r"); + if (fp == NULL) + { + printf("Could not open file: input.txt\n"); + exit(EXIT_FAILURE); + } + return fp; +} + +int +main(void) +{ + char *line = NULL; + int increases = 0; + int currentValue = 0; + int previousValue = -1; + size_t len = 0; + ssize_t read; + + FILE *fp = openInputFile(); + + while ((read = getline(&line, &len, fp)) != -1) + { + if (previousValue < 0) + previousValue = atoi(line); + + currentValue = atoi(line); + + if (currentValue > previousValue) + increases++; + + previousValue = currentValue; + } + + fclose(fp); + if (line) + free(line); + printf("%d\n", increases); + exit(EXIT_SUCCESS); +} diff --git a/2021/day1/main2.c b/2021/day1/main2.c @@ -0,0 +1,68 @@ +#include <stdio.h> +#include <stdlib.h> + +#define ARR_SIZE 3 + +FILE * +openInputFile(void) +{ + FILE *fp = fopen("input.txt","r"); + if (fp == NULL) + { + printf("Could not open file: input.txt\n"); + exit(EXIT_FAILURE); + } + return fp; +} + +int +sumArray(int *array, int size) +{ + int sum = 0; + for (int i = 0; i < size; i++) + sum += array[i]; + return sum; +} + +int +main(void) +{ + FILE *fp = openInputFile(); + int increases = 0; + + int array[ARR_SIZE] = { 0 }; + int arrayPosition = 0; + int currentSum = 0; + int previousSum = 0; + int firstFill = 1; + + char *line = NULL; + size_t len = 0; + ssize_t read; + while ((read = getline(&line, &len, fp)) != -1) + { + if (arrayPosition == ARR_SIZE) { + if (firstFill) + previousSum = currentSum; + firstFill = 0; + arrayPosition = 0; + } + + currentSum = sumArray(array, ARR_SIZE); + array[arrayPosition++] = atoi(line); + + // Don't do comparisons until we've filled the array + if (firstFill) + continue; + + if (currentSum > previousSum) + increases++; + previousSum = currentSum; + } + + fclose(fp); + if (line) + free(line); + printf("%d\n", increases); + exit(EXIT_SUCCESS); +} diff --git a/2021/day2/main1.c b/2021/day2/main1.c @@ -0,0 +1,49 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +FILE * +openInputFile(void) +{ + FILE *fp = fopen("input.txt","r"); + if (fp == NULL) + { + printf("Could not open file: input.txt\n"); + exit(EXIT_FAILURE); + } + return fp; +} + +int +main(void) +{ + FILE *fp = openInputFile(); + + int position = 0; + int depth = 0; + + char *line = NULL; + size_t len = 0; + ssize_t read; + while ((read = getline(&line, &len, fp)) != -1) + { + char *direction = strtok(line, " "); + int magnitude = atoi(strtok(NULL, " ")); + + if (strcmp("forward", direction) == 0) + position += magnitude; + else if (strcmp ("down", direction) == 0) + depth += magnitude; + else + depth -= magnitude; + } + + printf("Final position: %d\n", position); + printf("Final depth: %d\n", depth); + printf("Product: %d\n", position * depth); + + fclose(fp); + if (line) + free(line); + exit(EXIT_SUCCESS); +} diff --git a/2021/day2/main2.c b/2021/day2/main2.c @@ -0,0 +1,53 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +FILE * +openInputFile(void) +{ + FILE *fp = fopen("input.txt","r"); + if (fp == NULL) + { + printf("Could not open file: input.txt\n"); + exit(EXIT_FAILURE); + } + return fp; +} + +int +main(void) +{ + FILE *fp = openInputFile(); + + int position = 0; + int depth = 0; + int aim = 0; + + char *line = NULL; + size_t len = 0; + ssize_t read; + while ((read = getline(&line, &len, fp)) != -1) + { + char *direction = strtok(line, " "); + int magnitude = atoi(strtok(NULL, " ")); + + if (strcmp("forward", direction) == 0) + { + position += magnitude; + depth += aim * magnitude; + } + else if (strcmp ("down", direction) == 0) + aim += magnitude; + else + aim -= magnitude; + } + + printf("Final position: %d\n", position); + printf("Final depth: %d\n", depth); + printf("Product: %d\n", position * depth); + + fclose(fp); + if (line) + free(line); + exit(EXIT_SUCCESS); +} diff --git a/2021/day3/main1.c b/2021/day3/main1.c @@ -0,0 +1,117 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define ENTRY_LENGTH 12 + +FILE * +openInputFile(void) +{ + FILE *fp = fopen("input.txt","r"); + if (fp == NULL) + { + printf("Could not open file: input.txt\n"); + exit(EXIT_FAILURE); + } + return fp; +} + +struct list { + size_t used; + size_t size; + int *contents; +}; + +int +createList(struct list *list, size_t initialSize) +{ + list->contents = (int *) malloc(initialSize * sizeof(int)); + if (list->contents == NULL) + return 1; + list->used = 0; + list->size = initialSize; + return 0; +} + +int +insertIntoList(struct list *list, int item) +{ + if (list->used == list->size) + { + list->size *= 2; + list->contents = (int *) realloc(list->contents, list->size * sizeof(int)); + if (list->contents == NULL) + return 1; + } + list->contents[list->used++] = item; + return 0; +} + +void +freeList(struct list *list) +{ + free(list->contents); +} + +// Just computes most common value from a set of 1 or 0 because nothing more is +// needed for this challenge +int +mostCommonValue(struct list *list) +{ + int numZeros = 0; + int numOnes = 0; + for (int i = 0; i < list->used; i++) + { + if (list->contents[i] == 0) + numZeros++; + else + numOnes++; + } + if (numZeros > numOnes) + return 0; + else + return 1; +} + +int +main(void) +{ + FILE *fp = openInputFile(); + + struct list bits[ENTRY_LENGTH]; + for (int i = 0; i < ENTRY_LENGTH; i ++) + if (createList(&bits[i], 1) != 0) + exit(EXIT_FAILURE); + + char *line = NULL; + size_t len = 0; + ssize_t read; + while ((read = getline(&line, &len, fp)) != -1) + { + for (int i = 0; i < ENTRY_LENGTH; i ++) + insertIntoList(&bits[i], line[i] - '0'); + } + + int gamma = 0; + + for (int i = 0; i < ENTRY_LENGTH; i++) + { + printf("%d: %d\n", i, mostCommonValue(&bits[i])); + if (mostCommonValue(&bits[i])) + gamma = gamma | (1 << (ENTRY_LENGTH - i - 1)) ; + printf("Gamma is: %x\n", gamma); + } + + int epsilon = ~gamma & ((1 << ENTRY_LENGTH) - 1); + + printf(" Gamma %d\n", gamma); + printf("Epsilon %d\n", epsilon); + printf("Product %d\n", gamma * epsilon); + + fclose(fp); + if (line) + free(line); + for (int i = 0; i < ENTRY_LENGTH; i++) + freeList(&bits[i]); + exit(EXIT_SUCCESS); +} diff --git a/2021/day3/main2.c b/2021/day3/main2.c @@ -0,0 +1,133 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define ENTRY_LENGTH 12 + +FILE * +openInputFile(void) +{ + FILE *fp = fopen("input.txt","r"); + if (fp == NULL) + { + printf("Could not open file: input.txt\n"); + exit(EXIT_FAILURE); + } + return fp; +} + +struct list { + size_t used; + size_t size; + int *contents; +}; + +int +createList(struct list *list, size_t initialSize) +{ + list->contents = (int *) malloc(initialSize * sizeof(int)); + if (list->contents == NULL) + return 1; + list->used = 0; + list->size = initialSize; + return 0; +} + +int +insertIntoList(struct list *list, int item) +{ + if (list->used == list->size) + { + list->size *= 2; + list->contents = (int *) realloc(list->contents, list->size * sizeof(int)); + if (list->contents == NULL) + return 1; + } + list->contents[list->used++] = item; + return 0; +} + +void +freeList(struct list *list) +{ + free(list->contents); +} + +//TODO Make a function that handles deleting an element from the list by index + +//TODO Make a function that duplicates a list + +// Just computes most common value from a set of 1 or 0 because nothing more is +// needed for this challenge +int +mostCommonValue(struct list *list) +{ + int numZeros = 0; + int numOnes = 0; + for (int i = 0; i < list->used; i++) + { + if (list->contents[i] == 0) + numZeros++; + else + numOnes++; + } + if (numZeros > numOnes) + return 0; + else + return 1; +} + +int +main(void) +{ + FILE *fp = openInputFile(); + + struct list bits[ENTRY_LENGTH]; + for (int i = 0; i < ENTRY_LENGTH; i ++) + if (createList(&bits[i], 1) != 0) + exit(EXIT_FAILURE); + + // TODO For Part 2: + // Add each _line_ into a list as a list of strings (not ints) + // (Duplicate the list for the second calculation) + // Iterate over each element, picking the nth bit and add that to a list + // of ints + // Compute the most common number (1 or 0) + // Iterate over each element again and remove the entries which don't + // start with the most common number + // Repeat until one number remains -> this is the result value + // Do the same for the second calculation, but take the least common + // number instead of the most common + + char *line = NULL; + size_t len = 0; + ssize_t read; + while ((read = getline(&line, &len, fp)) != -1) + { + for (int i = 0; i < ENTRY_LENGTH; i ++) + insertIntoList(&bits[i], line[i] - '0'); + } + + int gamma = 0; + + for (int i = 0; i < ENTRY_LENGTH; i++) + { + printf("%d: %d\n", i, mostCommonValue(&bits[i])); + if (mostCommonValue(&bits[i])) + gamma = gamma | (1 << (ENTRY_LENGTH - i - 1)) ; + printf("Gamma is: %x\n", gamma); + } + + int epsilon = ~gamma & ((1 << ENTRY_LENGTH) - 1); + + printf(" Gamma %d\n", gamma); + printf("Epsilon %d\n", epsilon); + printf("Product %d\n", gamma * epsilon); + + fclose(fp); + if (line) + free(line); + for (int i = 0; i < ENTRY_LENGTH; i++) + freeList(&bits[i]); + exit(EXIT_SUCCESS); +} diff --git a/2021/day4/input.txt b/2021/day4/input.txt @@ -0,0 +1,19 @@ +7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1 + +22 13 17 11 0 + 8 2 23 4 24 +21 9 14 16 7 + 6 10 3 18 5 + 1 12 20 15 19 + + 3 15 0 2 22 + 9 18 13 17 5 +19 8 7 25 23 +20 11 10 24 4 +14 21 16 12 6 + +14 21 17 24 4 +10 16 15 9 19 +18 8 23 26 20 +22 11 13 6 5 + 2 0 12 3 7 diff --git a/2021/day4/main.c b/2021/day4/main.c @@ -0,0 +1,104 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +struct bingocard +{ + char *card[5][5]; + int hits[5][5]; + char *name; +}; + +static int +getline(char* line, FILE* fp, int length) +{ + for (int i = 0; i < length; i++) + { + line[i] = fgetc(fp); + if (line[i] == EOF) + return -1; + } + line[length-1] = '\0'; + + return 0; +} + +static int +getlinelength(FILE *fp) +{ + int linelength = 0; + long offset = ftell(fp); + for (;;) + { + if (fgetc(fp) == '\n') + { + linelength += 1; + break; + } + else + { + linelength += 1; + } + } + fseek(fp, offset, SEEK_SET); // seek back to beginning of line + return linelength; +} + +int +main(void) +{ + printf("Opening input.txt\n"); + FILE *fp = fopen("input.txt","r"); + if (fp == NULL) + { + printf("Could not open file: input.txt\n"); + exit(EXIT_FAILURE); + } + + // First line of file is all the numbers drawn + char *drawsline = (char *) malloc(getlinelength(fp)+1); + getline(drawsline, fp, getlinelength(fp)+1); + + // Figure out how many numbers are drawn in total + int numelements = 0; + for (size_t i = 0; i < strlen(drawsline); i ++) + if (strncmp(&drawsline[i], ",", 1) || drawsline[i] == '\0') + numelements += 1; + printf("There are %d numbers drawn\n", numelements); + + // Create an array from that string + char **draws = (char **) malloc(numelements * sizeof(char)); + if (draws == NULL) + { + printf("Unable to allocate memory for drawn values.\n"); + exit(EXIT_FAILURE); + } + + // THIS BREAKS THINGS ON WINDOWS FOR SOME REASON??? + // NOT INFINITE LOOP, PROGRAM EXITS BUT DOESN'T PRINT + // ANYMORE + // ... Maybe I should write my own strtok + char *token = strtok(drawsline, ","); + int index; + while (token != NULL) + { + draws[index++] = token; + printf("Consumed: %s\n", token); + token = strtok(NULL, ","); + } + + printf("["); + for (int i = 0; i < numelements; i++) + printf("%s,", draws[i]); + printf("]\n"); + + // Now parse bingo cards + + fclose(fp); + if (draws) + { + free(draws); + } + + exit(EXIT_SUCCESS); +} diff --git a/2021/day5/input.txt b/2021/day5/input.txt @@ -0,0 +1,10 @@ +0,9 -> 5,9 +8,0 -> 0,8 +9,4 -> 3,4 +2,2 -> 2,1 +7,0 -> 7,4 +6,4 -> 2,0 +0,9 -> 2,9 +3,4 -> 1,4 +0,0 -> 8,8 +5,5 -> 8,2 diff --git a/2021/day5/main.c b/2021/day5/main.c @@ -0,0 +1,101 @@ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define ENTRY_LENGTH 10 + +int seafloor[10][10] = { + {0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0}, +}; + +static int +getline(char* line, FILE* fp, int length) +{ + for (int i = 0; i < length; i++) + { + line[i] = fgetc(fp); + if (line[i] == EOF) + return -1; + } + line[length-1] = '\0'; + + return 0; +} + +int +main(void) +{ + FILE *fp = fopen("input.txt","r"); + if (fp == NULL) + { + printf("Could not open file: input.txt\n"); + exit(EXIT_FAILURE); + } + + char *line = (char *)malloc(ENTRY_LENGTH + 2); + while ((getline(line, fp, ENTRY_LENGTH + 1)) != -1) + { + // printf("Got line: %s", line); + int x1 = line[0] - '0'; + int y1 = line[2] - '0'; + int x2 = line[7] - '0'; + int y2 = line[9] - '0'; + + + if (x1 == x2) + { + printf("Coordinates: (%d,%d) -> (%d,%d)\n", x1, y1, x2, y2); + if (y1 < y2) + for (int i = 0; i <= y2-y1; i++) + seafloor[y1+i][x1] += 1; + else + for (int i = 0; i <= y1-y2; i++) + seafloor[y1-i][x1] += 1; + } + else if (y1 == y2) + { + printf("Coordinates: (%d,%d) -> (%d,%d)\n", x1, y1, x2, y2); + if (x1 < x2) + for (int i = 0; i <= x2-x1; i++) + seafloor[y1][x1+i] += 1; + else + for (int i = 0; i <= x1-x2; i++) + seafloor[y1][x1-i] += 1; + } + else + { + continue; + } + + printf (" 0 1 2 3 4 5 6 7 8 9\n"); + for (int i = 0; i < 10; i++) + { + printf(" %d ", i); + for (int j = 0; j < 10; j++) + printf("%d ", seafloor[i][j]); + printf("\n"); + } + } + fclose(fp); + if (line) + free(line); + + int dangerzones = 0; + for (int i = 0; i < 10; i++) + for (int j = 0; j < 10; j++) + if (seafloor[i][j] > 1) + dangerzones += 1; + + printf("There are %d dangerous zones!\n", dangerzones); + exit(EXIT_SUCCESS); +} diff --git a/2021/day6/main.c b/2021/day6/main.c @@ -0,0 +1,157 @@ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <locale.h> + +// List contains elements of type char which cuts memory utilization down to a +// quarter compared to using ints. Max value we need to track is 8, which fits +// handily into a char and doesn't require 4 Bytes. +// (Could pack values into a nibble, or even pack eight values into three bytes +// but that seems a bit extreme and complicated); +struct list { + size_t used; + size_t size; + char *contents; +}; + +int +createList(struct list *list) +{ + list->used = 0; + list->size = 1; + list->contents = (char *) malloc(1 * sizeof(char)); + if (list->contents == NULL) + return 1; + return 0; +} + +int +insertIntoList(struct list *list, char item) +{ + // Grow list (double its size) when full + if (list->used == list->size) + { + list->size *= 2; + list->contents = (char *) realloc(list->contents, list->size * sizeof(char)); + + // Print new mem usage + float mem_used = (float) list->size * sizeof(char); + if (mem_used / 1073741824.0 > 1 ) + printf("Size of list: %.2f GiB.\n", mem_used / 1073741824.0); + else if (mem_used / 1048576.0 > 1) + printf("Size of list: %.2f MiB.\n", mem_used / 1048576.0); + else if (mem_used / 1024.0 > 1) + printf("Size of list: %.2f KiB.\n", mem_used / 1024.0); + else + printf("Size of list: %.2f Bytes.\n", mem_used); + + if (list->contents == NULL) + return 1; + } + list->contents[list->used++] = item; + return 0; +} + +void +freeList(struct list *list) +{ + free(list->contents); +} + +int +main(int argc, char **argv) +{ + setlocale(LC_NUMERIC, ""); + int quiet = 0; + + if (argc < 2) + { + printf("Error. At least one argument expected: num_days.\n"); + exit(EXIT_FAILURE); + } + + // Convert num_days to integer + char *end; + int num_days = strtol(argv[1], &end, 10); + + if (num_days < 0) + { + printf("Error. num_days must be greater than zero.\n"); + exit(EXIT_FAILURE); + } + + // Print state each iteration? + if (argc >= 3 && strncmp(argv[2], "-q", 2) == 0) + quiet = 1; + + struct list lanternfish; + if (createList(&lanternfish)) + { + printf("Failed to allocate memory for list.\n"); + exit(EXIT_FAILURE); + } + printf("Initial Lanternfish Spawned\n"); + + // Initial population + insertIntoList(&lanternfish, 3); + insertIntoList(&lanternfish, 4); + insertIntoList(&lanternfish, 3); + insertIntoList(&lanternfish, 1); + insertIntoList(&lanternfish, 2); + + // Print out initial state + if (quiet) + goto simulation; + printf("Initial state: ["); + for (int i = 0; i < lanternfish.used; i++) + { + if (i == lanternfish.used-1) + printf("%d", lanternfish.contents[i]); + else + printf("%d,", lanternfish.contents[i]); + } + printf("]\n"); + + simulation: + for (int i = 1; i <= num_days; i++) + { + int newfish = 0; + // Iterate over lanternfish, subtracting days until next spawn + // or tracking new spawns and resetting days + for (int i = 0; i < lanternfish.used; i++) + { + if (lanternfish.contents[i] == 0) + { + lanternfish.contents[i] = 6; + newfish += 1; + } + else + { + lanternfish.contents[i] -= 1; + } + } + + // Add any new fish that have just spawned + for (int i = 0; i < newfish; i++) + insertIntoList(&lanternfish, 8); + + if (quiet) + continue; + // Print current state of lanternfish + printf("After %2d days: [", i); + for (int i = 0; i < lanternfish.used; i++) + { + if (i == lanternfish.used-1) + printf("%d", lanternfish.contents[i]); + else + printf("%d,", lanternfish.contents[i]); + } + printf("]\n"); + } + + printf("[FINISHED SIMULATION] After %d days, there are %d lanternfish.\n", num_days, lanternfish.used); + printf("Final list utilization: %.2f\n", ((float)lanternfish.used / (float)lanternfish.size) * 100.0); + + exit(EXIT_SUCCESS); +} diff --git a/2021/day7/main.c b/2021/day7/main.c @@ -0,0 +1,134 @@ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +struct list { + size_t used; + size_t size; + char *contents; +}; + +int +createList(struct list *list) +{ + list->used = 0; + list->size = 1; + list->contents = (char *) malloc(1 * sizeof(char)); + if (list->contents == NULL) + return 1; + return 0; +} + +int +insertIntoList(struct list *list, char item) +{ + // Grow list (double its size) when full + if (list->used == list->size) + { + list->size *= 2; + list->contents = (char *) realloc(list->contents, list->size * sizeof(char)); + if (list->contents == NULL) + return 1; + } + list->contents[list->used++] = item; + return 0; +} + +int +isInList (struct list *list, int number) +{ + for (int i = 0; i < list->used; i++) + if (list->contents[i] == number) + return 1; + return 0; +} + +void +printList(struct list *list) +{ + printf("["); + for (int i = 0; i < list->used; i++) + { + if (i == list->used-1) + printf("%d", list->contents[i]); + else + printf("%d, ", list->contents[i]); + } + printf("]\n"); +} + +int +abs (int number) +{ + if (number < 0) + return number * -1; + return number; +} + +void +freeList(struct list *list) +{ + free(list->contents); +} + +int +main(int argc, char **argv) +{ + struct list positions; + if (createList(&positions)) + { + printf("Failed to allocate memory for list.\n"); + exit(EXIT_FAILURE); + } + + // Initial list of positions + // Would be best read from a file and parsed with strtok but that seems + // to be broken with MinGW? (at least a program stops printing when + // using strtok) + insertIntoList(&positions, 16); + insertIntoList(&positions, 1); + insertIntoList(&positions, 2); + insertIntoList(&positions, 0); + insertIntoList(&positions, 4); + insertIntoList(&positions, 2); + insertIntoList(&positions, 7); + insertIntoList(&positions, 1); + insertIntoList(&positions, 2); + insertIntoList(&positions, 14); + + printf("Initial positions loaded.\n"); + printList(&positions); + + // An optimal position will always have at least one of the initial + // positions (because then at least one has fuel use = 0) + struct list possiblePositions; + if (createList(&possiblePositions)) + { + printf("Failed to allocate memory for list.\n"); + exit(EXIT_FAILURE); + } + for (int i = 0; i < positions.used; i++) + if (! isInList(&possiblePositions, positions.contents[i])) + insertIntoList(&possiblePositions, positions.contents[i]); + printf("Determined possible optimal positions.\n"); + printList(&possiblePositions); + + // Determine the position that will have the lowest fuel usage + int optimalPosition = -1; + int lowestFuelUse = 99999; // Not max int but good enough for this + for (int i = 0; i < possiblePositions.used; i++) + { + int fuelCount = 0; + for (int j = 0; j < positions.used; j++) + fuelCount += abs(possiblePositions.contents[i] - positions.contents[j]); + if (fuelCount < lowestFuelUse) + { + lowestFuelUse = fuelCount; + optimalPosition = i; + } + } + + printf("The optimal position is %d, using %d fuel.\n", optimalPosition, lowestFuelUse); + exit(EXIT_SUCCESS); +} diff --git a/2021/day8/input.txt b/2021/day8/input.txt @@ -0,0 +1,10 @@ +be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe +edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc +fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg +fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb +aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea +fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb +dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe +bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef +egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb +gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce diff --git a/2021/day8/main.c b/2021/day8/main.c @@ -0,0 +1,115 @@ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +// half done +// Need to only count number of segments in output (after | key) +// Should only split on string after | key, throw out inputs for part 1 + +void +printArrayOfStrings(char **array, int length) +{ + printf("["); + for (int i = 0; i < length; i++) + { + printf("%s", array[i]); + if (i + 1 != length) + printf(", "); + } + printf("]\n"); +} + +int +getNumTokens(char* string, char delimiter) +{ + int numTokens = 1; // Start at one because there will always be at least one element + for (int i = 0; i < strlen(string); i++) + if (string[i] == delimiter) + numTokens += 1; + return numTokens; +} + +void +splitString(char *string, char **array, char delimiter) +{ + int index = 0; + int charCount = 0; + for (int i = 0; i < strlen(string); i++) + { + if (string[i] == '\n') + { + continue; + } + else if (string[i] == delimiter) + { + index++; + i++; + array[index][charCount] = '\0'; + charCount = 0; + } + array[index][charCount] = string[i]; + charCount++; + } + return; +} + +int +main(int argc, char **argv) +{ + size_t size = 512; + int digitcount[] = {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0}; // Indices correspond to digits 0-9 + + FILE *fp = fopen("input.txt","r"); + if (fp == NULL) + { + printf("Could not open file: input.txt\n"); + exit(EXIT_FAILURE); + } + + char *line = (char *)malloc(size); + while (getline(&line, &size, fp) != -1) + { + printf("Current line: %s", line); + int numTokens = getNumTokens(line, ' '); + printf("NumTokens = %d\n", numTokens); + char **entry = (char **)malloc(numTokens*sizeof(char *)); + for (int i = 0; i < numTokens; i++) + entry[i] = (char *)malloc(512*sizeof(char)); + splitString(line, entry, ' '); + printf("Current entry: "); + printArrayOfStrings(entry, 14); + + for (int i = 0; i < 14; i++) + { + printf("String %s - %ld chars\n", entry[i], strlen(entry[i])); + switch (strlen(entry[i])) + { + case 2: + digitcount[1]++; + break; + case 3: + digitcount[7]++; + break; + case 4: + digitcount[4]++; + break; + case 7: + digitcount[8]++; + break; + } + } + } + + printf("Number frequency:\n"); + for (int i = 0; i < 10; i++) + printf("%d: %d\n", i, digitcount[i]); + + printf("Total number of digits which use a unique number of segments: %d\n", + digitcount[1] + digitcount[7] + digitcount[4] + digitcount[8]); + + if (line) + free(line); + + exit(EXIT_SUCCESS); +} diff --git a/2022/day1/input.txt b/2022/day1/input.txt @@ -0,0 +1,2251 @@ +18313 +2404 +10479 + +7011 +10279 +1496 +10342 +8918 +3162 +4525 +4368 + +17242 + +10920 +14072 +9754 +4435 +9396 + +5915 +2602 +4032 +3303 +2685 +1856 +1334 +4865 +6385 +1733 +5328 + +8899 +5482 +3195 +7837 +8986 +13794 + +25121 +22211 +21257 + +6360 +4007 +5124 +2266 +6943 +6966 +3887 +3427 +1255 +5266 +6119 +2841 + +6167 +3883 +2776 +5894 +2013 +6930 +6613 +4637 +5259 +2347 +3550 +5639 +5117 + +4530 +5961 +5956 +2503 +5060 +4770 +5240 +4123 +4089 +6794 +3333 +5244 +4415 + +1733 +1209 +4458 +1223 +3859 +7728 +9793 +8350 + +2278 +3241 +4668 +1836 +3669 +2996 +1772 +5016 +6294 +6367 +1080 +5964 +5547 + +4793 +13744 +9478 +7979 +1490 +12490 + +6770 +7282 +2059 +5242 +13237 +9365 + +2230 +6498 +1352 +3846 +3863 +6205 +5803 +2646 +4188 +4431 +6380 +1526 +2620 +4438 + +24331 +23578 + +4199 +5126 +5725 +1123 +2415 +5427 +5789 +4856 +2610 +4833 +3398 +4239 +1432 +5122 + +5013 +4939 +11760 +8419 +5706 +3765 +10182 + +11795 +9688 +1712 +2091 +9651 +13022 + +10393 +11862 +1672 +5431 +6812 +1866 +5599 + +2036 +6561 +4097 +3467 +4739 +7656 +3252 +4322 +5068 +2345 +4155 + +3090 +1228 +1270 +8407 +8118 +4996 +7157 +3944 +6230 +2728 + +5669 +2113 +3076 +1290 +7673 +1022 +5805 +5128 +7912 +7862 +4955 + +20640 + +4131 +4874 +5220 +9947 +9739 +1662 +9418 +9684 + +4875 +3073 +1151 +11911 +10310 +10793 +10307 + +6979 +4265 +8322 +3518 +1855 +4912 +5481 +7124 +4664 + +19975 +16112 +19450 +7231 + +3909 +4743 +3794 +4448 +3200 +3447 +3331 +7011 +3345 +3935 +6750 + +3900 +14850 +4890 +9625 +4832 + +23682 +1375 + +7463 +8112 +10624 +1105 +6625 +5157 +5839 +1019 + +19089 +36719 + +5372 +7085 +8536 +8717 +6255 +5961 +3198 +7729 +3055 + +4406 +2263 +6686 +1458 +2377 +2740 +1284 +3045 +7121 +3070 + +19161 +10515 +1027 + +7782 +1703 +1864 +3611 +1039 +2394 +3472 +6964 +5824 +8011 +3341 + +56690 + +5107 +1876 +4973 +5485 +4325 +6079 +3187 +3565 +2875 +7137 +3887 +6763 + +2633 +2151 +1105 +2400 +5338 +3382 +1330 +3945 +6556 +1670 +5526 +4281 +2168 + +5382 +2347 +4225 +1905 +4672 +6706 +5747 +5808 +1159 +4878 +5923 + +11148 +8722 +3896 +15697 +3499 + +46749 + +13315 +20966 + +1625 +6338 +7882 +1505 +4226 +9948 +6438 +6012 + +1003 +10033 +5066 +7756 +10151 +10719 +5299 +5629 + +4466 +6290 +7220 +3657 +8261 +7348 +8525 +4531 +4892 +7833 + +24723 +7817 + +4234 +4428 +5810 +2091 +4301 +1378 +2342 +3872 +1167 +2073 +6867 +7026 + +7005 +10018 +3954 +4497 +4951 +1283 +5383 + +4152 +4390 +8731 +8521 +5415 +1029 +8200 +3769 + +7224 +12281 +8337 +8969 +13938 +9651 + +10923 +14358 +8896 +8401 +7073 + +3934 +4424 +4538 +4371 +2335 +6363 +1264 +4331 +5107 +2270 +3109 +2547 +2304 +5911 + +4600 +5674 +2620 +1846 +3804 +2387 +4094 +3648 +4241 +5593 +2974 +2101 +1621 +3939 + +7429 +9425 +9653 +5835 + +13414 +4517 +15783 +3867 + +2287 +1019 +11604 +11910 +2591 +2468 +10543 + +13050 +1396 +13959 +6883 + +6303 +2889 +4047 +3954 +2245 +6672 +4574 +5731 +5879 +2837 +5859 +4812 +6785 + +10290 +10131 +5605 +3321 +2122 +6331 +9107 +7937 + +1786 +1362 +7409 +2093 +3901 +1306 +2089 +1558 +4163 +6118 +6270 +1929 + +4224 +3864 +3922 +3609 +2496 +2246 +5397 +1535 +5499 +4932 +3684 +1348 +3437 +3665 + +2973 +4506 +3971 +2511 +4563 +3168 +2687 +6249 +2593 +6470 +4100 +6309 +1316 +4362 + +14966 +2940 +11485 + +6675 +16873 +5695 +14706 + +3953 + +5620 +1678 +3630 +5015 +3277 +4957 +5288 +5555 +2860 +1750 +2795 +6170 +6308 + +3671 +1354 +4018 +4730 +4070 +1685 +4478 +1918 +5532 +3970 +3751 +4890 +4277 +2737 +5535 + +14799 +8129 + +5254 +2065 +1282 +2407 +5174 +4549 +4326 +1201 +3372 +3906 +6234 +4937 +4234 +2149 + +5194 +2519 +3555 +6929 +4367 +4547 + +1024 +37240 + +3693 +6526 +2679 +5434 +7924 +2974 +1754 +4287 +4744 +4187 +1348 + +6598 +5963 +6035 +5404 +3270 +1250 +5947 +3037 +3927 +3702 +5255 +1640 +6476 + +4499 +5207 +1991 +4613 +3088 +5024 +4737 +5160 +2450 +3582 +2605 +2573 +1100 +3732 + +1633 +5663 +2415 +4039 +4957 +2697 +5428 +5050 +3979 +3428 +4563 +3770 +3635 +4012 +4773 + +7377 +6189 +10812 +6905 +11196 +5394 +10135 + +10008 +2868 +2630 +8542 +8897 +2718 +5831 +4460 + +1303 +4771 +1574 +2960 +5070 +1596 +5530 +4177 +4210 +6465 +4179 +1000 +4460 +6173 + +6654 +1378 +2148 +8704 +6875 +4511 +7540 +6078 +5585 +4030 + +4333 +5953 +5095 +4782 +3651 +4673 +3267 +6389 +2358 +2120 +3684 +3034 +3420 +1322 + +8922 +9535 +16357 +16552 + +1378 +1039 +1262 +2067 +6491 +2776 +1352 +4914 +5832 +1369 +6105 +1384 +2474 +1704 + +6256 +2538 +4701 +4090 +4077 +5333 +2777 +4082 +4640 +2542 +6356 +3065 +4855 +2535 + +6510 +3709 +4197 +4344 +5906 +6852 +7398 +3578 +3308 +1239 +3817 +5676 + +5441 +3368 +1752 +2629 +1923 +4711 +1667 +3701 +1350 +4764 +6439 +2341 +4052 + +6407 +5327 +1229 +4834 +6008 +1302 +5595 +5260 +3680 +3220 +3154 +3353 +1942 +1740 + +2356 +1908 +3058 +4668 +3641 +1663 +4199 +2190 +6067 +3923 +3288 +1978 +6110 +5330 + +7020 +2487 +6521 +1084 +6582 +1784 +7861 +2759 +8077 +2961 + +68314 + +2160 +15170 +2267 +3867 +6851 + +8543 +4118 +22260 + +5313 +3525 +1483 +3613 +5798 +1472 +4020 +5607 +4391 +2502 +5512 +5535 +3755 +5590 +3668 + +25502 +7623 +15508 + +2138 +1227 +11355 +1288 +7062 +5391 +3994 + +7738 +3667 +5000 +1691 +7322 +8054 +4540 +2353 +6870 +8475 + +4289 +6415 +1251 +4933 +7224 +2608 +2320 +4432 +4637 +1395 +6575 +5581 + +6841 +1161 +5617 +12505 +6274 +9171 + +13548 +20154 +20682 + +2843 +7363 +5967 +15256 +16032 + +7940 +6992 +3940 +3100 +1334 +1389 +5182 +6666 +4030 +6116 +3036 + +35234 +19273 + +7268 +10689 + +1846 +4211 +1504 +3780 +3789 +2017 +6279 +5001 +2341 +6350 +5169 +1966 +2640 +5574 + +2125 +8432 +8339 +8130 +3506 +6115 +4684 +9548 +3300 + +6848 +1274 +6463 +12007 +10719 +10590 +5616 + +4471 +1304 +1635 +5960 +3702 +1747 +3658 +2446 +4724 +5675 +1739 +3170 +3556 + +9134 +12963 +6987 + +5728 +7877 +6499 +5909 +1119 +3090 +5033 +4884 +3151 + +10675 +8053 +9237 +14206 +11710 + +49154 + +5999 +3861 +1836 +3451 +3673 +5193 +2680 +6068 +1743 +5600 +4793 +6211 +2967 +1021 + +24720 + +8749 +11674 +4039 +5341 +14256 + +4553 +3893 +1528 +5490 +2417 +2392 +4023 +2430 +4649 +3163 +3673 +1378 +1957 +1055 + +10777 +4742 +16291 +16430 +12295 + +6502 +6052 +2944 +8166 +7567 +3580 +5957 +4691 +1623 +3269 + +1013 +5809 +16958 +4994 + +36633 + +8535 +13662 +24448 + +6651 +7911 + +19793 +2436 + +4671 +4420 +5322 +1215 +4952 +1193 +2387 +2799 +2748 +1343 +4123 +1715 +2978 +2148 +6050 + +2036 +4670 +5444 +4584 +4220 +6061 +2834 +2319 +1370 +4572 +4249 +4144 +3236 +3812 +3487 + +55521 + +3845 +1473 +6794 +4597 +5566 +2735 +2660 +6402 +2515 +3439 + +3772 +12809 +5790 +10817 +9729 + +1788 +2476 +4333 +2940 +7232 +3582 +8604 +3742 +7644 +4823 + +6128 +6737 +5712 +4168 +4465 +2183 +1217 +2257 +6326 +5966 +5699 +2985 +2290 + +5190 +4155 +10754 +9830 +4507 +2165 +7208 +4409 + +4256 +4158 +1941 +3124 +5852 +3376 +6411 +5508 +5072 +2374 +4189 +4032 +4957 +2050 + +2737 +6772 +4468 +2072 +1214 +6231 +7077 +6100 +6990 +5676 + +7474 +4524 +8778 +7129 +8151 +7975 +7277 +8672 +8097 +3847 + +29172 +7125 + +50466 + +14169 +22037 + +4483 +1801 +4863 +1920 +2776 +1407 +3998 +1142 +2664 +3920 +5708 +5481 +5701 +5159 +3654 + +1960 +4419 +5933 +4941 +4511 +1120 +5179 +3447 +5285 +3675 +1333 +1418 +4595 +4140 +2353 + +1995 +5588 +1443 +5681 +3439 +6178 +5795 +5144 +2788 +6232 +2620 +4665 +5624 + +37076 +7362 + +3885 +8268 +10947 +9084 +3016 +10383 +9594 + +16223 +16281 +5352 +11764 +10192 + +5248 +3151 +7341 +8125 +10671 +13510 + +1759 +7635 +7021 +3620 +7640 +4967 +3662 +3840 +7613 +3948 +1820 + +4010 +3936 +7375 +8060 +12396 +8236 + +5033 +2739 +4296 +5564 +6502 +3501 +5637 +5826 +5862 +5579 +6416 +2277 +2471 + +4325 +2621 +1600 +4290 +15494 + +6834 +15459 +10418 +12560 +6382 + +8598 +1089 +11386 +9538 +6933 +3943 + +14268 +7172 +7680 +15936 + +5695 + +69893 + +7293 +4968 +7862 +3210 +1924 +4742 +3519 +1095 +5922 +6335 +2405 + +4233 +3819 +7282 +10018 +3820 +5380 +9377 +8889 + +13183 +9964 +4473 +12469 +9862 +6567 + +4903 +1198 +5238 +1582 +1820 +6080 +10178 + +4554 +6315 +2818 +5956 +2491 +5291 +5056 +3699 +2843 +1243 +5430 +2175 + +31126 + +5392 +1590 +3738 +2507 +2817 +6963 +6140 +4606 +2814 +7044 +1282 + +12230 +1430 +4283 +11385 +2557 +6868 + +3871 +2558 +1957 +4058 +1935 +5738 +1513 +5744 +4130 +3321 +3760 +4155 +1063 +3694 + +67474 + +2215 +8962 +5442 +7390 +6223 +3648 +2390 +5464 + +5125 +3842 +2131 +3414 +6085 +3353 +3660 +1593 +4395 +2664 +4987 +2352 +5693 +1391 +1661 + +6017 +5013 +5243 +3734 +3116 +1541 +6456 +2806 +6143 +1344 +5977 +5422 +2326 +2164 + +33377 + +5950 +5168 +1348 +3144 +5760 +6974 +8537 +6664 +1248 + +9040 +4510 +5642 +8547 +9407 +5058 +6158 +7726 +6731 + +4182 +5246 +2686 +4196 +3809 +3253 +3260 +3162 +2703 +6373 +6136 +2904 +6356 +5038 + +3368 +11040 +5793 +13747 +10478 +5166 + +11697 +10072 +8434 +5732 + +5244 +6374 +1877 +2902 +4263 +4835 +2283 +2086 +6044 +6343 +6232 +2849 +3694 +6392 + +13062 +12405 +12869 +16366 +1652 + +8036 +1720 +7759 +13661 +1370 +3505 + +5885 +1708 +10440 +7438 +11480 +4904 +8387 + +4788 +10326 +6143 +11063 +9649 +9384 + +5849 +1935 +7411 +4694 +4435 +1321 +5301 +2096 +2422 +5484 +6560 +3070 + +5008 +5405 +9444 +6355 +13150 +12686 + +1618 +5831 +1957 +1288 +1382 +2946 +1926 +1828 +1646 +4823 +3637 +1309 +5727 +4450 + +66234 + +4016 +4075 +4551 +5869 +2866 +2461 +1055 +4712 +7220 +1973 +4650 + +2387 +1102 +4558 +2290 +1969 +3624 +2383 +4059 +4877 +3439 +6891 +3819 +5824 + +3440 +11447 +5522 +16700 + +8373 +1356 +7249 +8484 +6767 +7990 +8778 +2235 +5995 +8511 + +5466 +6088 +2583 +5235 +6093 +3089 +1570 +6112 +3668 +2187 +5582 +1380 +4800 +5529 +2987 + +12323 +4775 +11105 +3805 +7671 +6737 + +3197 +1256 +4539 +6383 +1476 +2134 +6785 +7337 +1229 +4965 +5055 + +13128 +8149 +4426 +8087 +2873 + +1695 +8214 +7147 +7216 +1070 +2801 +5033 +8103 +7750 +5749 + +4082 +1630 +1958 +6113 +4486 +2111 +1486 +6451 +5630 +5682 +4558 + +10475 +30751 + +5397 +5067 +1178 +2106 +5557 +4847 +1661 +3469 +4908 +2590 +4686 +2118 +4495 +2994 + +12590 +12902 +8742 +15433 +2777 + +7451 +8722 +7302 +2269 +9592 +6162 + +1907 +6741 +4442 +6526 +8289 +7479 +9481 +1642 +4982 + +2001 +12803 +7255 +13545 +10713 + +14621 +25826 +20771 + +1055 +8331 +7422 +8400 +2208 +6688 +8425 +6883 +4192 +4725 + +9970 +10127 +2777 +3933 +9274 +3791 +9906 + +2779 +1118 +2823 +1563 +9917 +6065 +1863 +1885 + +5053 +1922 +5356 +8266 +7335 +4260 +8730 +7307 +3140 + +4340 +1080 +2120 +5498 +6769 +6170 +2944 +4677 +1155 +4676 +2420 +4945 +5941 + +14838 +3844 +19712 +16283 + +1906 +3672 +4274 +6550 +6072 +5299 +4493 +3348 +2804 +3747 +3208 +5359 + +59156 + +6128 +5959 +12262 +12166 +4945 +9635 + +8095 +1757 +13656 +7215 +2545 +6298 + +7873 +6515 +6407 +4182 +4911 +3966 +1942 +7281 +5082 +7446 +5545 + +3876 +5618 +4226 +4445 +3915 +1250 +5469 +3872 +5157 +5539 +2894 +1357 +1419 +4214 +3009 + +3364 +6618 +1432 +7583 +6192 +2529 +2076 +6231 +7333 + +5028 +2989 +3801 +1895 +2367 +5747 +2724 +1304 +3085 +2524 +1295 +1022 +2613 +4239 +4413 + +3418 +23885 + +3895 +3302 +1097 +3150 +1379 +1815 +1484 +2074 +2616 +3395 +6100 +5488 +1006 +3046 + +1951 +8238 +6010 +3652 +6640 +7990 +3900 +8191 + +6978 +3025 +1966 +7152 +5963 +7326 +1866 +2507 +4764 +1434 +3194 +3326 + +3081 +6828 +6495 +3257 +4236 +4938 +7090 +2826 +6614 +1341 +7098 +6771 + +18885 +3826 +13103 +1193 + +16653 +13676 +19294 +13878 + +7460 +4327 +7335 +4898 +6259 +7941 +5940 +5603 +3095 +5674 +2886 + +8680 +4662 +8044 +8210 +5886 +9057 +7917 +3258 +5995 + +2800 + +31568 + +5423 +33845 + +8988 +13780 +9059 +14650 +9824 + +10327 +13747 +10937 +4594 +6882 + +2696 +2860 +1070 +1662 +6098 +2369 +2821 +3603 +3593 +5347 +4871 +4967 +2211 +3330 +5226 + +8520 +1896 +1906 +5172 +6845 +1565 +6366 +5509 +5068 + +4047 +4661 +2384 +1904 +4705 +5402 +6650 +6120 +4834 +5897 +6492 +6200 +5868 + +4130 +1530 +9654 +9306 +8529 +2508 +8918 +4040 +3717 + +3245 +4195 +11698 +10053 +6635 +4749 + +12283 +11894 +8168 + +6418 +7151 +4827 +2391 +8643 +2315 +8664 +3547 +6900 +4418 + +65083 + +5164 +1748 +8739 +4945 +2604 +5493 +5310 +3763 +6011 +4091 + +4342 +2566 +6021 +4067 +1385 +2486 +4444 +1713 +3380 +4253 +4720 +1602 +4814 +3234 +1739 + +13603 +22634 + +7845 + +4139 +8832 +7441 +1967 +6707 +7981 +3416 +10633 + +4808 +9206 +6975 +4257 +6024 +5176 + +10639 +9329 +5010 +7215 +4522 +8168 +9936 +9792 + +4326 +4601 +6508 +2501 +3866 +3060 +2188 +6429 +1014 +1383 +6937 +5313 +4392 + +8977 +2420 +1013 +4267 +9689 +2955 +5583 +2947 +5570 + +5067 +1139 +4531 +3001 +7698 +2028 +8025 +1424 +6837 +1357 +2186 + +5433 +19739 +5321 + +5019 +4714 +2688 +5290 +1593 +1320 +1703 +2978 +1476 +3034 +3538 +2825 +5819 +5547 + +6441 +1104 +3128 +7940 +5977 +8434 +4493 +2139 +1617 +4933 + +3219 +3273 +12383 +6076 +11222 +5523 + +8875 +1005 +4958 +3234 +3196 +5527 +9875 +7306 + +1591 +6031 +2124 +1224 +7008 +2559 +4540 +7670 + +5927 +4323 +5630 +1067 +5482 +1939 +5428 +3416 +3494 +2469 +4287 +5538 +1190 +4234 +1734 + +10808 +15234 +14069 +10497 +7697 diff --git a/2022/day1/main.c b/2022/day1/main.c @@ -0,0 +1,70 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <err.h> + +// Inserts a value into the array at the given index by pushing all elements at +// and to the right of the index one position to the right. Like a list insert +// but without the list. +void +intarrayInsert(int *array, size_t arrayLength, size_t index, int value) +{ + for (size_t i = arrayLength-1; i > index; i--) + array[i] = array[i-1]; + array[index] = value; +} + +int +main (void) +{ + FILE *fp = fopen("input.txt", "r"); + if (fp == NULL) + { + printf("Failed to open file %s", "input.txt"); + exit(EXIT_FAILURE); + } + + int bestElves[3] = {0, 0, 0}; + int bestElvesCalories[3] = {0, 0, 0}; + int currentCalories = 0; + int currentElf = 1; + + char *line = NULL; + size_t linesize = 0; + ssize_t linelen = 0; + while ((linelen = getline(&line, &linesize, fp)) != -1) + { + // If the current line is blank, we are done counting up the + // Calories of the snacks the current elf is carrying + if (strncmp(line, "\n", linelen) == 0) + { + for (int i = 0; i < 3; i++) + { + if (currentCalories > bestElvesCalories[i]) + { + intarrayInsert(bestElvesCalories, sizeof(bestElvesCalories)/sizeof(int), i, currentCalories); + intarrayInsert(bestElves, sizeof(bestElves)/sizeof(int), i, currentElf); + break; + } + } + currentCalories = 0; + currentElf += 1; + } + else + { + // strtonum(3) is on OpenBSD only 😠 + currentCalories += atoi(line); + } + } + free(line); + if (ferror(fp)) + err(1, "getline"); + fclose(fp); + + printf("PART 1: The elf carrying the most Calories is elf %d with %d Calories\n", bestElves[0], bestElvesCalories[0]); + printf("PART 2: The top three elves carrying the most Calories are elves %d, %d, and %d with %d Calories total\n", + bestElves[0], bestElves[1], bestElves[2], + bestElvesCalories[0] + bestElvesCalories[1] + bestElvesCalories[2]); + + exit(EXIT_SUCCESS); +} diff --git a/2022/day10/input.txt b/2022/day10/input.txt @@ -0,0 +1,146 @@ +noop +noop +addx 15 +addx -10 +noop +noop +addx 3 +noop +noop +addx 7 +addx 1 +addx 4 +addx -1 +addx 1 +addx 5 +addx 1 +noop +noop +addx 5 +addx -1 +noop +addx 3 +noop +addx 3 +addx -38 +noop +addx 3 +addx 2 +addx 5 +addx 2 +addx 26 +addx -21 +addx -2 +addx 5 +addx 2 +addx -14 +addx 15 +noop +addx 7 +noop +addx 2 +addx -22 +addx 23 +addx 2 +addx 5 +addx -40 +noop +noop +addx 3 +addx 2 +noop +addx 24 +addx -19 +noop +noop +noop +addx 5 +addx 5 +addx 2 +noop +noop +noop +noop +addx 7 +noop +addx 3 +noop +addx 3 +addx -2 +addx 2 +addx 5 +addx -38 +noop +noop +noop +addx 5 +addx 2 +addx -1 +addx 2 +addx 30 +addx -23 +noop +noop +noop +noop +addx 3 +addx 5 +addx -11 +addx 12 +noop +addx 6 +addx 1 +noop +addx 4 +addx 3 +noop +addx -40 +addx 4 +addx 28 +addx -27 +addx 5 +addx 2 +addx 5 +noop +noop +addx -2 +addx 2 +addx 5 +addx 3 +noop +addx 2 +addx -25 +addx 30 +noop +addx 3 +addx -2 +addx 2 +addx 5 +addx -39 +addx 29 +addx -27 +addx 5 +noop +noop +noop +addx 4 +noop +addx 1 +addx 2 +addx 5 +addx 2 +noop +noop +noop +noop +addx 5 +addx 1 +noop +addx 2 +addx 5 +addx -32 +addx 34 +noop +noop +noop +noop diff --git a/2022/day10/main.c b/2022/day10/main.c @@ -0,0 +1,78 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <err.h> + +int +sampleValues(int x, int cycle) +{ + // Could compact this with cycle - 20 % 40 + switch (cycle) + { + case 20: + case 60: + case 100: + case 140: + case 180: + case 220: return x * cycle; + } + return 0; +} + +void +drawPixel(int x, int cycle) +{ + if ((cycle - 1) % 40 >= x - 1 && (cycle - 1) % 40 <= x + 1) + printf("#"); + else + printf(" "); + + if (cycle % 40 == 0) + printf("\n"); +} + +int +main (void) +{ + FILE *fp = fopen("input.txt", "r"); + if (fp == NULL) + { + err(1, "Failed to open input.txt"); + exit(EXIT_FAILURE); + } + + + int x = 1; + int cycle = 0; + int change = 0; + int sum = 0; + + char *line = NULL; + size_t linesize = 0; + ssize_t linelen = 0; + while ((linelen = getline(&line, &linesize, fp)) != -1) + { + line[--linelen] = '\0'; // cut off the newline + change = atoi(&line[5]); + + cycle++; + drawPixel(x, cycle); + sum += sampleValues(x, cycle); + + if (line[0] == 'a') + { + cycle++; + drawPixel(x, cycle); + sum += sampleValues(x, cycle); + x += change; + } + } + free(line); + if (ferror(fp)) + err(1, "getline"); + fclose(fp); + + printf("PART 1: %d\n", sum); + + exit(EXIT_SUCCESS); +} diff --git a/2022/day11/input.txt b/2022/day11/input.txt @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 85, 77, 77 + Operation: new = old * 7 + Test: divisible by 19 + If true: throw to monkey 6 + If false: throw to monkey 7 + +Monkey 1: + Starting items: 80, 99 + Operation: new = old * 11 + Test: divisible by 3 + If true: throw to monkey 3 + If false: throw to monkey 5 + +Monkey 2: + Starting items: 74, 60, 74, 63, 86, 92, 80 + Operation: new = old + 8 + Test: divisible by 13 + If true: throw to monkey 0 + If false: throw to monkey 6 + +Monkey 3: + Starting items: 71, 58, 93, 65, 80, 68, 54, 71 + Operation: new = old + 7 + Test: divisible by 7 + If true: throw to monkey 2 + If false: throw to monkey 4 + +Monkey 4: + Starting items: 97, 56, 79, 65, 58 + Operation: new = old + 5 + Test: divisible by 5 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 5: + Starting items: 77 + Operation: new = old + 4 + Test: divisible by 11 + If true: throw to monkey 4 + If false: throw to monkey 3 + +Monkey 6: + Starting items: 99, 90, 84, 50 + Operation: new = old * old + Test: divisible by 17 + If true: throw to monkey 7 + If false: throw to monkey 1 + +Monkey 7: + Starting items: 50, 66, 61, 92, 64, 78 + Operation: new = old + 3 + Test: divisible by 2 + If true: throw to monkey 5 + If false: throw to monkey 1 diff --git a/2022/day11/main.c b/2022/day11/main.c @@ -0,0 +1,154 @@ +#include <stdio.h> +#include <stdlib.h> + +struct monkey { + unsigned long items[32]; + int numItems; + char operator; + int operand; + int modulus; + int trueMonkey; + int falseMonkey; + int inspections; +}; + +void +throwItem(struct monkey **monkeys, int from, int to) +{ + monkeys[to]->items[monkeys[to]->numItems++] = monkeys[from]->items[0]; + for (int i = 0; i < monkeys[from]->numItems; i++) + monkeys[from]->items[i] = monkeys[from]->items[i+1]; + monkeys[from]->numItems--; +} + +int +main (void) +{ + struct monkey *monkeys[8]; + for (int i = 0; i < 8; i++) + monkeys[i] = (struct monkey *)malloc(sizeof(struct monkey)); + + unsigned long items0[] = {85, 77, 77}; + monkeys[0]->numItems = 3; + for (int i = 0; i < monkeys[0]->numItems; i++) + monkeys[0]->items[i] = items0[i]; + monkeys[0]->operator = '*'; + monkeys[0]->operand = 7; + monkeys[0]->modulus = 19; + monkeys[0]->trueMonkey = 6; + monkeys[0]->falseMonkey = 7; + monkeys[0]->inspections = 0; + + unsigned long items1[] = {80, 99}; + monkeys[1]->numItems = 2; + for (int i = 0; i < monkeys[1]->numItems; i++) + monkeys[1]->items[i] = items1[i]; + monkeys[1]->operator = '*'; + monkeys[1]->operand = 11; + monkeys[1]->modulus = 3; + monkeys[1]->trueMonkey = 3; + monkeys[1]->falseMonkey = 5; + monkeys[1]->inspections = 0; + + unsigned long items2[] = {74, 60, 74, 63, 86, 92, 80}; + monkeys[2]->numItems = 7; + for (int i = 0; i < monkeys[2]->numItems; i++) + monkeys[2]->items[i] = items2[i]; + monkeys[2]->operator = '+'; + monkeys[2]->operand = 8; + monkeys[2]->modulus = 13; + monkeys[2]->trueMonkey = 0; + monkeys[2]->falseMonkey = 6; + monkeys[2]->inspections = 0; + + unsigned long items3[] = {71, 58, 93, 65, 80, 68, 54, 71}; + monkeys[3]->numItems = 8; + for (int i = 0; i < monkeys[3]->numItems; i++) + monkeys[3]->items[i] = items3[i]; + monkeys[3]->operator = '+'; + monkeys[3]->operand = 7; + monkeys[3]->modulus = 7; + monkeys[3]->trueMonkey = 2; + monkeys[3]->falseMonkey = 4; + monkeys[3]->inspections = 0; + + unsigned long items4[] = {97, 56, 79, 65, 58}; + monkeys[4]->numItems = 5; + for (int i = 0; i < monkeys[4]->numItems; i++) + monkeys[4]->items[i] = items4[i]; + monkeys[4]->operator = '+'; + monkeys[4]->operand = 5; + monkeys[4]->modulus = 5; + monkeys[4]->trueMonkey = 2; + monkeys[4]->falseMonkey = 0; + monkeys[4]->inspections = 0; + + unsigned long items5[] = {77}; + monkeys[5]->numItems = 1; + for (int i = 0; i < monkeys[5]->numItems; i++) + monkeys[5]->items[i] = items5[i]; + monkeys[5]->operator = '+'; + monkeys[5]->operand = 4; + monkeys[5]->modulus = 11; + monkeys[5]->trueMonkey = 4; + monkeys[5]->falseMonkey = 3; + monkeys[5]->inspections = 0; + + unsigned long items6[] = {99, 90, 84, 50}; + monkeys[6]->numItems = 4; + for (int i = 0; i < monkeys[6]->numItems; i++) + monkeys[6]->items[i] = items6[i]; + monkeys[6]->operator = '*'; + monkeys[6]->operand = 0; + monkeys[6]->modulus = 17; + monkeys[6]->trueMonkey = 7; + monkeys[6]->falseMonkey = 1; + monkeys[6]->inspections = 0; + + unsigned long items7[] = {50, 66, 61, 92, 64, 78}; + monkeys[7]->numItems = 6; + for (int i = 0; i < monkeys[7]->numItems; i++) + monkeys[7]->items[i] = items7[i]; + monkeys[7]->operator = '+'; + monkeys[7]->operand = 3; + monkeys[7]->modulus = 2; + monkeys[7]->trueMonkey = 5; + monkeys[7]->falseMonkey = 1; + monkeys[7]->inspections = 0; + + // I just used an external calculator + unsigned long lcm = 9699690; + + /* for(int i = 0; i < 20; i++) */ + for(int i = 0; i < 10000; i++) + { + for (int j = 0; j < 8; j++) + { + monkeys[j]->inspections += monkeys[j]->numItems; + while (monkeys[j]->numItems > 0) + { + + if (monkeys[j]->operator == '*' && monkeys[j]->operand == 0) + monkeys[j]->items[0] *= monkeys[j]->items[0]; + else if (monkeys[j]->operator == '*') + monkeys[j]->items[0] *= monkeys[j]->operand; + else + monkeys[j]->items[0] += monkeys[j]->operand; + + /* monkeys[j]->items[0] /= 3; */ + monkeys[j]->items[0] %= lcm; + + if (monkeys[j]->items[0] % monkeys[j]->modulus == 0) + throwItem(monkeys, j, monkeys[j]->trueMonkey); + else + throwItem(monkeys, j, monkeys[j]->falseMonkey); + } + } + } + + for (int i = 0; i < 8; i++) + printf("Monkey %d inspected items %d times.\n", + i, monkeys[i]->inspections); + + exit(EXIT_SUCCESS); +} diff --git a/2022/day12/input.txt b/2022/day12/input.txt @@ -0,0 +1,41 @@ +abccccccccaaaaaaaccaaaaaaaaaaaaaaaaccccccccccccccccccccccccccccccccccccaaaaaa +abccccccccaaaaaaaccaaaaaaaaaaaaaaaaccccccccccccccccccccccccccccccccccccaaaaaa +abccccccccccaaaaaaccaaaaaaaaaaaaaaaaccccccccccccccccacccccccccccccccccccaaaaa +abcccccaaaacaaaaaaccaaaaaaaaaaaaaaaaacccccccccccccccaaaccccaccccccccccccccaaa +abccccaaaaacaaccccccaaaaaacaaacaacaaaaaaacccccccccccaaaacccaacccccccccccccaaa +abaaccaaaaaaccccaaacaaaacacaaacaaccaaaaaacccccccccccaklaccccccccccccccccccaac +abaaccaaaaaaccaaaaaacccccccaaacccaaaaaaaccccccccccckkkllllccccccccccccccccccc +abaaccaaaaaaccaaaaaacccccccaaaaacaaaaaaacccccccccckkkklllllcccccccaaaccaccccc +abacccccaacccccaaaaacccccccaaaaaccaaaaaaacccccccckkkkpppllllccccccaaaaaaccccc +abacccccccccccaaaaacccccccccaaaacccaaaaaaccccccckkkkpppppplllccccddddaaaccccc +abccccccccccccaaaaaccccccccccaaaccaaaccccccccccckkkppppppppllllldddddddaccccc +abccacccccccccccccccccccccccccccccaaccccccccccckkkopppupppplllmmmmdddddaacccc +abccaaacaaaccccccccccccccccccccaaaaaaaaccccccckkkkopuuuuupppllmmmmmmddddacccc +abccaaaaaaaccccccccccccccccccccaaaaaaaacccccjjkkkooouuuuuuppqqqqqmmmmddddcccc +abccaaaaaacccccccccccccccaaccccccaaaacccccjjjjjjoooouuxuuuppqqqqqqmmmmdddcccc +abcaaaaaaaacccccccccccccaaacccccaaaaaccccjjjjoooooouuuxxuuvvvvvqqqqmmmdddcccc +abaaaaaaaaaacccccccaaaaaaacaacccaacaaacccjjjooooouuuuxxxxvvvvvvvqqqmmmdddcccc +abaaaaaaaaaacccaaacaaaaaaaaaacccacccaaccjjjooootttuuuxxxyyvyyvvvqqqmmmeeecccc +abcccaaacaaacccaaaaaaacaaaaaccccccccccccjjjooottttxxxxxxyyyyyyvvqqqmmmeeccccc +abcccaaacccccccaaaaaacaaaaaccccaaccaacccjjjnnntttxxxxxxxyyyyyvvvqqqnneeeccccc +SbccccaacccccccaaaaaaaaacaaacccaaaaaacccjjjnnntttxxxEzzzzyyyyvvqqqnnneeeccccc +abcccccccccccccaaaaaaaaacaaccccaaaaaccccjjjnnnttttxxxxyyyyyvvvrrrnnneeecccccc +abcccaacccccccaaaaaaaaaccccccccaaaaaacccciiinnnttttxxxyyyyywvvrrrnnneeecccccc +abcccaaaaaaccaaaaaaaacccccccccaaaaaaaaccciiiinnnttttxyyywyyywvrrrnnneeecccccc +abcccaaaaaaccaaaaaaaacccccccccaaaaaaaacccciiinnnntttxwywwyyywwwrrnnneeecccccc +abcaaaaaaaccaaaaaaaaaccccccccccccaacccccccciiinnnttwwwwwwwwwwwwrrnnneeecccccc +abcaaaaaaaccaaaaaacccccccccccccccaaccccccaaiiiinnttwwwwwwwwwwwrrrnnnffecccccc +abcccaaaaaaccaaaaaccccccccccccccccccccaaaaaciiinnssswwwssssrwwrrrnnnfffcccccc +abaacaaccaaccaaaccccccccaacccccccccccccaaaaaiiinnssssssssssrrrrrronnfffcccccc +abaccaaccaacccccccccaaacaacccccccccccccaaaaaiiimmmssssssmoosrrrrooonffaaacccc +abaaaccccaaaaaaccccccaaaaaccccccccccccaaaaaccihmmmmsssmmmoooooooooofffaaacccc +abaaaccccaaaaaacccccccaaaaaacccccccccccccaacchhhmmmmmmmmmoooooooooffffaaccccc +abaacccaaaaaaaccccccaaaaaaaaccccaaccccccccccchhhhmmmmmmmgggggooofffffaaaccccc +abaacccaaaaaaaccccccaaaaaaaccccaaaaccccccccccchhhhmmmmhggggggggfffffaaaaccccc +abccccccaaaaaaacccccaacaaaaacccaaaaccccccccccchhhhhhhhggggggggggfffaacaaccccc +abccaacccaaaaaaccccccccaaaaaccaaaaacccccccccccchhhhhhhggaaaaaaccccccccccccccc +abccaaaccaaccccccccccccccaaaaaaaaaccccccccccccccchhhhaaaccaaaacccccccccccccaa +abaaaaaaaccccccccccccccccaaaaaaaaccccccccccccccccccccaaaccccaaccccccccccccaaa +abaaaaaaaccccccccaaaccccacaaaaaacccccccccccccccccccccaaaccccccccccccccccccaaa +abaaaaaacccccccaaaaacaaaaaaaaaaacccccccccccccccccccccaaccccccccccccccccaaaaaa +abaaaaaacccccccaaaaaaaaaaaaaaaaaaacccccccccccccccccccccccccccccccccccccaaaaaa diff --git a/2022/day12/main.c b/2022/day12/main.c @@ -0,0 +1,188 @@ +#include <stdio.h> +#include <stdlib.h> + +struct coords +{ + int x; + int y; + int elevation; + int explored; + struct coords *parent; +}; + +struct FIFOqueue +{ + int size; + struct coords *contents[128]; +}; + +void +enqueue(struct FIFOqueue *queue, struct coords *item) +{ + queue->contents[queue->size++] = item; +} + +struct coords* +dequeue(struct FIFOqueue *queue) +{ + struct coords *item = queue->contents[0]; + queue->size--; + for (int i = 0; i < queue->size; i++) + queue->contents[i] = queue->contents[i+1]; + return item; +} + +// Depth-first search algorithm +int +traverseMountain(struct coords *map[41][77], struct coords *start, struct coords *end) +{ + int steps = 0; + + struct FIFOqueue *queue = (struct FIFOqueue *)malloc(sizeof(struct FIFOqueue)); + queue->size = 0; + + start->explored = 1; + + enqueue(queue, start); + + while (queue->size > 0) + { + // Current position + struct coords *cur = dequeue(queue); + + if (cur == end) + { + int result = 0; + while (end->parent) + { + end = end->parent; + result++; + } + // Reset state + for(int x = 0; x < 41; x++) + { + for(int y = 0; y < 77; y++) + { + map[x][y]->explored = 0; + map[x][y]->parent = NULL; + } + } + free(queue); + return result; + } + + // North + if (cur->y - 1 >= 0 + && ! map[cur->x][cur->y - 1]->explored + && map[cur->x][cur->y - 1]->elevation - 1 + <= map[cur->x][cur->y]->elevation) + { + map[cur->x][cur->y - 1]->explored = 1; + map[cur->x][cur->y - 1]->parent = map[cur->x][cur->y]; + enqueue(queue, map[cur->x][cur->y - 1]); + } + + // East + if (cur->x + 1 < 41 + && ! map[cur->x + 1][cur->y]->explored + && map[cur->x + 1][cur->y]->elevation - 1 + <= map[cur->x][cur->y]->elevation) + { + map[cur->x + 1][cur->y]->explored = 1; + map[cur->x + 1][cur->y]->parent = map[cur->x][cur->y]; + enqueue(queue, map[cur->x + 1][cur->y]); + } + + // South + if (cur->y + 1 < 77 + && ! map[cur->x][cur->y + 1]->explored + && map[cur->x][cur->y + 1]->elevation - 1 + <= map[cur->x][cur->y]->elevation) + { + map[cur->x][cur->y + 1]->explored = 1; + map[cur->x][cur->y + 1]->parent = map[cur->x][cur->y]; + enqueue(queue, map[cur->x][cur->y + 1]); + } + + // West + if (cur->x - 1 >= 0 + && ! map[cur->x - 1][cur->y]->explored + && map[cur->x - 1][cur->y]->elevation - 1 + <= map[cur->x][cur->y]->elevation) + { + map[cur->x - 1][cur->y]->explored = 1; + map[cur->x - 1][cur->y]->parent = map[cur->x][cur->y]; + enqueue(queue, map[cur->x - 1][cur->y]); + } + } +} + +int +main (void) +{ + FILE *fp = fopen("input.txt", "r"); + if (fp == NULL) + { + err(1, "Failed to open input.txt"); + exit(EXIT_FAILURE); + } + + struct coords *map[41][77] = { NULL }; + struct coords *start = NULL; + struct coords *end = NULL; + + int n = 0; + char *line = NULL; + size_t linesize = 0; + ssize_t linelen = 0; + while ((linelen = getline(&line, &linesize, fp)) != -1) + { + line[--linelen] = '\0'; + for (int i = 0; i < linelen; i++) + { + map[n][i] = (struct coords *)malloc(sizeof(struct coords)); + map[n][i]->x = n; + map[n][i]->y = i; + map[n][i]->elevation = (int)line[i]; + map[n][i]->explored = 0; + map[n][i]->parent = NULL; + + if (line[i] == 'S') + { + start = map[n][i]; + start->elevation = 97; // 'a' + } + else if (line[i] == 'E') + { + end = map[n][i]; + end->elevation = 123; // 'z' + 1 + } + } + n++; + } + free(line); + if (ferror(fp)) + err(1, "getline"); + fclose(fp); + + int part1 = traverseMountain(map, start, end); + + int part2 = 999; + for(int x = 0; x < 41; x++) + { + for(int y = 0; y < 77; y++) + { + if (map[x][y]->elevation == 'a') + { + int temp = traverseMountain(map, map[x][y], end); + if (temp && temp < part2) + part2 = temp; + } + } + } + + printf("The shortest path up to the peak requires %d steps.\n", part1); + printf("The shortest path from any lowest point requires %d steps.\n", part2); + + exit(EXIT_SUCCESS); +} diff --git a/2022/day13/input.txt b/2022/day13/input.txt @@ -0,0 +1,449 @@ +[[],[1],[[[1,3],2,1,3]]] +[[[],6,[3,8]],[]] + +[[9,6,0],[[0]],[[[5,3,1,1,10],4,[],[10,2],[2,9,9,7]],[[10],8]],[1,[[4,9,10,0]],[[0,8],10,8],0],[[7,[5,6,5,7],9,4,[0,4]]]] +[[9,[],[10,4],[2,9,[2]]],[[[],6,3],1,7,[5,0],[7,7,[10],[0,6,2,4,7],7]]] + +[[[],[],[]],[6,[[1,4,7,9],7],[]]] +[[],[],[],[[[6,2,1],[5,7],2,[8,0],[7,1,5,6]]]] + +[[5,1],[3],[4,6]] +[[3,3,[1,[2,4,2,7,2],[0,3],8],9,0],[4,[6]],[],[9,[2,[9,9,2,6,7],[],2,2],[[],[10],[],[0]],6]] + +[[[10,[10,3,5],1,8],[[10,4],2,10,0,6],[3,3,2,[4,4],[3,6]]],[[[],2,0,8,[2,2,3,3]]],[[8],3,10,4,4],[]] +[[1,[7,10],[[2],[10,2,1,4,2],[2,3,8],0,8],5],[7,[[10,4],8]],[],[],[]] + +[[[4,[2,2,8,0]],3,5,[0],3],[4,[10],10,1],[3],[1],[1,4,[10],[[8],[5,3,0,6,6]],8]] +[[2,4],[3,1,[],[[]],[4,[7,0,3],0]],[]] + +[[2,0],[[],[[3,0,6],6,2,6],8,5,[0,[10,0,10,10,8],[4,5,1]]],[[[6,7,0,6,10],[8],[1],6,7],0,6,[10,5,4,[4,2,9],0],[[2,7,8,6,7]]],[[[7,8,6,3],0,[4,3,3,10,8],[4]],[8],2,1,[1,7,[2,3,6],[7,3],9]]] +[[6],[5],[],[6,0],[[],[9,[10,5],10,[4,3,0,6,6]]]] + +[10,0,2,0] +[10,0,2,0,2] + +[[9],[4],[1,[[7],[8,3,4,6,5],[7,7,9],[10,9,1],[7]],[[3,9,9],[],[],6,[1,2]]],[],[3]] +[[4],[3,9,[[9,10,8,2,7],9,[10,9],3,[7]]],[[6],[9,[9,10,3],[],0],[],[8],9],[7,1]] + +[[0,[[2,7,1,9],6,[4],[3,4,10]],[2,4,9,[3,8],7]],[4,[3]]] +[[[[2,3,5,2,7],[10],[10,2,7],4,0],[10,[8,10,2,1,2],[7]],3,[10,[7,3,10,3]]],[],[6,3,[[0]]],[0,[0,[],6,4],3,[[9,5]]],[9,6,7,8,3]] + +[[[],1],[2,1,[],8,[]],[8]] +[[4],[8,[9],[[5,3,1,3,9],[6,0,0],10,8,5],6],[]] + +[[1,[[7,6,3,4],9,[]]],[6],[],[[10],[3,[7,9],[8,0,1,6,7],3,[7,8,4,5]],3],[[4,[8,1,0,7],6]]] +[[[1]],[4,5,2,[0]]] + +[[9,[0,[1,10,9,8]]],[[3]]] +[[],[[[],[5,3,3],[]]]] + +[[0]] +[[],[[0,4,[],[10]]],[6,[[1,4,8]],3,[5,[10,3,7,5],7,[7,2,3,4]],8],[]] + +[[[9,[6,1,5,6,6]],[[6,5,9],9,2,[2,10,0,4]],2,7,[1,[],[5,5,6,3,10],[1,5,3,6]]]] +[[4],[],[],[[[]],[3,[8,3,3,2],0],[6,[8],1,[6,4],[5,0]]],[10,6]] + +[[4]] +[[],[3,[[4,1,3,7],5,[],[10,4,0,8]],[],8,9],[9,[[6,3,2,7,0],1,[],[6,4],6],6,[4,9,1,[3,0,6,4,5]],1],[9,[5,3,7],[[1,8,10,7],[5,3,5],[6],0]],[1]] + +[[5,[2,[6,1,4,2]],[[6,6,2],[10,3,10,10],[5,8,10],10],[0,3,[2,4,2,1]],5],[[[1,9],10,[]],[9,[1,9,3],8,7,[]],[[1],[],3,[5,2],10],[[1,2,1],3]],[3],[[],10,0,[]]] +[[],[[5],[9,9,4]],[[5,[10,10],[0,9,6,3,7],2,5]],[8],[1,7,8]] + +[[],[8,6],[0,8,[[7,0,2]]],[0,0,[9,[9]]],[]] +[[[1,[10,8,0,0],0],[]],[],[],[[7,[4,7,10,9],[]],[7,4,[],2,[3]]],[]] + +[[5,4,[]]] +[[0,1,9,9],[6],[[[4],[7,1,7,10],[5,2,7]],[1,8,6]],[[[8],[6],1,[3,8]],10],[]] + +[[1,9,[5,5,[0,5,0],[0,7,1,3,1]],3],[7,[],1,[4,7,6]],[[2,2,2,7,[8]],[[9],[5,9,5,2],6,7],[[7,3,10,1],2,0,[1,3],5],6,6]] +[[],[[[5,2],3,[],1,10],1,9,[3,6,4,[],[4,1,2,7]]],[],[[[9,9,10,2,5],[3]],[8,1,[6,0]]],[[],[],[2,[5,2,6,6],[4,9,6,3,9],2],6]] + +[[[[],3,7,5],[3,[4,9,0],[]],[[6,7,7,7,10],[0,6,7,4,8],5,7,[1,10,8,10]],1,1],[[4,5,10,[10,1]],9,[[6,1,0,6]],9],[3],[],[0]] +[[10,0,[[5,4,2,3]],4],[6,[],4,[8,8,[3,7,10,7],7,4],2],[4,7,[],[[3,5,6],2],10]] + +[[[[7,9],[7]],[3,10],[]]] +[[[[4,2],[0,9]],[2,0],4,4]] + +[[[9,[6,1],5,[5,10,0]]],[[[4,7,5,8,0],7,[2,2,3,0,4],[8,5,3,2,8]]],[3,[],[5,[3],9,[10,5,6]]],[[0],9,[2,[10,9,4,7],[7,4,0],[1]],4,1]] +[[[0,10]],[3,8,1,[10,[6,7,8],[],[10,6,7,6,9]],[10,5]],[6,7,[5]],[[[],7],2,3,[3]],[2,6]] + +[[[2,3,7,9]],[[[],[6],[],[9,1,0,0,10],10],1,7],[7,[[2,5,0,5],2,[9,9,1,2,3],[],9],7]] +[[[[3,0]],2,[[2,8,0,8],1,[0,3],[7,10,5,9,2],[10,5,4,5]]],[3,8,7,3],[5,[7,5],[2,3,10,[9,6,9],[7]],3,3],[0,[5,[7],[],6,[0,7,2,5]],4,[[5,6,9,9,3],[1,7,5,5,1],[7,4,3,8,5],[6,10,4,10,10],8]]] + +[[[9,[2,4,3]],6],[4,9],[8]] +[[10,[],7],[10],[[[1,6],[4,0],9,8],[[6,1,5,6],2],10,5],[],[[8,[8,5],[1,6,6,4,10]],5,8,6]] + +[[],[4,[4,[6,10,0],[10,8,10,10,2],[6,0]]],[[[5]]],[6]] +[[[10,[5,9,6]]]] + +[[[],[4],[[5,6,7,7],[4,3,7],[2,1],[]],1]] +[[[8,8],2,[],[],[[7,3,2],6,5,[6]]],[2,[5,7,[3,5],9],8],[1,[10,[0,6],[10,7],1,[9,3,4]],5],[]] + +[[]] +[[9,8]] + +[[8,3],[[],2,[[]],[2,5,9,1]],[[[7,9,9,4,1],[10,3],3,[3]],[1,10,3]]] +[[8,[2,3,7,9,[]],[],7],[]] + +[[[[4],[],6,9,[3,6,8]],[1,[0,6,7],[3,0]],5,[[6],[5,6,2,2],[8,10],1,2],[[3,8,7,10,10],4,8]],[1,[[4,8,2,7],3,[2],[],5]]] +[[[[3,6,4,8],[10,7,2,9],[4,6,5,9,1]]],[[4,7,4,[1,3],8],[[6,10],[5,0,3,0],5]],[[[3,9],[6,2,10,5,1],[4]]]] + +[[[[10,6],9,[5,1,9],[0,3,4,6],[6,1,8]],2],[5],[9]] +[[0,6,10,2],[[6,8,3,2,[9,8]]]] + +[[7],[],[],[[],[[10,0,3,3,1],0],[10],[5,[9],0,6,2],5],[[3,6,6,9,[8]],0,[4,9,[0,5],[]],[[7]],[[8,1,8,4],[2,6,6,3,4],[9,4,2,3],[],[10,4,5,7,2]]]] +[[4],[6,[],[2,9],7],[2]] + +[[[],0,[[2,6,8,0,9],6,[10,4],2,[]]]] +[[[0,[],[3],10],10,[6,[10,9,9,6],5,[0,2,4,3],[4,3]],5],[3,[5,0],7,2,[4,[5],[3,4,8],[3,5,4,9,6],5]],[[[2,10,0]],[[10,3,2],[7,3],[2,4,2,6],[5]]]] + +[[[[6,3],4]],[[],[],[[5,8,0,5,8],7,0,0],[[],6,5,10]]] +[[],[10,[10,[1]],7,[[8],[6,7,7],3,4,8]]] + +[[],[[1,5,[9,7,8,3,9],9,[1,3,5,10,8]],6,10,[[5,6,3,0,8],9,1],4],[],[[[2,1],[3,10,1,7]],[9,[6]],2,4,4]] +[[],[[8,1,5,[6,1,7],[8,1,1,0]],5,1]] + +[[7,5,6,3,1],[[1],10],[[]],[[],2],[[[8],10,3,[8,3,1,9],6],0,[0,[0,2,1,0],0,[9,4,6]]]] +[[1,2,8,8],[5,[6,[1],[6],[],[3,1,8,6,0]],[]],[[[5,6],3],6,[]]] + +[[],[[[2,4,6,0,0],6],4],[[7,[2,1]],[5],4],[2,[],[0]],[6,[[9,7]]]] +[[6,7,4],[],[],[[[8,9,4],[1,7,3],[6,2],7,8],[5,[6,5,3]],[3,6]],[]] + +[10,2,2,7,5] +[10,2,2,7] + +[[9]] +[[[6,5],0,[[],[],[6,9,6,2,6]]],[7,3,[],8,[8,3,[0,3,3,8],[5,6,9],[1,1,9,3,1]]]] + +[[4,0],[1,[[7],[],[8,7],[7],10]]] +[[[]],[[[6,2,1,7,2],[7],2],8,[[6],5,6],1],[6,[0,10,6]]] + +[[[8,4],[3],2],[[[],[7],5],[4]],[9,1,5,5],[8,1,[[],7],1,[]],[[10,[],3,[7,2,6,3]],[],[[1,2,10,7,0],[],1],7,[3,[1,4,1]]]] +[[4,5],[[],[],[8,[5,8,5,5],[]]],[[],8,4,10],[]] + +[[[[1,2,1,9],[3,8,4,2,9],[10,0],[2,1,0,10],[2,1,2,9]],[0,3,8],2,6],[[9,[],[]],[[],[],[2],5,[10,2,7,1]],[[6,9,2,8,2],0,10],[[0,0,10],5]],[1,[[6],1]],[[1],5],[9,[[1,6,8]]]] +[[[[10,4,9,9],[],0,5]],[9,[6,9,6],10,9,6],[],[[3,8]],[[1,5],[[]],[]]] + +[[0,[10,[2]],10,9],[10,0],[[],8,4,3,[[9],6]],[[4,10],4,[],2]] +[[4,[],8,7,5],[6,[3,[4,4,1,6,5],6,2,8]]] + +[[],[8,[[0,7,2],[7,1,0,2],[],[1],9],3],[10],[[3],0]] +[[],[7,3],[[]]] + +[[1,[0,[],[8,8,4],[2,8,3,10],4],8,[[6,1],[0,9,3,1,4],[8],2]],[3,9,[2,[],[7,3],5]]] +[[[7],1,[[0,7],7]],[6,7,7],[],[]] + +[[[[5,1]],[3,5,0]]] +[[],[0,[1],[[7,4],10],10],[0,8,[[3],2],[[6,7,5,10,2],[9,6,8,9],8]],[7,2,[1,[6,5],[],[4],[5,4,8,0,10]],[8]]] + +[[4,[8,10],6]] +[[1,[7,10,4,6,[6,1]],7,[[3,8,1,6],[7,2,0],[]]]] + +[[10,[0]],[[],[10],10,7],[1,[]],[0,[5],[10],8,[2,0,1,[3,5,2,3,1],4]]] +[[1,5,4],[[[2,6]],5,[0,[],[2,5,4,10],[9,7,0]],10],[]] + +[[],[[[6,6,1,0,3],[5]]],[2,[[1,7,7,4,3],0,1],[[1,10,6],[1,7,9]],7,[[5,9,4],[5,10,2],[6,5],[3,6]]],[[4,[3,10]],[],[[10,4,6,1],[9,0,2],1,[4],[8]],[[9]],10]] +[] + +[[4,4,6,[1,[2,10],6,[7]],[7,6]],[4,0,[0,3],6,8],[],[9,[3,0,[3,5],1],9,2,6]] +[[4,[[7],4]]] + +[[[[8,6,1]],2],[[1,10,[5]],[[],[4,3,9,7],[3,8,9],2],7,10],[4,[[],[10,6,9,4],[],5,2],[3,4,1,[3,9,10,4,6]],0,[10]],[[10],[4,3,3,9],[],[]]] +[[6,[[2,2,8,6],[10,3,5],1]],[7],[[3,[4,1,2],2,10,[3]],5],[[8,[2,1,2,3],7],[5,[6],[0,3,9],[2,2]]],[[5],[9,2],9]] + +[[],[[[],[3,1,1],8],[2,0,3]],[[[1],[4]],3,9,5,[5,10,4,9]],[[[5,3,4],[4,3,10,9,8],0,8,[8,4,10]],10,2,[3,7]],[[],1]] +[[[]],[4,[[6,8,3,2,10]],[[7,2,0],3,[1,3,10,10,9],[1,8,0,0],6],[10,[0],[3,4],[9],0]]] + +[[],[[[7,5,5,7,3]],[[4,4],10,[6,8,6,8],4,[9]]],[[],0]] +[[2,[]],[[],[],2,[],[]]] + +[[[],0,2,[]],[9,[[],6,3]],[[[6,0,1,4],7,5,6,[7,8,1,6,0]],10,2,6,4],[[],5,1]] +[[],[6,3,9,[[4,7,0,3],7,[5,9,7,10],6]],[[[1,0,0,1],[7],3,[10,6,5,9,0]],[5],9,0,3],[7,[[5,7,8,3],3,[1,4,4,1],[6,2,7],9]]] + +[[6,6,10,6,0]] +[[[7,[3,7,6,10,1]],[[1,4,9,9],10,4],3],[0,[]],[1,9,[[4,8],[],5]],[],[]] + +[[[[1,3],[10,7,9],[0,6],[0,7],[4]],[[3,0],2,[4,2,8]],7],[0],[],[]] +[[1,10,9,9]] + +[[1],[],[9,4,10]] +[[[[9,7],[2,0,8,6,5],0,8],[[0]],10,[6,[],6],3]] + +[[4,[],[[3,2,1,4],1,[5,10,0],[6,10,5],[1,1,6,10,6]],8],[[1,[5,0,1,8]],[[],[3,8,6,8],[1],[0,4]],10],[2,[[],[],[2,3],0,[5,9,2,6,9]],2,7,3],[],[5,0,[8,[0,7,4,7]],[9,5,10,4],5]] +[[[[4,6,5],[9,3,7,10],[1,9,8,6,2],[1],[9,9,4,2]],[[8],[0,6,5],[6,9,10,8],8]],[3,[[0,9]]],[1,5,[[0,4],[9,6]],2,9]] + +[[7],[],[[4],7]] +[[[9],0],[[3,[6,1,8,8],[4,7,3,0]],[[6,4,0],10,4,3,9],10,6,1],[[6,[2,8,6,3,1],[]]],[7,[[1,0,1],2,0,[10],[10,3,5,7,1]]],[7,7]] + +[[9,[[6],[3,0,4,2],0,0,9],2],[[[8,4,0]],[[0,8,9],9,4,6]],[],[6,[[6,6,9,8]],[[5]]]] +[[[3,1,[5,5,7,6,6]],7,[3,10,[8]]],[1,8,[9,[2,10,6,7,2]]],[[4,2,3],5,10,1,9],[4,0,[],6],[[1,[5,8,8],8,1,3],[[2]],9,[7]]] + +[[[],9,[],8],[[],[[8,4,6,3,8]],5,[]],[1,[7,10,[7,10,6,7]],[]],[9,[[],3],[0,5,0,[1,10,5,2,4],8]]] +[[8,2,6,0],[7]] + +[[10,[[],6,9,0]],[6,[7,[10],0,3],5],[[[7,2,1,9],[1,2,7,0],[9,1,8],[],6],3,[[3,5,0,8,8],6,[10,8]]],[[[8,4,1,6],2],[[7],10]]] +[[[[1,8,5,4],7,[5,3],5,[7,5,5,7,10]],[[],5,5],[]],[]] + +[[2],[0],[[10,[1,1,2],[],4],[[10,2,4,5],0,[]],[[10]],[[9,7,1,0,4],[9],1,[3,0,3,1]],[]],[],[6,1,1]] +[[],[[10,[3,3,4,5],1,[9,10,7,8,1],4],[[7,3,10,10,8],1],[[6,10,2,8],[5,10,6],3,2],2],[[0],[[],4],3],[],[[[8,3],[0,1,5,2],4,[2,1,7,10],6],[[0],[8],0,3],8,[[4],[],7,9,[7,4,4]]]] + +[[[7,[],7],[[]],[8,6,8],5,7],[1,5]] +[[9,[[6,3,3,6]],4,[5],6],[3,[],[]],[[3]],[6,5,[[0,5,2]],6,[9,6,1,7,7]],[[1,7,[9,6],4,0],1,[[8,1,0,8]]]] + +[[8,0],[[[10],[10,10,6,3,7],[9,6,8,8]],3]] +[[3,[3,9],6],[],[],[[7,7,5,4,[7,10,0]]]] + +[[],[7],[[[9,4,6],[10,0,6],9],[6],[[8,3,2],[2,10,6,9,8],8,[0,9],5],10]] +[[2,9,[[7,9,2],[1,3,6,1],4,7,4],[[8,0,7],[2],[0,3,7],[],[5,8,9,4,2]]],[9,[[10,2,1,8],3,[0,10,3,7]],1],[[4,4],2],[[[6,1,0,4],3,[10,1],3],[[],[7,3,8],[7,0]],[7],5,[[3,7,5,9,0],8]]] + +[[10,[8],[8,[10,10,5,9,1],[3,6],[9,2,7,6],7],[6,[8,1,4],[]]],[[[5,0,8],3,5,5,5],1],[]] +[[6,[[4,0],1,4,[7,7,8,1],10],[[8,5,8,7],7,[1],[8,10,2],[9,2,1,3]],[10,[8],7,4,[3,1,9,2]]],[0,4,1,0,[[2,2,7,6,10],3,7,[6,7,10],[]]],[],[4,5]] + +[[[1]],[0,[[8,4,1,3],10,[3,7,9]]],[3,[[6],4,[9,6,5,10,4],[0,9]]],[0,5,[[5,2,2]]],[9,[9,4,3],[[9,4,4,4,0]],6]] +[[[],7,5],[0],[[[0,3,6],[9,1,5,2],[4,0],[],8]],[]] + +[[[7,7,[7,4,6]],[[6,6,2]],2],[[[4],[3,1],7,[2,6,8]],[[],[10],[]]]] +[[10,[[2],2,[8,2]]],[[],[8,[2,3]],5,[10,[6,5,6],8,[],6],4]] + +[[[[]],10,8,[],9],[[[1,4,8,2],[7,3,1,0,6],4,[7,6,1],6],[7]]] +[[[6,5,7,8],[9,[2,0,9],9,[1,8,9,8,0]]],[9]] + +[[[8,[],[10]]]] +[[[5,1,[9],10,[3,3,10,4,9]],[1,[5,8,4],1,10]],[3,[4,5,3],5,5],[],[[[9,0,5]],[],2,[[0],1,[9,9,5,5,3]],[[0,4,7,1]]],[]] + +[[2,[2,6,5,1,[7,10,4,8,5]],[10,[0,8,9],[9]],4,[[8],[5,9,9],[8,10,4]]],[],[[],6,0,4],[],[7]] +[[[[7,8]],8,[4,4],0],[[3],[7,10,[7,7,2,10,7],[0,5,1],8],[[7,4,8,8,1]],[6],4],[2,3,10,2]] + +[[[[0],[2,2],[5,6,5,7],2]],[[4,8,9],1,0,9,[[7,0],[]]],[3,[4,[0,2,10,8,9],[2,9]],[],10]] +[[4,[3],[[],9]],[8,[5,3,[],[8]],[],5,[2,7,[10,4],6,[4,10]]],[[],[],6,[[],0]]] + +[[0,4,8],[[],[[3,7,5,1,8],5,9,8],[]],[[10,[2,4]],10],[],[5,[[10,5,6,2,0],6],[[2,9],0],4]] +[[3,7]] + +[[[5],2],[2],[0,[[1,6,10],7,1,[],[0,1,6]]],[4]] +[[4,[],8,2],[],[8,[[4,0,6,9,7],10],[[10,4],[8,6,6,4],[4]]]] + +[[5,9,5,3,[3,0,[0],[10]]]] +[[[[0],4,8,[],5],[9,[1]]],[10],[[],[1,6,4,0]],[],[[],10,10]] + +[[],[8,[[1,3,7],[0,2,6],1,10,[6,6,9,2]],1,7,[[2],10,5,7,[]]]] +[[[9,[6,7,8,7],8,[0,2,9,9],[9]],[[],7],[[7,6,3],3,[0,8,9],[1,6]],5],[8,5],[10,1,[[],[3,9],[],5]],[]] + +[[],[10,2],[3,2,0,[[10],[10],1,2],[]],[],[5,[],[[1],[5,4,4,9,8]],[7,2,[]]]] +[[[],7,3],[[],2,0],[6,8,[[8,5,4,0,1]],0],[[[0,0,5,10]]],[[8,[7,10,6,4],8],8,[[10,6,3,2],10,[2]],[],[3]]] + +[[[3],[4,7,1,[2,2,1,8],[1,5]],4,2],[[],3],[],[[[3,8,0,6,5],6,[0]],4]] +[[[3,0],[[0,0,10,4],[4,6,4,5,2]],[[7],7,[10,7,2],[2,6,3],6],9]] + +[[[[3,4,2,3,4],[2,2],[1,7],5,[5]],8,[[5,6,9,8,0],2,0]],[],[1]] +[[],[[6,[],5,8,[6,1]],[[5,5,6,1,9],[10,5,0,7],5,[3,6],[5,6,10,0]],4]] + +[[5,[[2,9,4,5],7,4],6]] +[[[[1,6,3,1,0],[6,10,6],[],1,[]],6,[[1],[4,6,10],[],[6,1,10],[5,5]],2,[[8,6,4,4,7],[]]],[9,4]] + +[[],[],[2],[[[5,1,6],[4,1,9,7],6,[8,1,0],[0,6,0,10,1]],[],[[4,2,3,7,10],[10,6,3,6],3,[0]],[[10],[9,4,6]]],[4,0]] +[[],[[10,10,[6,3]]],[[[8,9,3,4]],4,[],10,[[4,1,10],4,8,[6,6,0,10],4]]] + +[[[6,[],7],[2,0,[3,4,0],6,[7,1]],[[2,6,10],9],[]],[[]]] +[[[[10,10,7],[0,6,8,4],6],9,[],10],[4],[0,[8,[],0],8,4],[[[8,9,8,6,1]],[[5,8],[],8,[3,3,3,10]],[[],[],[5,6,2,3,9],3,[3,3,5,4,0]],7,[[3,5],4,6,10]],[2,[2,6]]] + +[[[[9,7,5,2],[4,2,7,5,2]]],[0,[2,6,[3,7,6,0]],[0,6,[2],8],2,6],[[],2,[[9,9],[9,1,3,8,6],8,3,6]]] +[[[4],2,[1,6,[0,7,9,3],[10,4],[5,2,1]],[0,[],[6],1]],[[9,9,[6,8],9,[8]],[8,[2,10],[7,0],5],9,10]] + +[[3,4],[0,[0,0,[0,8,5,9]],5]] +[[[],[4,7,7],[[3],[4,5,6],7,[9,8,7],[10,8,0,10]],[[4,0,5,8,5],[8,3,9,5],5]],[[],0,6],[[[5,0,3],[],[4],[6,1,2,5]]],[[[8],[9,6],9],[[],[2,10]],[[5,7,7,7,7],8],[[],[],7,[5]],[6,[4,5],[3,4,6,5,2]]],[2,[]]] + +[[4,6,8,[4],[[6,8],[],0,6,1]],[1,[[3],[5,5,6,2],8],0,6],[[7,[1,4,4]],[10,[7,4,9,8,0],5,7],[[3,4,2,6]],0]] +[[],[[2,8,[4,3,3]],10,[9,[],8,8]]] + +[[[[1,10],[7,2,2,9]]],[[[9],9,[0]],[[8,10,7],0],[8,[5,2,1,2],[5,4,1,10,5],2,[9]],[[7,10,7,8],[7,10,4,0],[6,0,1,0,2],[4,4,4]],[10,[],0,[9,5,7,4,8],[8,7,1,6]]],[[[1],[8,8],9],3],[],[[[6,2,2],9,[],[1,1,6,3]],[7],0,0]] +[[[],[5,9,3,[5],8],[7,0,[],2,9],0,8],[[0,[],[2,4,6],9],[5,[],[8,6]],6,8,5],[[],10,7],[[[],3,5,[10,4,4]],3,8],[[[0,0,8]]]] + +[[3,10,[5,8,3],[],8],[[[1,7,10],8,[8,4],0,[7,4,0]],[6,6,[10,7,3,1,7]],[],[[0,9,8,10],6,[8,6,0],[8,10,2,1,4]]],[],[7]] +[[[],2,[1,[9,0,2],[1,7,1]]],[[[5,1,2],[6,6,1,2,2],2,5,[10,3]]]] + +[[8,[[3,1,9]],3]] +[[],[1,5,10],[3,[9,5,[10,2,10],7,8],[[]],8,[]]] + +[[[[4,0],[],[0,3,4]]],[3,9]] +[[5,9,0,6]] + +[[],[1],[3],[[[7,8,8,4],0],[7,5],[5,[2,0,5,10],[7]],10],[9,[10,7,[10,1,10,8]],5,7,0]] +[[],[],[[],10,[[6],[4,5],[2,2],[7,7]]],[[10,[8]],[[6,9],[3],8],2,[[0,9,1,3],0,5,2,3],[8,5,7,10]]] + +[[5,9,10,[3,7,10,[],1],3],[5,3,[[3,4]],1]] +[[[[7,3,3,7],7,2],[[2,3,8],[10],2]],[[8,[1,3,1,9],6,[6,1]],0,[9],3],[[],[[2,3,9,0],4],[10,7,[3]]],[5,4,[]]] + +[[[4,[8,1]],10],[[[],[4],5,3],[[5,6,1,9],[9,0,8],7],[],[[],[8,7,10],9,[4]]],[9,9]] +[[[[1],7],[[4],6,8,0,1]],[6,[0,[],[5],6,[1,6,2]]],[9,1,[],10],[[[8,3,2,7,3],[0,9,1,8]]],[]] + +[[[0],[]]] +[[[[10,2,2,6],[5],3,6,5]]] + +[[6,[[7,0],[7,5],1,3],[8,[3,2],[10,4,4,5,3],[6,10,10,6],5],1,3],[1],[[],[[],4,0]],[[[7,0,2]]]] +[[],[],[3,[3],0,[[4],8],[2,[5,7,3,0,2]]],[]] + +[[1,7,[[6,9,8,9,6],10]],[[]]] +[[4,[[4,4,9,7,1],[4,10]],[3,10,[3,10,10,3,1],9]],[10,10],[[[3,4,8,5,6],4,[]],0,8,4]] + +[[[]]] +[[[9,1,[5,2,7],[10]]]] + +[[10,[],8],[[[9,4,7,7,2],[0,5],[8,2,1],[10,5]],2,0],[],[[[5,1],[1,1,4,8],4],10,[4],9],[]] +[[[],[[0,1,1,8],[]],[8,2],7,5]] + +[[9,10,10,8,1],[],[4,[[4,4,8,6,9],[],[],7]],[4,9]] +[[[[6,4,7],2,9,9,10],[[9],[6,7,4,9,7],[2,10]],[9,[1,6],9,5,0]]] + +[[8],[[1],1,[1,2,0],6],[],[],[3,[[9,5,10],[8]],[6],[],1]] +[[[[]],[],8]] + +[[[[10,7,9,6,6],10,[6,2,8,0],5,4],[[4,7,10,10],3,[8,10,10],[2,10]],[[8,1,6,10,10],[]],[]],[2,6,10]] +[[],[9],[7,5,2,[6]],[[[3,0,7,6],[4,7],[4,8,2,3],6]],[4,5,[[10,7],[1,3],[8,10,9]],6,8]] + +[[8,6,[[0,0,3],[2,4,0,6],6,[4,1,10,2]],8],[[8,2,9,[4,6,8,1,6],3],[[8],2,[1,7,2]],[[],[7],8,8,[10]],8],[5,5,[9],2,[2,[2],4,8,[]]],[[[8],3,9,10,[1,1,9,7]],9,0],[[10],[8,7,3]]] +[[[[],6,[6,10],5],[],3,7]] + +[[0,9],[[3,1,8,3,7],[1]],[[1,[]],3,2,3],[2,5,1,[8,4,9,[3],4],5]] +[[[[10,7],5],2,2],[0,0,7],[7,[[5,3],5,0],[[],10,[6,10]]],[[[0],[],[5,10,3,3,0]],4,[[10,7],[0,0,4],8,10]],[]] + +[[8],[4]] +[[5,8,[0]],[8,[[3,3]]],[10,2,[10,3,[7,4,1],0],2,4],[[2],[3,5]]] + +[[10,8,[[6,10,5,5],[7,3]]],[[[2],[9,7],[],3,[10,4,9]],[],[1,[9,0],[8]],4,10]] +[[10,4,4,0],[]] + +[[1,7,7,8],[3,[]],[0,[5,10],8,[],[[4,10]]],[[[],4,9],8,[2,[10]]],[0,0,[6,7,[8,5,6]],[[5,2,3,9],10]]] +[[0,2],[6,[[4,0,9,9],1],[[7,0,9,2],3,3]],[4,4,6,[2,[3,5,6,7,3],1]],[],[2,5,7,8]] + +[[],[[],4,2]] +[[2,[6],0,3],[1,[1,[6,4,3,7,2],3,3],1,[[]]],[[[],[5,9,6,8,0],[7,10,10]],[[],[1,7,2,3,6],8,7]],[6,[[3,8,9],[2,2,4,9],[10,6,4,4],8,[1,9,3,10]],[1,[8,8],[6,5],4,[]],4,[1,10]]] + +[[[],5,10,7,9]] +[[5],[],[8,9,[[],[0,6,2],7],10],[[1,[10],[10,4,7,8]],2,1],[]] + +[[[],10,1],[[5,10,[],[]],9,7],[[]],[],[[7,[2,6,7]],9,5]] +[[1,[[],7,[7,2,2,4]],3,[6]],[8,[[1,1,5,1],2,5,[0,4,3,10]],3]] + +[[9,5]] +[[8],[8,6]] + +[[[8,10,5],[9,1,[]],2,4,6],[5],[[0,9,2],[],1,3,[8,6]]] +[[],[[[6,7,0,5],[3],[10,7,7],7],[[2,4,0,1],[7,1],[7,10,4,4,4]],[[10,4,10],[],[4,6,8],[6,0],[1,7,8,7,9]]]] + +[[3,9,[10,6,[9,8,8,8,0],[10,1,5,9]],10],[5,[[0],10,4,[0,1,7],8],[[2,3,7],[8,8,0,4],6,[1,1,2],[1,10,0,7,7]]],[[[10,8,9,8,9],1,[9,0,1,6]],0,8],[7,6,[[6,9,7]],[]],[[[5,4,8]],[[0,9],10,[2,6,2,9],[],4],[[7,2,7],[6,7,3,4,5],[7],1,[1,9,3,3]],[6,[6],[],[10,2,7,4,2]]]] +[[4,[[0,4,1,9,1],[],5,6],[[0],[0,6,7,9,2],[],[10,1,7,0],[7]]]] + +[[8,[[],[2,9,6,2,0],[6],0,2],[],2,[2]],[[4,1,2,[2,1],[4,9,0,9]]],[[1,[3,4]],[[4,0],3,3],0,10],[2]] +[[[7,4],3,4],[[],0,[[10,3,4,0,3]],[[1,5,6,5],[],10]],[[4,[6,4],[0],5],2,[7],[5,[4,5,9,4],[2,8,0]],4],[0,[2,8,2,[8],[]]],[[[2,8],[5],9,[]],[[8,9,2],[9,3],[9,9,3,2,7],5,7]]] + +[[10,[6,[3,2,2,7,8],[3,3,9,4,4],10],4,[7,0,[7,8,4,6,2]],1],[7,[5,[4,0],[7]],[6]],[9,5,8,[[4,7,0,10,8],5],[]]] +[[[[],[],[1,6,6,9],6,[3,0,2]],7,[8,[6],[2,6],1],[[10,1,5,1,9],1,[]]],[5,2,10,[4,[3,6]],7],[[0,[3,2,8,5],[10,1,8,8,1]],8,0],[[[3],[7,8,6],[10,0],[2,5,1,1,9],8],[8,5,[0,10],1],[[3,6,1],5,[2],3,[0,9,0,2]],8,[[]]],[2,[3,[6,2,9],[7,6,8,5]],[2,9,2,1]]] + +[[6,6,7],[],[10,2]] +[[[4,[0],1,1,6],10,5,[8,9,8,6],8]] + +[[[6,[10],[9,10,7],0,8]]] +[[1,8,4,[[8,9,1,6,0],[]]],[4,[[9],4,10,7],2,3]] + +[[[1,[6,3,6,5,3],6],1,0,[[],5,7,6]],[[],6],[],[]] +[[[9,[1,4,10,10]],[4,6,4,[8]],10]] + +[[[],7,[1],[],[0,[3,0],[4,7,5,1,3],[]]],[]] +[[[[7],5,9,9],5,3,7],[[[1,3,2,0],1],0,[[8,7,0,5,2],8,3,[7,7,3,4],9],3],[],[3],[6,[[3,9,3,0],[4,10],[]],[10,0,9,[],10],[],[[1,5],7,9]]] + +[[[3,10,10,3,3],[3],[1,0,4,2,2],6,3],[[9,[10],9,8],[[9,1,4,0,4],2,[4,8],[10,7,7],10],3,9,1],[[5,5]]] +[[8,2,7,0],[9,6,[2,[5,5],1,[7,10,6],[10,8,3,7]]],[]] + +[[[[2,7,3],8,8,[10,10,9,6,4],[]],7],[5],[],[],[]] +[[4],[[[],[5,4]],1],[2,[],5,[[10,0,4,5,9],5,[6,2,0,8],[4],8]]] + +[[0,5,3],[5,2],[[6]],[0],[[1,[6,10],10],[9,[4,7],3,[6],5],[4,[1,7,3,7,8]],[[5,10,1,1,3]],[8,[0,0,2,3],[8,1,7,0,3],[5,7]]]] +[[4],[],[8,4,1,[],1],[6],[]] + +[[[[7,8],[],[9,5,4],[6,9,3],4]],[8],[7],[4,7,[1,[],6,4],7,8],[]] +[[[[1],[]],6],[],[5],[5,[[1]],[[10],[1,8,4],6,[1]],4,[2,[0,5,6,7],4,7]]] + +[[],[9,[3]],[[10,10,[1],[],[4,2]],[[7,10,4]],[[4,0,3,3],1,[7,2]]],[]] +[[6],[[5,[3,3,5],[9,4,1,3,10],9],7,1],[4,10,7,4,9],[[[],[7,4,3,3,1],[],[2,8,4,10]]]] + +[[2,7,[5,8,9,[],4]],[1,[7,3,[0,1,0],4],5,6],[[]],[[[],10]]] +[[2,[[]],[],6,10]] + +[[[[2,6,2,6],7],[7,4],[7,[4,6]]],[[10,4,1,4],3,[]]] +[[[[1,6,9,9,7]]],[]] + +[[[]],[1,3,[2,[],[4,9],0],7,7],[[]],[[2,[0,5,7,1],[10,2,7,7,7],9,[]],7,5,1],[]] +[[[[],0,[8,3,3,10,10],[6,3,10,10,9]]]] + +[[9,[[7,5],8,[2,2,0,7]],[[],1,0]],[0,[3,3,8,[6,5,7,1,10],[9]]]] +[[],[],[],[7,8,10,6],[[4,[7],[]],[5,7],1]] + +[[2,[[5,6,3,9,10],4],[[10,4,4,8,10],5,[4,7,8,10],[],[7,4,2]],6,[[3,5],[0,5,1],8,7,10]],[[[9]]],[2,[[9,9,10],[0,10,1,4,8],[2]],1,[[1,9],1],[[],[6,0,6,9,5],0,[6,3]]]] +[[1,9]] + +[[1]] +[[[],5,8],[0,[6],8,6,4],[],[]] + +[[[[8,7],[8,3,10],5],[],3],[7,9],[6,1],[7],[[[1,8],9,[9,5,4],4],[[6,5,1,4,7],[5,5,2,2],[8]]]] +[[[[7,8,9],[2],9,[10,4,7],0],[[2],[1,7,4],[2],6,[6]],9],[[[1,9],[10,1,6,7,8],2,[10],[8,9,9]],[7],[2]],[8,2,3,3,[[9,2]]],[10,[[],[6,3,0,0]],7],[[[2,10],7,[5,7],8],4,7]] + +[[0,[[2,7,8,8],[5,5,6,2,2],[5,10,2,4],10,[]],[[2,1,9,6]]],[]] +[[3,[1],[[4],0,[7]],[[],5,1,0]],[[6],[5]],[[[3,1,4],[]],6,6,9],[2,[[4,7],6,[9,0,9,0,5],[8,10,4,4]],10,6,1]] + +[[8,9],[],[2,7,10,[[],2]],[[[],9],[[],[7,1,5,3,5]],0,4],[]] +[[7],[[]]] + +[[[[3,7],[2,9],9],1]] +[[10,[[8],[6,2],1,4]],[4,[6,[5,5]]]] + +[[6,8],[[[3,7,10],[5],5],0],[5,4,0,7,9]] +[[7]] + +[[[4]],[],[8]] +[[8,5],[[8,[10,1,3],[9,7,8],[9,10,7,10]],9,[[2,2,5,2],6,[0],[7,10,3]],9,[]],[4,[[8,7,1,5],[0,1],[6,7,6]],[[10,9,10]],[[]],[[8]]],[[5,[1,8,7],[7,7,0,9]],7,7,[],[7,8]]] + +[[[[10,9,9,1],5,7,3,[3,10,3]],5,10,5,0]] +[[1,[[],4,[],5],3]] + +[[2,[8]],[[],0,7,7],[7,[[],[6,9,7],0],5],[8,[5,[1,1,2],[1,0,6,1,0],2,[4]],6,[7,7],[10,[9,6,8,1,7],[]]],[1]] +[[4,10,[],[[9],[8]],[9,[10,3,4,4,3]]],[6,[[2,9]],[],8,4],[[6,[],7,10]]] + +[[],[2,9,10,7,2],[5,[],[[6,4]],[[2,1,9,6],4,[0,0,1,2],4,10],1],[[[8,7,1],3,4],10,[]],[10,[1,[9,4,0,4,9]],9]] +[[[[],[9,4,6,3],[7],5],[3,8,[5,0,0],[2]],0,[[0]]],[[10]],[8],[2,[1],5,4],[8,6,9]] + +[[[],[[9,7,2,7]],[[9,7,3,6,3],7,0,[7,3],0],4,[7]],[[[1],[6,6],3,9,[]],[],5,1],[[7,[],8],9],[8,[[2,4,6,5,8]]],[7]] +[[10,[]],[[[1,9,7,8],0,9,[1,10,9,8],[7,10,10,9,3]],[5,[3,2,5,8,1],[]]],[]] + +[[8,7,[],8,[[2,9,1],[3,6,3,7,0],[9,2],[],[1,3,4,10]]],[[8,4,6,4]],[[7,[2,2,10],[5],8],[],8,10]] +[[6,1]] + +[[],[7,[[2,4,5,8],[7,5,9,0,5],[8,1],[6,7,9,7],[5,8]],[[0,8],[9]],[6,[],[4],[3,6,8,8],[5,10,1]],[3,[9,5,0,2,0],6,[10,3]]],[5,[],7,[[1,1,7],[6,5,3,5,2]],[[0,1,4,3,6],[9,1,5,4,6],[7,3],6,7]]] +[[8],[[[7],10,8],[[10,5,8,8,3],0],[1,9],8]] + +[[1,[[6,8],5,[1,7,7],6,[3,0,1]],6,[5,[3,7],8]]] +[[3,[],10,[[1,1,8],6,3],[3,9,6,4,[2,9,10,6]]],[],[8,[[8,5,6],[],[],[5,7,6]],[6,[9,6,1,5,2]]],[]] + +[[8,[[],2,[5,7]],[5,[6,10]]],[[[1,5]]],[0,4,0],[5,[8],[[1,10,1],0,10,[3,10,5,6,1],[9,5,6]],0],[[[],[10,9,4,9,2]],[8,7,5],1,6]] +[[1,9,[[3],5,10,2],[3,[3,3],[1,9,10]],[]]] + +[[7],[6,0,[7,10],[9]],[],[]] +[[9,7,[1,8,[],9],7,0]] + +[[0,7],[7,2,2],[8,[[3]]],[[[8],1],7],[1]] +[[[[8,1,1,9,2],[5],9],[[3],[5,6,9],1,[]],[],0],[[],5,3,[0,0,3]],[[4,6,6,6,[8,1,8,6,4]],[8,4,[3,10,0,2]]]] + +[[[[10,3,4],0,[]],6,3],[],[[10,[],[0],9,1],5,[[6,2],[5,3,1],[10,3,7,9]]],[[8,7,4,2,0],[1,[10,10,0,1],1,[7]],1]] +[[1,[[4,8,4,0,1],9,5]],[[[1],6],8,[4,[4]]],[3,10,[0,1,[7],7],[6,[],6],[7]]] + +[[[0,0,[8,10,2,3],[6,2,5,6,7]],[9,6,10,1,[1,4,5]],[3,[5,8,3],10,10],4,[5,10,[],[9,2,10,2]]],[6,[5],[[5,6,1],[7,7,6],[0,7],4]]] +[[[[2,10,9,7,0],[8,0],[1,9,3]],[]],[[4],8,7],[],[10,10,10,[],5],[[2],[8,3],8,[1,[5],1]]] + +[[0,6,3],[6,[3,[8],3,1],[]],[3],[[],[8,6,4,[]],[[7,1,2,7,9]],[10,[10,2],[3,7,5],6]]] +[[[1,[10,3,4,2,6],6],[9,3,0,6],[9,[],[6,7,7,1,2],[6]],[[10]],1],[[]],[8,0],[],[8,[[9],7]]] + +[[5,[[8,3],10,9,[8,9,7,4],0]],[],[3,3,7],[2,[5],[4,4,[9,3,5]],3]] +[[[5,[6,9,0,1]],8,4,5,3]] + +[[[7,[],[],6,[4]],[],7],[[],10,5],[[[1,2,5],2,[6,1,3,3]],[[5,4,10,2,6],[1,9,9],10],8,1,9],[8,7]] +[[[5,[7],[9],1,7],6,9,[8],[[],[10,7,9,8,9],7,7,[3,7,10,2,7]]],[[10],[],[],[[2,0,8]],[[],0]]] diff --git a/2022/day13/main.clj b/2022/day13/main.clj @@ -0,0 +1,3 @@ +// each array is valid clojure +// read in each line, group pairs, process +// add indices of pairs in the correct order diff --git a/2022/day2/input.txt b/2022/day2/input.txt @@ -0,0 +1,2500 @@ +B Z +B X +C Y +B Y +B Y +A X +A X +B Z +A Z +B Z +B Y +B X +C X +B Y +A Z +B Y +A X +B X +C Y +B Y +B Y +C Y +B X +B X +C X +B Y +B Y +B Y +C Y +C X +B Y +C X +C X +B Y +B Z +C Y +B Y +B Z +B X +B Y +B Y +C Y +B Y +C Y +C Y +A Z +B X +C X +B Y +B X +C X +A X +B Y +C Y +B X +C X +C Y +B X +B Z +B Y +B X +C Y +B X +B Y +B Y +B Y +B Y +A X +A Z +B Z +B Y +C X +B Y +B Y +C Y +C Z +C Z +C X +B X +C Z +B Y +B Y +C X +C Z +C X +A Z +C Y +B Y +B Y +B Y +C Y +C Y +C X +C X +C Z +A X +B Y +C X +B Z +B Y +C X +B X +B Z +A Z +C Y +B Z +C X +C X +B Y +B Y +B Y +C Y +C X +B X +B Y +A Z +A Z +C Y +B Y +C Y +C Y +C Y +B X +A Z +C Y +C Y +A Z +A Z +B X +B Y +B Z +A Z +B X +B Y +C Z +C Z +B Z +B Y +B X +A Y +C Z +C X +A Z +A Z +B Y +B Y +C Y +C Y +B Y +B Y +B Y +A Z +C Y +C Z +C X +A Z +B X +B Y +A Y +A Y +B Y +B Y +C X +B Y +B Y +C Z +B Y +B Y +A Z +C Y +B X +C X +A X +C X +B Y +B X +A Z +C X +C Y +A Z +B Y +A Z +B X +B X +A Z +B Y +C X +C X +A Z +A X +C X +C X +C X +B Y +C Z +C Y +C X +B Y +B Y +B Y +A X +A Z +C Y +B Y +B Y +A X +C Y +C Z +C Y +C X +B Y +B Y +B Y +B Y +A Z +B Y +A Z +B X +B Y +B Y +B X +C Y +A X +A Z +B Y +C Y +C Y +B Y +B Y +C X +B Y +B X +A Z +B Y +B Y +C Y +B Y +B Y +B Y +A X +B X +B Z +C Y +B Z +C X +C X +B Z +B Y +A Z +A Z +B Y +C X +A X +C Y +B Y +B Y +A X +B Y +A Y +C Y +B Y +C X +B Y +A Z +B Z +C Y +B Y +B Y +C X +B Y +B Y +B Y +C Z +A X +B Y +B X +C X +C Z +C Z +C Y +C Y +A X +C Y +B Y +A X +C X +A Z +C X +B Y +C Z +C Z +A Z +A X +C Y +C X +B Y +C Z +B Y +C Y +C X +C Y +B Y +B Y +B Y +A X +A Z +B Y +B X +B X +B Y +B Y +B Y +A Z +B Y +B Y +A X +A X +A Z +A Z +B X +C Y +A Z +B Y +B Y +C Z +C X +C Y +A Z +C Y +C Y +C Y +C Y +C X +A Z +C Z +A Z +C Y +C X +B Y +B Y +C Z +B X +B Z +C X +A Y +C Y +B X +B X +B Y +C Y +A Z +A Z +B X +B Z +B X +C Y +A X +A X +C Z +B Y +C Y +C X +B Y +A Z +B Y +C Y +B Z +C Z +C X +B Z +C Z +B Z +A X +C Y +A Z +B Y +B Y +B Y +B Y +B Y +B Y +A Z +C Z +B X +C X +C Y +C Y +B Y +C X +C X +A Y +C Y +C Y +A Z +C Z +B Y +B X +C X +B Y +C X +B Y +B Y +C X +B Y +B Y +B Y +A Z +C Y +B Z +B Y +C X +B Y +C X +A Y +A Z +B Y +B Y +B Y +A Z +B Y +B Y +B Y +B Y +B Z +C Y +B Z +B Z +A Z +B Y +B Y +C X +A X +B Y +A Z +C X +C X +B Y +B Y +A X +B Y +B Y +C X +B Y +C X +B Y +B Y +B Y +B Y +A X +B Y +B Z +B Y +A Z +C X +C X +C Y +A Z +C Y +B Y +B X +A X +B Y +A Z +B Y +C Y +B Y +B Y +C Y +C X +A Z +A X +A Z +C Z +B Y +B Y +C Y +B X +C X +B Y +A Z +B Y +C X +A X +C X +C Y +C Y +B X +B Y +B Y +C Z +B Y +C X +A Z +C Z +C Y +A Z +C X +C Y +C Y +C X +B Y +B Y +C Z +A Z +B Y +A X +B Y +A X +A X +C Y +C X +A Z +B Y +C Y +C X +B Y +B Y +B Y +B X +B X +A X +C X +B Y +C Y +A Z +A Z +A Z +B Y +A Z +B Y +C X +B Y +C X +C Y +A Z +B Y +B Z +A Z +B Y +A Z +B Y +B Y +C Z +B Y +B Y +A Z +A X +C Z +C Z +B Z +B Y +A Z +B Y +B Y +B Y +B Y +C Z +A Z +C X +B X +B Y +A X +A X +B Y +A Z +A X +C Y +B Y +C X +A Z +B Y +C Z +C Y +B Y +A X +C Y +B Y +B Z +A Y +B Y +B Y +A Z +B Y +C Z +C X +B Y +A Y +C Z +B X +A X +B Y +B Y +B Z +B Z +C Y +B Y +B Y +A Z +A Z +B Y +B X +C Y +C Y +B Y +B Y +B Z +B Z +B Y +C Y +A Z +B Y +A Z +B Y +B Y +A Z +A Z +C X +C Z +B Y +C X +B X +A Z +B Y +B Y +C X +B Y +B Y +B Y +B Y +B X +B Y +A X +B Z +A Z +C Z +B X +A Z +C Y +C Y +A Z +B Y +C Z +C Z +C Y +B Y +B Y +B Y +A Z +B Y +B X +C X +B Y +B Y +B Y +B Y +C Y +B Y +C Y +B Y +C Y +C X +A Z +C Y +C Z +C Z +B X +C Y +B Y +B X +C Y +B Y +B Y +C Y +C X +C X +A Z +B Y +C X +A Z +C Z +B Y +B Y +C X +C X +B Y +B X +C Y +B Y +B Y +A Z +A Z +C Z +A Z +B Y +C X +C X +B X +B Y +B Y +B Z +B Y +B Y +B Y +B Y +B Z +B Y +C Y +C Z +B Y +C Y +C Y +C Y +C Y +A Z +A Z +C Y +C Y +A X +B Y +C Y +A X +C X +C Y +A Z +B Y +B Z +A X +B Y +B Z +B Y +B Y +B Z +C X +C Y +B Y +B Z +B X +A Z +B Y +C Y +B Y +B Y +A X +C X +B Y +C X +B Y +C Y +A X +A X +A Z +C Y +B Y +C Y +C X +B X +C X +C X +A X +A Z +B Y +B Y +B Y +A Z +B Y +B Y +B Y +B Y +B Y +B Y +B X +B Y +C Y +B Y +A Z +B Y +C X +C Y +B Z +C Z +B Y +A Y +C X +B Y +B Y +B Y +C X +A Y +C Z +B Y +C X +C Y +C Y +C Y +C X +C Y +B Z +B Y +C Y +C X +B Y +B Y +B X +C Y +B X +C Z +B Y +C X +B Z +C X +B Y +C X +B Y +C Y +C Y +A Z +C Y +C X +B Y +C X +B X +A Z +B Y +A Z +A Z +A Z +C Y +B X +A Y +C Y +B Y +B Y +C Y +C Y +C X +B X +A X +A Z +C X +A Y +B X +C Z +B Y +B Y +B X +C X +B Y +B Y +B Y +B X +B Y +C X +B Y +B Y +C Z +C Y +B Y +C Y +B Y +A Z +C Z +A Y +B Y +B Y +B Y +C Y +C Y +C Y +B Y +C Z +B Y +C Z +A Z +A Z +C Z +C Y +C Z +C Y +A Z +C X +B X +B Y +C Y +B Z +C Z +C X +B Y +A Z +B Y +C X +B Z +A X +B Y +A Z +C Z +C X +C X +C Y +B Y +B Y +A Z +A Z +B Y +B X +A Z +B Y +C Y +A X +C Z +B Y +B X +B Y +C Y +C Z +B Z +A Z +B Y +A X +C X +B Y +A Z +C X +B Z +C Y +C Y +C X +C X +C Y +B Y +B Y +B Z +B X +C X +B Z +C X +B X +C Z +C Z +C X +B Y +C Y +C X +B Y +A Z +C Y +C Z +C Z +C Y +B Y +A X +C Z +C X +B Y +C X +C Z +B Y +C Y +B Y +C Y +A Z +B Z +C Z +C X +B Z +B Z +B Y +A Y +C Y +C Y +B X +B X +B Y +B Y +B Y +C X +B Z +B Y +B Y +B Y +C X +A X +C Y +A X +B Y +B Y +B Y +B Y +B Y +C Y +C Y +B Y +B Y +B Y +B Y +C Y +B Y +C X +B Y +B Y +C X +A Z +A X +C Y +C Z +B Y +C X +B Y +B X +A Z +B X +B Y +B Y +A Z +B Y +B Y +B Y +B Z +A Z +A X +B Y +A Z +C Y +B Y +C X +B Y +C Y +B Y +B Y +C X +C Y +A Y +C X +C Y +B Y +A Y +A Z +C Z +A Y +A Z +B Y +C Y +C Y +B Y +B Y +B Y +B Y +A Z +B Y +B Y +B Y +C X +C X +B Y +C X +B Y +A Z +B Y +B X +C Z +C Y +A Y +B Y +C Y +B Y +C Y +C Y +C Y +C X +C Y +B Y +B Y +C Y +B Y +C Y +A Z +A X +B Y +A Z +B Y +C X +C X +B Y +C Z +B Y +B Y +C Y +B Y +C Z +A Y +B Y +C Y +B Y +A Z +C Y +B Z +C Y +C Y +C Z +B Y +C X +B Z +B Y +B Y +B Y +C Z +B Y +B Y +B Y +A Z +C X +B Z +B Y +B Y +C Y +B Y +C X +B Y +B Y +C Y +C X +C Y +B Y +B Y +C Y +B Y +A Y +B Y +A Z +B Y +B Y +B X +A Z +B Y +B Y +C Y +C X +C Z +A Z +A Z +C X +B Y +C Y +B Y +C X +B Y +B Y +C Y +C X +B Y +B Y +B Y +B Y +B Y +C X +B Z +B Z +A Z +B Z +B Y +B Z +B Y +C Y +A Z +A X +B Y +C Z +B X +A X +C Y +B Y +B Y +C Y +B Y +C Y +B Y +A Z +B Y +B Y +A Z +B Y +B Y +A Z +B Y +B Y +B Y +B Y +C Y +B Y +A X +A X +B Y +B Y +A Y +C Z +A Z +A Z +B Y +A Z +C Y +B Y +B Y +B Y +B Y +C Y +B Y +A X +B X +B Y +B Y +B Y +B Y +B X +C Y +B Y +B Y +B X +C Y +A Y +B Y +B Y +B Y +A X +B Z +C X +A X +C Z +B Y +B Y +B Y +C X +B Y +B Y +B Y +B Y +C Y +B Z +B Z +B Y +B Z +B Y +B Y +C Z +C Y +B Y +C Z +C Z +B Y +C X +A Y +B Y +B Z +A Z +B Y +C X +B Y +B Y +C Z +A Z +B Y +B X +B Y +C Y +B Y +B Z +B Y +B Y +C X +C X +C Z +B Y +B Y +B Y +A Z +A X +B Y +A Z +B Y +B Y +B Y +B Z +C X +C Z +B Y +B Y +B Y +C Z +B X +C Z +A X +B Y +C Y +B Y +A Z +A X +C X +B X +A Z +C X +B Y +C Y +B X +A Z +C Y +C Y +B Z +C X +B Y +C X +C Z +C Y +B Y +C Z +B Y +B Y +C X +B Y +B Y +B Y +B Y +B Y +B Y +C X +B Y +B Y +B Y +A Z +B Y +B Y +C Z +B Y +B Y +B Y +C X +A Z +B Z +C Y +C Y +B Y +A X +C Y +B Y +C Z +B Z +B Y +B Y +C Y +B Y +B Y +B Y +C Z +A X +B Y +C Y +A X +B Y +B Y +A X +B Y +A Y +C X +A Z +A Z +C Y +B Y +C Z +C Y +C Y +C Z +A Z +A X +C Z +B Y +C X +A Z +C X +B Y +B Y +B Y +C Z +C Y +C X +C Y +A Y +C X +A Z +A Z +B Z +C X +B Y +B Y +C X +A Z +C Z +C Z +B Y +B Y +A Y +C X +B Z +B Y +B Y +C X +C X +C Z +C Z +B Y +B Y +B X +B Y +B Y +C Z +C Y +C Z +B Y +B Y +B Y +C Y +B X +C X +A Z +C X +C X +C Y +B Y +B Y +C Y +B Z +B Z +C X +C Y +B Y +B Z +B Y +B X +A Z +C X +B Z +A Z +C X +B Y +C Y +C Y +B Y +B Y +B Y +B X +A X +B Y +A X +A Z +C Y +B Y +B Y +B Y +B Y +B Y +C Z +B X +B Y +C X +A Z +B Y +B Y +A Z +B Y +C Y +C Z +C X +C Y +B Y +B Y +B Z +B Y +A X +C Y +B Y +A X +B Y +C X +A Z +B Y +C X +A Z +B Y +C X +C Y +C X +B Z +C Z +B Y +C Y +C X +C Y +B Z +B X +A X +C Z +B Y +B Y +C Y +B Z +C X +C X +B Y +B Y +C Y +B X +B Y +C Y +B Y +B Y +A Z +B Y +B Y +B Y +A X +A Z +B Z +B Z +B Y +A Z +B Y +C Y +B Y +C X +B X +B Y +B Y +B X +C Z +B Y +C Z +C X +B Y +C Z +B Y +C Y +B Z +C Y +C Y +C Z +C Z +A X +B Y +A Z +B Y +B X +A X +B Y +B Y +B Y +C Y +B Y +B X +B Z +C Z +B X +B Z +B Y +C Y +B Y +B Z +C X +A Z +B X +B Z +C X +C Z +B Y +A Z +C X +C Y +B Z +B Y +C Y +B Y +C Y +B Y +B Y +B Y +C Y +B Y +C Y +C X +C Z +B Y +B Y +B X +C Z +B X +A Z +C Y +A Z +C X +C Y +B Y +C Z +B Z +C Z +C Y +C X +B Y +C Z +C X +B Y +B Y +B Y +B X +B Y +B Y +C X +A X +B Z +C X +C Z +B Y +C Y +B Z +C Z +B Y +C X +B Y +B Y +A Z +C Z +B Y +C Z +C X +B Y +B Y +C X +C X +C Y +B Y +A X +A Z +B Y +C X +B Y +B Y +C Y +B Y +A Z +A Z +C X +C Z +C X +C X +A X +B Y +B Y +C Y +C Z +C Y +B Z +C Y +B Y +B Y +B Y +C Y +B Z +B Y +B Y +B X +C Y +C Y +B Y +A X +C X +A X +C Z +C Y +A Y +B Y +B Y +B Z +C X +C X +B Y +A X +B Y +A Z +B Y +A Z +C X +C X +B Y +B Y +B Y +B Y +A X +B Y +B Z +C Y +C X +C Z +C Z +B Y +C Z +B Y +B Y +C X +B Y +A X +A Z +B Y +A Z +C X +B Y +C Y +B Z +C Z +B Y +B Y +B Y +B Y +C Y +C Z +B X +B Y +A Y +C Y +B Z +B Y +C Y +A Z +B Y +B Y +C Y +C Y +A Y +C Y +A Z +C Y +B X +B Y +B Y +C Y +A Z +C Y +A Z +B Y +B Y +B Y +B Y +A X +C Y +A Z +B Y +B Y +B Y +B Y +B Z +C Z +C Y +C X +B Y +C X +B Y +B Y +B Y +B Y +A Z +B Y +C Y +C Y +A Z +A X +B Y +C Y +C Y +B Y +C Y +C Y +C Y +C Z +A Z +C X +C Y +C X +B Y +B Y +C X +C Z +C X +C Z +B X +B Y +C Z +B Z +A Z +C Y +B Z +C Y +B Y +A X +B Y +A Z +B Y +A X +B Y +B X +B Z +B X +B Y +B Y +B Y +C X +B Y +B X +C X +B Y +B Z +B Y +C Y +B Y +B Y +C X +A Z +C X +B Y +C Y +C Z +B Y +C Z +B Y +B Y +B Y +B Y +C Z +C X +C Y +C Z +B X +A Z +A Z +B Y +B Z +C Y +A X +B Y +B Y +A Z +B Y +B Y +B Y +B Y +A Z +C Z +B Z +C Y +A Z +C Y +B Y +B Y +B Y +B Z +B Y +C X +C Z +B X +C X +B Y +C Z +C X +B Y +A Z +A Z +B Y +B Y +B Y +C X +A Y +B Y +B Y +A Z +A Z +C Z +C Y +B Y +B Y +A X +B Z +A X +B Y +A Z +B Y +C X +B Y +A Y +B Y +B Z +B Y +B Z +B Y +B Y +C Y +C Y +B Y +C X +C Z +B Y +B Y +B Y +C Z +A Z +C X +B Y +B Z +B Y +C Y +B Y +C X +B Y +B Y +C Y +B Y +B Y +B Y +B Y +B Y +B Y +B X +B Y +B Y +C X +B Y +B Z +A Z +A Z +C X +B X +B X +C Y +C Z +B Y +C X +A X +B Y +C X +C Z +C X +B Y +B Y +C Y +B Y +B Y +B Y +A Z +C Z +C X +B Y +B Y +A X +B Y +C Y +B Y +B Y +C Y +B X +B X +A X +C Y +C Z +C Y +B Y +B Y +C X +A X +A X +C X +B Y +C X +B Y +B Y +B Y +C Y +A Z +B Z +A Z +B Y +B X +C Y +B Z +B Y +A Z +B Y +C Y +B Y +C Y +A X +B Y +B Y +B Y +C X +A Z +C Y +B Y +B Y +B Y +C X +B Y +C Y +B Y +B X +C X +B Y +B Y +C Y +C X +C Z +C Z +B Y +B Y +B Y +B X +C X +B X +A Y +A Z +C Y +A X +B Y +B Y +B Y +A Z +C Y +C X +C Y +C Z +A Z +B Y +B Y +B Y +B Y +A Z +B Y +A Z +B Y +B Y +C Y +C Z +C Y +A Z +C Y +B Y +C Y +B X +B Y +A X +C Z +C Z +A X +C X +C Z +C X +C X +B Y +A X +A Z +C Y +B Y +C X +B Y +B Y +B X +C Y +C Z +B Y +B Z +A Z +C Y +B Y +A Z +B Y +C X +C X +B Z +C Y +B X +B Y +C Y +B Z +A Z +C X +C Y +C X +C Z +B Z +C Z +B Y +B X +B Y +B X +B Y +B Y +B Y +B Y +C X +C X +C Y +B Z +C X +B Y +B Y +B Z +C Z +C Y +B Y +B Y +B Y +C X +B Z +B Y +B X +B Y +C Y +C X +C Z +C X +C Y +B Y +B Y +B X +B Y +C X +B X +A X +B Y +C Y +B Y +A X +C Y +B Z +B Z +C X +C Z +C X +B Y +C Y +C Y +B Y +C Z +B Y +C Y +B Y +C Y +A Z +B Z +C X +C X +B Y +B Y +B Z +B Y +B Z +A X +C X +C X +B Y +C X +A Z +C Y +B Y +A Z +C Z +B X +C Y +C X +C X +A Z +B Y +B Y +C Z +B Y +A Y +C Y +B Y +B X +B Z +C X +B Y +B X +C Z +B Y +B X +B Y +B Y +B Y +A Z +A Z +B Z +A Z +C Y +C Z +B X +C X +A Z +C X +B Y +B Y +A X +B X +B Y +B X +B X +A Y +A Z +C X +B Z +B Z +C Y +C Y +B Y +B Y +B Y +A Z +A Z +B Y +B Y +C Y +C Y +C Y +C Z +C X +C Y +B Y +B X +B Y +A Y +A X +C Y +B Y +A Z +B Y +B Y +C X +C X +B Y +B Y +C Y +A Z +B Y +C Y +C Z +C Z +C Y +A Y +B Y +B Y +C Y +B X +C Z +C Z +C Z +B Y +B Y +B Y +C Y +A Z +B Y +A Z +B Y +A Z +C X +C Z +C Y +B Y +A Z +B Y +B Y +C Z +B Y +C X +B Y +C Y +C Y +B Y +B Y +A X +C Z +B Y +C Y +C X +B Y +B Y +B Y +B Z +A Z +B Y +B Y +B Y +A Z +B Y +A X +B Y +B Y +B Y +C Y +C X +C Y +A Z +B Y +C Y +B Y +C Y +C Y +C Y +A X +C Z +B Y +B Y +C Z +B Y +B Y +C Z +B Z +B Y +C Y +B Y +B Y +B Y +B Y +B X +C X +C Y +B Y +A Z +B Y +A X +B Y +B Y +B Y +B Y +C Z +C Z +B Y +A Z +B Y +C X +C X +C Y +B X +A X +B X +B Y +C X +C Z +C Y +C Y +B Y +C Z +B Y +B Y +C Z +A X +B Y +C Z +B Z +B Y +C X +C X +B Y +B Y +B X +C Z +A Z +A X +B Y +C X +B Y +B Y +C Z +B Z +C Y +B Y +B X +C X +C Z +B Z +B Z +C Y +B Y +A X +B Y +B Y +C X +B X +A Z +B Y +A Z +B Y +B Y +A Z +C X +C X +B Y +B X +B Y +A Z +C Y +C Z +B Y +B Y +A X +B Y +C Y +C Y +B Y +A X +B Y +B Y +A Z +C X +C X +C Y +B Y +A X +B Y +B Y +C Y +B Y +B Y +B Y +B Z +B Y +C Y +C Y +B Y +C X +C X +B Y +C Y +B Y +C X +B Y +B Z +B Y +A Z +A Z +C X +A Z +A Z +B Y +B X +C Z +B Y +B Y +B Y +B Y +B Y +C X +C Y +B Y +A X +C X +C X +C Y +B Y +C X +C X +C Z +B Z +C Y +C X +B Y +B Y +C X +B X +C Z +B Y +B Y +C Y +A Z +C Y +C X +C Y +B Y +B Y +C X +C Y +C Y +C X +B Z +B Z +B Y +B Y +C Y +B Y +C X +A Z +A X +C Z +B Y +C Y +C Y +B Y +B Y +C Y +B Y diff --git a/2022/day2/main.c b/2022/day2/main.c @@ -0,0 +1,78 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <err.h> + +int +main (void) +{ + FILE *fp = fopen("input.txt", "r"); + if (fp == NULL) + { + printf("Failed to open file %s", "input.txt"); + exit(EXIT_FAILURE); + } + + int totalScoreFirstPart = 0; + int totalScoreSecondPart = 0; + + char *line = NULL; + size_t linesize = 0; + ssize_t linelen = 0; + while ((linelen = getline(&line, &linesize, fp)) != -1) + { + // Lazy string parsing + char opponentMove = line[0]; + char myMove = line[2]; + + // Super good code + if (opponentMove == 'A' && myMove == 'X') + totalScoreFirstPart += 1 + 3; + else if (opponentMove == 'A' && myMove == 'Y') + totalScoreFirstPart += 2 + 6; + else if (opponentMove == 'A' && myMove == 'Z') + totalScoreFirstPart += 3 + 0; + else if (opponentMove == 'B' && myMove == 'X') + totalScoreFirstPart += 1 + 0; + else if (opponentMove == 'B' && myMove == 'Y') + totalScoreFirstPart += 2 + 3; + else if (opponentMove == 'B' && myMove == 'Z') + totalScoreFirstPart += 3 + 6; + else if (opponentMove == 'C' && myMove == 'X') + totalScoreFirstPart += 1 + 6; + else if (opponentMove == 'C' && myMove == 'Y') + totalScoreFirstPart += 2 + 0; + else if (opponentMove == 'C' && myMove == 'Z') + totalScoreFirstPart += 3 + 3; + + // Super DUPER good code + char outcome = myMove; + if (opponentMove == 'A' && outcome == 'X') + totalScoreSecondPart += 3 + 0; + else if (opponentMove == 'A' && outcome == 'Y') + totalScoreSecondPart += 1 + 3; + else if (opponentMove == 'A' && outcome == 'Z') + totalScoreSecondPart += 2 + 6; + else if (opponentMove == 'B' && outcome == 'X') + totalScoreSecondPart += 1 + 0; + else if (opponentMove == 'B' && outcome == 'Y') + totalScoreSecondPart += 2 + 3; + else if (opponentMove == 'B' && outcome == 'Z') + totalScoreSecondPart += 3 + 6; + else if (opponentMove == 'C' && outcome == 'X') + totalScoreSecondPart += 2 + 0; + else if (opponentMove == 'C' && outcome == 'Y') + totalScoreSecondPart += 3 + 3; + else if (opponentMove == 'C' && outcome == 'Z') + totalScoreSecondPart += 1 + 6; + } + free(line); + if (ferror(fp)) + err(1, "getline"); + fclose(fp); + + printf("PART 1: Your total score after following the strategy guide: %d\n", totalScoreFirstPart); + printf("PART 2: Your total score after following the real strategy guide: %d\n", totalScoreSecondPart); + + exit(EXIT_SUCCESS); +} diff --git a/2022/day3/input.txt b/2022/day3/input.txt @@ -0,0 +1,300 @@ +NLBLfrNNLvqwbMfDqSjSzzSJjjggcdVs +lTRGPPZnRRHszcsZdSsccZ +CFTTFtFHTtCtDDzrmBtrBD +BJldgBWnRgWNWtllSlWShMcLcVSvVjbVVVvDVVVL +HFGFwqQPQGwHrTFpwmThMbDDVcVmLvvshj +HrpHrGPZZCQrfqlNdtMlzfMltlgn +hQLhBtBtQNQjBjNLvtLjzLJpWbjJdppSwjpCCplllJdj +FGFsmccSPTVPfVVHpJJgwlJwwWJWpCmR +sFPfPFHZTHScnzBttqzvQzqZ +MNTGMTnGWvTwwwnZhNZnWDPPdSjqsSPWjmBCSBWS +RJrtVfRlLrfHgblHJVBjqqFmjCdBJjDmJdSD +tgRftftRcRLftrpHpflHlctVwNNvZNcTwZnznQzwTzmhQwQh +sQPpQpQhnlNsJpJSQphHcZffL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NLjjS diff --git a/2022/day3/main-bitmap.c b/2022/day3/main-bitmap.c @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <err.h> + +int +determinePriority(char item) +{ + // Upper case A-Z (returns 27-52) + if (item - 96 < 0) + return item - 38; + // Lower case a-z (returns 1-26) + else + return item - 96; +} + +int +secondPart(FILE *fp) +{ + unsigned long firstRucksack = 0; + unsigned long secondRucksack = 0; + unsigned long thirdRucksack = 0; + int sumOfPriorities = 0; + int lineCounter = 1; + + char *line = NULL; + size_t linesize = 0; + ssize_t linelen = 0; + while ((linelen = getline(&line, &linesize, fp)) != -1) + { + switch (lineCounter) + { + case 1: + lineCounter++; + for (int i = 0; i < linelen-1; i++) + firstRucksack |= ((unsigned long)1 << (determinePriority(line[i])-1)); + break; + case 2: + lineCounter++; + for (int i = 0; i < linelen-1; i++) + secondRucksack |= ((unsigned long)1 << (determinePriority(line[i])-1)); + break; + case 3: + lineCounter++; + for (int i = 0; i < linelen-1; i++) + thirdRucksack |= ((unsigned long)1 << (determinePriority(line[i])-1)); + break; + } + + if (lineCounter > 3) + { + lineCounter = 1; + unsigned long commonItem = firstRucksack & secondRucksack & thirdRucksack; + for (int i = sizeof(unsigned long)*8; i >= 0; i--) + if (commonItem & ((unsigned long)1 << i)) + sumOfPriorities += i + 1; + firstRucksack = 0; + secondRucksack = 0; + thirdRucksack = 0; + } + } + free(line); + if (ferror(fp)) + err(1, "getline"); + return sumOfPriorities; +} + +int +main (void) +{ + FILE *fp = fopen("input.txt", "r"); + if (fp == NULL) + { + err(1, "Failed to open input.txt"); + exit(EXIT_FAILURE); + } + + printf("PART 2: The sum of of the priorities of the badges is: %d\n", secondPart(fp)); + fclose(fp); + + exit(EXIT_SUCCESS); +} diff --git a/2022/day3/main.c b/2022/day3/main.c @@ -0,0 +1,140 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <err.h> + +int +determinePriority(char item) +{ + // Upper case A-Z (returns 27-52) + if (item - 96 < 0) + return item - 38; + // Lower case a-z (returns 1-26) + else + return item - 96; +} + +int +firstPart(FILE* fp) +{ + char firstCompartment[64] = { 0 }; + char secondCompartment[64] = { 0 }; + int sumOfPriorities = 0; + + char *line = NULL; + size_t linesize = 0; + ssize_t linelen = 0; + while ((linelen = getline(&line, &linesize, fp)) != -1) + { + // The line we got includes the newline character at the end + int compartmentSize = (linelen - 1) / 2 ; + + // Split the line into the first and second compartments + strncpy(firstCompartment, line, compartmentSize); + strncpy(secondCompartment, &line[compartmentSize], compartmentSize); + firstCompartment[compartmentSize] = '\0'; + secondCompartment[compartmentSize] = '\0'; + + // I love O(n^2) + for (int i = 0; i < compartmentSize; i++) + { + for (int j = 0; j < compartmentSize; j++) + { + if (firstCompartment[i] == secondCompartment[j]) + { + sumOfPriorities += determinePriority(firstCompartment[i]); + // Break out of both loops + i = compartmentSize; + break; + } + } + } + } + free(line); + if (ferror(fp)) + err(1, "getline"); + return sumOfPriorities; +} + +int +secondPart(FILE *fp) +{ + char firstRucksack[64] = { 0 }; + char secondRucksack[64] = { 0 }; + char thirdRucksack[64] = { 0 }; + int sumOfPriorities = 0; + int lineCounter = 1; + + char *line = NULL; + size_t linesize = 0; + ssize_t linelen = 0; + while ((linelen = getline(&line, &linesize, fp)) != -1) + { + switch (lineCounter) + { + case 1: + lineCounter++; + strncpy(firstRucksack, line, linelen - 1); + firstRucksack[linelen-1] = '\0'; + break; + case 2: + lineCounter++; + strncpy(secondRucksack, line, linelen - 1); + secondRucksack[linelen-1] = '\0'; + break; + case 3: + lineCounter++; + strncpy(thirdRucksack, line, linelen - 1); + thirdRucksack[linelen-1] = '\0'; + break; + } + + // We have the next set of three rucksacks, now we compare + if (lineCounter > 3) + { + lineCounter = 1; + // Friendship ended with O(n^2), now O(n^3) is my best friend + for (size_t i = 0 ; i < strlen(firstRucksack); i++) + { + for (size_t j = 0; j < strlen(secondRucksack); j++) + { + for (size_t k = 0; k < strlen(thirdRucksack); k++) + { + if (firstRucksack[i] == secondRucksack[j] + && firstRucksack[i] == thirdRucksack[k]) + { + sumOfPriorities += determinePriority(firstRucksack[i]); + // Break out of all loops + i = strlen(firstRucksack); + j = strlen(secondRucksack); + break; + } + + } + } + } + } + } + free(line); + if (ferror(fp)) + err(1, "getline"); + return sumOfPriorities; +} + +int +main (void) +{ + FILE *fp = fopen("input.txt", "r"); + if (fp == NULL) + { + err(1, "Failed to open input.txt"); + exit(EXIT_FAILURE); + } + + /* printf("PART 1: The sum of of the priorities of the common item types is: %d\n", firstPart(fp)); */ + /* rewind(fp); */ + printf("PART 2: The sum of of the priorities of the badges is: %d\n", secondPart(fp)); + fclose(fp); + + exit(EXIT_SUCCESS); +} diff --git a/2022/day4/input.txt b/2022/day4/input.txt @@ -0,0 +1,1000 @@ +8-41,8-79 +1-71,2-71 +11-74,74-75 +44-96,43-96 +79-79,3-78 +67-86,50-93 +15-42,41-93 +21-98,20-99 +42-53,13-41 +51-80,38-79 +13-13,13-93 +14-20,20-66 +34-64,33-34 +93-94,56-93 +28-59,58-90 +9-58,4-8 +39-79,32-87 +60-62,48-61 +1-76,21-75 +1-11,2-12 +7-85,4-7 +5-69,26-69 +10-95,11-96 +73-99,65-97 +6-67,1-35 +26-48,29-44 +3-85,1-3 +51-53,14-52 +38-64,37-70 +19-30,30-55 +73-77,72-73 +28-65,28-66 +30-60,47-87 +32-32,30-34 +82-96,3-97 +12-63,13-63 +42-91,19-91 +38-45,45-80 +4-98,14-76 +22-91,23-90 +9-84,10-41 +79-98,48-92 +41-45,41-46 +15-94,30-89 +13-84,25-83 +60-68,37-69 +35-86,7-87 +32-78,31-78 +4-98,4-99 +20-20,20-93 +5-88,87-88 +9-42,4-12 +44-97,54-86 +32-82,6-26 +39-41,40-42 +10-76,10-75 +5-99,2-99 +48-48,47-99 +1-1,1-91 +60-75,59-61 +31-32,32-33 +21-88,22-88 +11-42,11-11 +57-75,74-95 +94-94,94-94 +60-84,58-86 +18-87,11-18 +14-34,29-33 +64-90,22-69 +33-95,71-96 +10-93,9-93 +18-18,18-68 +3-94,93-93 +38-38,38-78 +79-81,67-86 +24-73,3-73 +93-94,46-94 +10-97,11-35 +5-93,4-98 +3-99,47-97 +31-60,59-86 +1-67,45-68 +22-84,59-83 +24-72,23-73 +15-78,16-77 +90-90,85-92 +43-91,44-92 +2-88,86-89 +4-38,4-37 +43-87,44-87 +4-91,5-91 +23-37,27-46 +1-4,3-99 +77-77,78-81 +33-35,33-37 +80-87,81-81 +5-52,6-99 +66-66,52-66 +6-96,16-92 +16-89,4-98 +60-60,60-97 +5-62,1-6 +38-91,38-38 +4-14,13-57 +7-53,7-54 +1-97,13-97 +2-90,3-46 +19-19,20-96 +3-33,33-33 +28-42,29-42 +25-76,47-76 +16-24,25-44 +55-71,55-71 +29-76,28-89 +17-90,17-89 +1-98,10-99 +6-43,22-42 +7-95,67-95 +4-97,97-98 +62-94,62-95 +4-86,5-87 +6-83,6-89 +18-26,26-69 +95-98,10-95 +7-86,1-7 +91-92,27-91 +3-75,8-87 +38-53,30-52 +11-85,9-12 +39-83,39-83 +62-94,10-63 +12-35,35-97 +15-17,16-87 +2-93,1-92 +33-86,34-87 +4-19,19-92 +5-70,69-90 +10-97,5-97 +82-83,41-82 +16-29,16-29 +1-82,5-81 +62-71,61-72 +6-99,8-99 +10-44,6-10 +14-45,13-92 +36-37,37-91 +11-41,22-68 +51-78,79-95 +19-36,28-36 +4-91,9-92 +3-98,3-3 +1-44,2-43 +4-22,4-22 +24-89,42-89 +72-98,57-73 +28-92,4-29 +60-91,25-61 +2-89,5-88 +71-73,3-71 +17-99,24-98 +88-96,3-88 +39-74,75-75 +52-92,4-93 +18-56,53-97 +27-75,16-71 +81-94,1-82 +65-67,15-66 +77-79,77-79 +16-33,17-34 +83-87,29-84 +84-84,20-85 +26-28,27-82 +6-66,66-66 +8-72,9-59 +9-99,10-97 +48-48,47-47 +7-58,6-57 +46-58,23-70 +2-2,2-93 +20-33,20-20 +7-20,8-15 +17-67,16-17 +2-21,1-99 +5-5,4-72 +79-95,9-96 +7-38,7-38 +8-98,6-99 +68-84,10-68 +19-35,2-34 +5-43,3-9 +15-62,14-14 +82-95,48-96 +75-77,34-76 +33-65,65-66 +95-95,19-95 +12-88,13-87 +28-68,25-30 +12-37,8-91 +38-63,16-63 +41-58,53-57 +12-42,29-71 +68-96,68-96 +6-77,78-78 +22-37,23-36 +65-74,64-75 +36-88,35-99 +3-49,13-32 +84-91,1-84 +13-66,13-66 +9-92,10-91 +26-26,26-27 +10-17,15-35 +68-82,59-83 +27-69,26-58 +82-92,10-81 +12-18,12-18 +29-35,29-46 +60-71,71-71 +6-80,79-95 +2-12,1-3 +19-77,20-77 +95-97,88-95 +55-79,21-54 +3-4,9-97 +23-98,23-98 +1-4,3-47 +2-26,1-2 +22-72,9-22 +3-84,13-83 +43-45,18-44 +49-53,51-51 +15-85,16-85 +7-21,4-21 +1-24,1-25 +16-86,16-16 +79-87,80-86 +1-95,57-97 +4-99,29-99 +97-98,6-98 +46-78,53-83 +28-82,51-93 +59-95,32-96 +11-32,23-33 +13-29,24-47 +7-74,11-73 +28-36,36-89 +17-83,18-82 +2-85,10-66 +4-79,4-78 +36-62,61-79 +50-52,28-51 +22-48,3-28 +51-92,68-91 +29-91,28-92 +9-52,8-53 +7-97,55-96 +16-59,15-92 +98-98,3-97 +67-72,15-72 +15-52,5-77 +1-78,3-79 +6-66,3-3 +48-64,63-63 +6-97,6-97 +27-27,19-27 +2-72,1-71 +47-59,48-79 +12-61,13-65 +50-57,38-62 +30-73,65-70 +79-94,79-93 +81-97,98-98 +26-78,17-26 +46-57,29-89 +86-95,29-95 +57-64,43-64 +14-93,71-97 +4-99,66-98 +77-87,17-78 +25-26,26-96 +34-73,33-73 +36-57,35-62 +41-73,55-91 +41-48,12-47 +21-83,20-82 +15-35,14-73 +19-93,93-99 +50-69,51-95 +59-66,66-66 +5-93,92-92 +37-37,37-94 +38-39,38-54 +8-49,27-57 +43-83,79-82 +64-66,13-70 +40-92,92-97 +13-67,51-73 +29-88,30-71 +16-96,17-97 +25-37,27-77 +20-57,3-36 +91-96,63-99 +21-84,44-83 +4-87,2-4 +9-31,31-79 +33-82,33-33 +4-65,3-64 +18-71,19-22 +18-63,10-18 +87-92,88-93 +62-63,61-72 +20-49,49-68 +77-83,64-84 +10-79,8-11 +33-59,34-49 +5-85,11-85 +95-96,7-96 +59-60,35-59 +19-90,7-13 +16-84,16-47 +61-89,88-90 +24-52,51-51 +51-51,50-93 +39-77,18-96 +49-90,90-92 +37-76,75-76 +12-20,21-83 +2-94,3-95 +83-83,83-88 +3-99,2-97 +48-72,72-78 +14-99,13-99 +10-11,1-10 +4-99,5-92 +4-81,71-81 +5-5,5-93 +17-53,5-53 +38-91,37-37 +27-71,26-70 +1-98,73-82 +13-96,7-14 +14-82,14-14 +5-36,4-46 +9-75,9-93 +83-83,44-82 +34-95,23-35 +58-87,58-88 +70-98,69-69 +1-15,2-14 +22-22,22-24 +8-93,8-92 +50-60,2-55 +39-78,40-78 +55-92,31-92 +3-88,1-50 +23-65,23-66 +63-64,17-64 +15-31,11-91 +19-97,18-96 +12-89,12-88 +6-26,7-10 +45-70,45-69 +53-79,77-79 +31-63,30-30 +66-68,7-67 +22-85,43-85 +9-9,9-82 +1-30,4-80 +36-58,36-37 +15-84,14-83 +41-42,40-71 +38-73,37-98 +1-61,2-91 +71-73,71-73 +31-80,31-80 +12-99,13-99 +1-83,1-83 +9-92,9-93 +3-35,4-34 +15-64,19-63 +36-36,36-84 +15-43,14-57 +96-98,4-97 +36-46,45-79 +27-71,27-27 +6-6,6-53 +2-55,10-19 +8-48,2-53 +17-99,16-97 +13-64,12-65 +75-86,76-85 +63-64,3-63 +5-87,3-5 +56-91,41-91 +7-23,7-22 +3-88,3-86 +3-5,5-53 +1-94,2-95 +83-85,81-84 +98-98,82-99 +31-68,4-68 +5-95,12-96 +22-35,35-91 +1-93,3-93 +33-86,28-39 +24-95,95-95 +89-91,10-90 +5-77,4-76 +2-49,21-49 +67-91,54-91 +51-76,51-51 +70-74,59-74 +22-32,31-31 +22-53,21-84 +39-63,25-27 +5-95,2-5 +56-66,67-89 +60-60,58-61 +9-36,1-36 +16-35,15-34 +34-69,69-70 +52-84,54-97 +10-84,36-83 +96-97,24-95 +4-99,2-5 +34-90,33-35 +87-97,97-99 +29-38,30-39 +67-97,66-67 +64-83,76-94 +4-94,2-5 +67-87,14-96 +3-94,7-86 +69-80,74-79 +67-82,66-66 +3-85,29-85 +95-97,94-96 +81-85,3-81 +13-78,19-77 +5-35,16-35 +14-42,14-91 +21-81,3-88 +1-7,7-89 +18-70,17-18 +47-96,96-97 +69-93,73-92 +48-48,11-48 +36-95,35-35 +67-67,11-66 +3-86,5-85 +48-89,2-7 +9-58,10-57 +69-70,69-95 +36-97,96-98 +9-43,9-92 +10-69,12-69 +17-76,76-77 +2-99,98-98 +30-31,29-33 +38-64,41-64 +1-91,1-92 +7-16,2-16 +1-99,1-1 +11-96,12-95 +14-93,13-93 +10-87,2-10 +10-66,10-96 +17-87,16-16 +14-58,57-59 +19-95,20-94 +43-59,22-44 +84-97,84-98 +14-77,34-77 +45-69,44-46 +52-87,51-68 +3-7,6-56 +14-51,46-50 +44-66,49-67 +9-12,1-11 +86-90,86-90 +32-41,29-42 +53-80,11-73 +93-93,13-94 +39-84,40-73 +16-71,71-72 +9-94,9-94 +44-91,3-98 +18-78,18-98 +19-48,18-47 +23-77,68-77 +20-68,63-67 +48-90,48-97 +40-49,39-71 +4-15,15-99 +76-77,60-76 +12-56,17-57 +27-92,28-93 +13-74,12-14 +4-97,5-66 +50-50,50-51 +66-66,44-66 +58-68,23-52 +47-76,48-76 +8-96,48-98 +43-92,7-92 +33-54,15-97 +72-99,71-98 +13-76,31-36 +2-14,11-13 +8-70,7-9 +8-54,8-55 +93-96,94-96 +31-74,73-73 +65-74,53-74 +46-93,45-94 +31-99,8-99 +6-37,10-37 +21-82,22-83 +55-73,17-20 +65-79,66-72 +15-43,14-44 +1-96,6-97 +3-98,5-99 +24-80,18-71 +56-92,57-80 +12-54,40-83 +31-80,16-81 +35-48,22-85 +2-90,1-89 +2-2,2-59 +87-96,51-87 +36-99,37-99 +8-90,37-70 +14-14,14-76 +39-99,20-71 +52-65,52-65 +31-48,31-48 +57-57,57-88 +68-92,68-81 +5-98,6-98 +87-96,25-88 +6-86,87-87 +69-88,33-84 +39-86,40-40 +77-82,77-87 +11-88,97-97 +29-87,87-89 +40-55,39-40 +96-97,47-95 +5-85,85-85 +11-93,93-94 +29-96,89-95 +6-69,26-92 +20-76,15-20 +39-40,38-39 +7-33,6-34 +34-78,35-37 +18-67,9-67 +7-99,6-99 +64-88,47-64 +17-96,18-93 +6-84,4-85 +21-21,21-96 +6-42,5-98 +35-97,38-85 +31-94,9-57 +59-73,62-73 +5-97,5-97 +16-53,16-32 +8-42,7-43 +10-94,11-60 +42-50,17-80 +54-57,57-77 +37-89,88-96 +24-79,20-79 +7-91,24-96 +24-89,88-94 +28-82,10-81 +3-3,3-94 +89-93,92-92 +34-99,34-34 +22-22,23-63 +10-92,3-10 +77-77,77-84 +32-59,28-42 +54-86,55-71 +42-50,11-46 +88-88,6-90 +70-74,72-72 +1-1,1-50 +46-50,9-47 +95-95,3-96 +93-95,12-94 +22-85,14-15 +23-73,22-92 +4-86,4-86 +75-90,75-75 +56-56,55-55 +31-91,32-80 +64-78,3-79 +16-85,15-53 +61-64,62-65 +86-86,23-87 +27-96,68-80 +18-98,18-51 +6-92,49-98 +23-23,23-28 +13-74,5-14 +15-93,16-91 +2-97,12-97 +23-92,17-24 +56-76,75-75 +38-82,39-83 +15-97,97-98 +44-86,45-69 +79-85,35-86 +19-98,20-92 +59-59,7-59 +27-91,28-88 +11-71,66-70 +3-79,5-78 +22-77,21-76 +92-94,12-93 +6-85,6-6 +14-81,14-81 +5-6,8-44 +34-37,36-36 +1-53,29-87 +40-72,67-72 +4-81,1-97 +27-76,10-58 +32-75,2-40 +15-86,86-86 +42-90,2-90 +21-73,20-73 +84-99,3-97 +21-81,2-22 +75-99,53-76 +5-96,5-99 +68-70,21-69 +24-24,23-47 +7-8,7-73 +6-6,6-66 +31-37,30-36 +4-91,5-90 +16-76,18-47 +44-97,25-97 +48-92,47-95 +45-62,45-62 +62-93,48-93 +53-91,77-98 +54-70,12-69 +6-20,17-39 +46-46,12-47 +4-4,5-61 +9-42,43-88 +97-99,1-97 +11-90,89-89 +9-15,4-14 +15-81,4-80 +31-43,44-68 +4-9,1-10 +47-48,32-47 +22-47,46-55 +10-12,11-86 +3-4,4-41 +64-84,18-85 +15-39,14-47 +15-51,11-12 +10-57,9-56 +16-29,15-30 +38-57,10-39 +20-87,20-82 +1-43,3-43 +9-90,7-89 +23-88,25-89 +50-92,51-91 +13-94,94-96 +2-92,47-91 +22-95,1-23 +22-73,21-22 +3-5,4-33 +13-75,12-76 +18-26,17-39 +6-72,10-71 +17-76,3-41 +24-96,17-96 +50-57,51-55 +97-99,80-98 +5-66,5-78 +8-57,14-56 +20-21,20-97 +22-79,21-79 +2-99,99-99 +95-95,38-96 +34-95,35-94 +14-27,6-13 +19-87,18-88 +64-66,60-66 +38-85,37-38 +88-97,4-87 +1-99,2-92 +8-12,12-92 +25-60,26-59 +38-74,19-38 +45-76,15-46 +60-91,60-91 +81-81,12-82 +19-20,20-61 +22-97,19-98 +13-57,28-65 +15-90,12-15 +40-40,23-40 +27-92,17-92 +7-99,8-77 +6-83,16-83 +53-70,37-69 +66-93,42-71 +73-92,89-92 +5-87,6-39 +9-99,99-99 +27-80,81-81 +7-58,58-58 +9-85,50-86 +39-94,14-88 +13-40,2-39 +2-89,1-28 +43-89,72-88 +23-41,24-24 +23-34,34-83 +19-64,19-20 +60-97,61-89 +4-57,56-56 +8-89,40-90 +30-48,17-30 +3-93,2-92 +1-10,10-74 +1-24,5-56 +19-19,18-47 +3-10,5-83 +50-52,48-51 +13-75,13-13 +10-10,16-61 +27-92,27-88 +45-93,46-94 +47-61,16-61 +76-77,76-83 +32-84,84-87 +86-86,87-87 +7-95,8-73 +8-69,20-68 +44-95,44-95 +11-39,12-40 +63-99,70-99 +36-45,35-49 +6-55,55-75 +91-93,91-93 +61-75,49-75 +86-98,87-99 +46-98,10-14 +95-95,74-96 +5-92,91-99 +2-92,2-2 +1-57,11-56 +97-97,14-98 +8-74,74-83 +19-67,61-66 +94-94,24-95 +5-9,69-90 +30-66,30-65 +2-99,1-98 +38-94,63-73 +1-2,1-99 +43-93,20-90 +42-89,40-42 +22-88,52-87 +78-79,23-77 +22-89,21-89 +12-94,11-80 +2-7,6-84 +20-99,20-20 +8-93,38-99 +16-60,17-61 +41-41,41-83 +17-47,4-57 +18-56,18-56 +1-57,1-57 +77-87,1-77 +62-91,63-92 +6-90,10-91 +2-4,3-97 +53-67,54-62 +29-30,5-30 +51-51,7-50 +2-87,1-88 +13-73,55-87 +8-29,7-29 +64-97,15-98 +23-84,22-23 +7-10,3-11 +19-35,35-78 +49-96,50-97 +16-94,17-94 +24-71,9-82 +29-88,9-29 +24-52,43-51 +69-73,8-82 +1-3,2-91 +6-85,7-7 +4-86,16-79 +4-4,6-50 +17-83,82-88 +27-81,28-67 +82-85,56-84 +39-40,40-86 +2-48,8-49 +8-79,7-8 +62-86,61-85 +65-67,47-66 +48-97,49-97 +41-86,66-85 +18-41,17-40 +40-85,67-76 +77-95,58-91 +39-66,31-40 +4-7,7-98 +33-96,44-96 +3-11,3-3 +69-70,8-70 +97-97,3-97 +32-94,81-93 +21-70,20-69 +47-56,28-55 +1-1,1-33 +36-71,35-86 +18-88,19-88 +4-99,8-96 +13-79,14-62 +11-71,11-79 +69-92,68-92 +18-88,88-94 +26-92,26-92 +15-69,16-68 +59-95,95-98 +40-53,39-54 +53-64,32-53 +17-97,16-84 +28-99,7-88 +49-81,49-69 +22-98,97-98 +66-94,5-97 +3-70,1-76 +5-93,2-25 +46-66,65-65 +69-70,11-99 +55-93,56-77 +11-92,12-93 +34-96,95-96 +1-3,3-15 +49-88,19-88 +80-84,63-85 +7-83,56-99 +14-62,55-67 +22-84,23-66 +35-89,31-35 +33-81,7-81 +78-85,77-86 +38-44,46-80 +72-83,12-73 +62-92,65-92 +60-89,26-60 +48-59,41-57 +55-83,56-84 +27-89,48-50 +5-66,5-84 +30-84,21-84 +23-62,22-62 +55-93,37-94 +6-47,15-46 +28-28,29-39 +2-3,13-13 +46-81,18-81 +37-88,38-89 +7-53,17-52 +32-67,18-81 +22-22,22-24 +2-71,15-45 +1-92,1-92 +14-89,10-91 +9-79,12-78 +4-15,3-92 +49-68,29-69 +1-2,3-51 +27-45,28-44 +22-99,21-99 +66-70,64-69 +97-98,67-98 +47-98,47-57 +47-98,48-79 +7-31,9-34 +8-84,8-8 +20-92,6-20 +14-90,5-22 +23-68,29-87 +3-42,41-71 +80-80,81-96 +59-80,79-95 +17-86,35-86 +32-92,91-91 +72-77,53-78 +20-23,23-95 +94-97,8-94 +25-34,26-33 +12-96,13-98 +73-95,73-73 +43-50,44-50 +5-99,1-5 +63-94,63-94 +40-52,41-42 +25-25,25-78 +6-10,9-97 +23-26,26-77 +48-79,48-48 +26-96,88-90 +6-11,9-27 +34-42,43-92 +15-83,16-87 +9-93,82-93 +45-77,76-85 +74-98,18-99 +7-95,8-94 +46-98,97-99 +18-42,42-60 +15-17,16-35 +44-88,64-87 +60-61,56-62 +46-48,49-58 +90-97,6-90 +19-20,15-20 +1-1,1-61 +34-75,33-76 +1-90,5-90 +51-61,5-52 +40-91,90-95 +17-77,1-77 +77-77,17-77 +32-81,33-76 +12-41,13-41 +61-91,59-92 +44-67,44-81 +31-63,31-71 +8-97,6-79 +7-98,6-99 +4-38,3-4 +2-95,94-99 +3-28,1-3 +50-56,3-98 +24-76,24-24 +5-10,4-78 +30-75,30-76 +41-47,40-66 +4-37,3-4 +72-98,71-99 +7-19,8-19 +64-64,19-64 +7-58,7-58 +35-45,36-45 +1-99,99-99 +6-68,2-69 +52-75,53-72 +11-91,38-91 +6-17,6-17 +16-57,17-57 +6-16,7-15 +18-86,17-85 +13-35,7-74 +35-40,34-40 +20-85,37-84 +89-89,21-89 +2-24,21-25 +25-73,3-47 +7-7,7-93 +15-20,14-95 +10-83,11-82 +10-95,9-96 +17-86,47-87 +14-80,78-80 +47-50,53-92 +74-76,63-75 +8-44,3-9 +80-91,16-92 +11-11,12-89 +35-97,34-98 +28-98,98-99 +20-58,58-80 +21-71,8-40 +14-77,14-14 +28-73,27-73 +58-83,59-83 +12-96,44-59 diff --git a/2022/day4/main.c b/2022/day4/main.c @@ -0,0 +1,67 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <err.h> + +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); + } + + int fullOverlaps = 0; + int overlaps = 0; + + 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 + + // Is there a better way to allocate memory for this? + char *ranges[2]; ranges[0] = malloc(8); ranges[1] = malloc(8); + char *range1[2]; range1[0] = malloc(8); range1[1] = malloc(8); + char *range2[2]; range2[0] = malloc(8); range2[1] = malloc(8); + + splitString(line, ',', ranges); + splitString(ranges[0], '-', range1); + splitString(ranges[1], '-', range2); + + if (atoi(range1[0]) <= atoi(range2[1]) + && atoi(range1[1]) >= atoi(range2[0])) + overlaps++; + if (atoi(range1[0]) >= atoi(range2[0]) + && atoi(range1[1]) <= atoi(range2[1])) + fullOverlaps++; + else if (atoi(range1[0]) <= atoi(range2[0]) + && atoi(range1[1]) >= atoi(range2[1])) + fullOverlaps++; + } + free(line); + if (ferror(fp)) + err(1, "getline"); + fclose(fp); + + printf("PART 1: There are %d pairs in which one range fully contains the other.\n", fullOverlaps); + printf("PART 2: There are %d pairs which overlap.\n", overlaps); + + exit(EXIT_SUCCESS); +} diff --git a/2022/day5/input.txt b/2022/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 5 diff --git a/2022/day5/part1.c b/2022/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/2022/day5/part2.c b/2022/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); +} diff --git a/2022/day6/input.txt b/2022/day6/input.txt @@ -0,0 +1 @@ +tnmmpfmfzmmnsmsjmjjbvvhnhzzfmmgpmgpgbgnnwffjhffzqqmzzbnbssrqqrnnhsnngsszsqzszhzfhzfzwzfzrrmhmghgwhhjjqwqttwhttjllrtrtzzcfzfgzznfznfzfnnbddvmvzmmfsmfsmfffhlfldlqqrnrznnhmmgqqzhhmjhmhppqbpbbngnlldvvdqvvrtrdrtrnttnppfllrbbrprpnpdplpmllhwwddqpdprddzzfccqpcqpcpcbbdhdjdjwjcwcctdcttzgzmmscmsmdmttwhwzhhnjhnhlhvhlvlglpgpmmjmgmrgrddmwddjfftfwflfslffqtfqttpftppflfmmhvhvcvbvhbhggpbgbppvdpvpvfppbwwsnnhphllbdbnbvbmvvzffvsffdldmlmtmccnlnbnjbnjnhhbfhhgzzlwlfflzffdccggdcgcjjhffjfgfgcczjccvwcvvqgvvqvllqzqmqllhjjqnqggttsdddjgdjgjzzrgrfrbrssrgrgdgrgbbssmdsdfddsndnsdnsdnnmqqsspqqmrqqpmmsjmmszzqvqrvrzznnjdndtntfnttgtctqtwwnwswrrthrttsdttlhlvvdzzgqgttnppjpljplpgpvgvqqvppzmmqggtjgtgstslltjltjjgcjcmjmsshvvtppgmmlslqqshqshsllbggfpgffdsdgssncchctcwwtllgqlqblqlqvvmsvmmwnnzppqllsttgmttftvfvjjrzzswzzjvzjzljjchcshcscbbrdbrbcrrnvvtctntvtvbvjvqjqggsrspsprrbgghdghhmwwldldzdttrvrnrfftqtftrrdsszlzvvbtbffftzzrzqrrhjhghhwbhhsjsfsttdjdjnjhjmjpmplplrrdjdcdjdbjblllbqlqdlqlpqptppdhhqmqfqhqhchwwqjqfjqfqhqshsmswsbbvssdspdpsdssstntltrrgnnmttgmmsjjrlrnlrnrnwrwfwlfltlzllcjcmjcjpjhphcpcwppmvmjjzbzvbbfnfcflfddntddbmmmhnnsrnrrvdvnvcvwvcwvwrrqwqccqmmswmmjrjmmwjmjfjhwrtbjzdvlgrjmvzfmhcqsncvlhzzncjlbvcwrdwjmqjcnptqslvfzpsvltgzsvjdsjrppdrmqrbqwhddfhnftfblspsrhtdtjwdnhbcbtlwlvccsfscvczzrrqmwbwbdmwgzqntvflppqvppwrhnvtlsbzqglhsfdgssqzdtjdpwrrhbnbtwhhnmnlwfwlqffjjrndbpwwsvdrhddbjnnqzmtpvvtwbcpndjzlhcfrrdvmljswjzvmfqcdsgqwclqshwrmblszdvsnrpdgnllmlchzdjlrrpndmmgddjqgjqrhwfbwddqdfbvptrmzhtsqfsfswpnvmtswqprjhbzvntgrlzthhnqbtpplqpvcfnpgdtbhqbhflltbbtmmhcwztslmpznttmssclhmnbsbrwlblrbsdfmnpqbwwmsncvzmpqwhzjgcgdrzvglgdtswmstdhrprdjfmqtjlmplbjtzcgnrwpdvpfjjfwjfnnpmdtwtqsgfndngsbmcwjtglqwtfrclbczfcmjtgcwszhzrbcphrhwmhcwghjznzthnwpljjltdlvqtffsrbmwcsvrdmqqggbznnlzbbqtgspqvnjpbdhtzmgttrcwwszwpgdrcnfqtgrgqdrctlzwtdwqppbhnwgldnqltznnfpbfqtgmmwpcqnndbgmrrtgtvnmlfcwsldchjnnqfrhpzwtclrzftsqllgvpqbgmfjdhqjttwcvbpvfqsvhbhhtwnqnbgndbtzhcvgglbhghbzrbrmdllmgfgttqmhtdnwrpwllhnghrjctrbzrcpnjnctvmrlpjhftnfbczrjrnnbqplplcrbngbhvmmvcffmgvbhjzbhcmtwmwgmjmwjvvlqfldswpntjnsjvmdlbzqqlgbwspwvmnwtwjbczmwplrhmjgsppnmtwmvsfwnsgddgwqcvpftcpzrhpldnwmcjgtjmljjbcmjcqdbwczndnjnjgrmtjrqnnjndzqdqpcgdqptdbrqftnwrgqmrzrvsfmmmbpltlncvtgrjfjmvtgwqphczwjhdrdwtfvgztbhrndvpcbgfjfvmrrljwrvcrtdmtjndfnwgcnfrzgsnjpztbwwsbvqfnpjctgrhsflhnzbbsfqbnmtnvrmjzsbjfndvttpvpfjhqntflgbfnzcclcwmhbsgqfjdcgsvrhtstspfzgvgglgddqmclsmzgzgtncdsfmwdvtcsgwvbzjvclwppqdjgfcrcbzcwbdhrnssjbmnmfmwthdrnmlfhqlddwqrdhsdvdcsmcgjsgcmpnhlbnqftpdjswtmpbznlcrhtswgnmwjcdfmljdngzfsmlzjjnzmfzshmztdbdmcqwmlvcrzgpmbjqcghclwvdbrhgvwqchnndftnrtptmctdlhmfjvpzrpccddfpcdwmzqfhnsqzrvwblzfhcjdcjfctczwqrcbjnrpdcbbnsgnlvqqmnsfgsqschjlbzhhsrbvdbfrhvsgrlzwncgwpdbvmblgzbwbcbgqfwmdmgcrbbjfcvmqgztqpptdhwmvmsdqwplpgcjzgqzdrftzhqbltvhrmlrfffcgfpqzwrrbbtlsjgmtbjvtnmhwdpjptjwfwgjgvbfqwmflrrqzlzdcmtlnptdrpcpdnswcfscnndnrfbgwvvncdjgsdpbwptdtvrqlmrhmvvcwblhhzbjdpsbszhrftfbcgwhwrgglnjzqdhcqnvlhgqjhnddvrslhntssptsbhmqwwqqnbvfmcbgpvgjbrttnvlljdbtfplgmbwtcbcdtqdpqqdvhbmpmtszwpzblcfrtznhhtcljtdlhjdbnlhvwgjsmgvrslrfwnmzwlstpgltvrgnpdqztvfnvdhdtwwqdfsmtpbpdclsbnwcgjzchjcsjmvhbjshmjjlpgdzcgbmmchwmcsddsvhsnpqtcpnhqnbvwgwqhtjbqncgwwftnrzsbsjtvqmjzqvvncmncwflcfpcjqgdtbsmjzzsdjfvhnqbgjhmfgjghwscthbfmbndltbqzwpqtmrswvprpmgwqnqpfnmffrpdlpfqmhrthppzvzwbrtjvwvjndsqdlqtbpqwfcttggnjmcqqnmjwfhfjgcvlnmtlgbdvmctzlwbfgnflwtsflgnfbnfbhhdgjctzvvmrhdsmvmmtnqwtszmqcpsbrqrgjfrzctcbzmtdlhwjtfdqbtthdnqcrpwrhcrvjstbhpltvgmvpmvfjstgzjsgzprzcqzqztvvdcnrrqwrhddcrhhncdrlwzwqlnbbzcfmqtnwgfdscmrbwnbldlfrqchzdnlnmwncgrzdclnvcvplgwjsbzmbnnsdrsfhrlssvncnwmcrjdjbjpdtrrvlnbjvspfqbwdpcnnpjzfnmbhcdhlmdgbpvbzmfltzstnznfctcdzhbfsvnfbsjqzmwfllhtrsfghlrpjgrgzgchlwrdmqzbrncsvnwhfqmwjbnvjctzphcsftqsbmwntgvjqhhvwndvmfmjhhhmfdvrlhpvzmmhrbhbddqbdmgqqsvddsswmzqcjmvhztfqpchzpwhdshzjlmbmnsgzqhbnmrshwvtmgmgndtddpfwsjrrjdhncdhtlczdvlbvqplttnzrblthlcffdtfsdtpwzdgbldvnsttvpzmbgnqddrszftcpwrgmfzhjjvghpntmzcttcsnrjnfpqzqqqljhzlrpgwngllqjwnwfcsphqplgbzmfqfgbfsqpsrntszqbcqnhctsnbfshmlbwfflrwwsjwqwfqlgnftdwmctmclwjhjhbsspqldlshbmpbgrftpnbpsqldhrrbdqwfwvfhclrlfdjfmzgmptdjdcsplcspznfjrfhtsjndwpslrdgnllllwqjgznrhswfssdlvdpmwwgmstqbhfmdhtzvzzvhwzbrrvvsl diff --git a/2022/day6/main.c b/2022/day6/main.c @@ -0,0 +1,47 @@ +#include <stdio.h> +#include <stdlib.h> +#include <err.h> + +#define NUM_CHARS 14 + +int +main (void) +{ + FILE *fp = fopen("input.txt", "r"); + if (fp == NULL) + { + err(1, "Failed to open input.txt"); + exit(EXIT_FAILURE); + } + + char *line = NULL; + size_t linesize = 0; + ssize_t linelen = 0; + linelen = getline(&line, &linesize, fp); + fclose(fp); + line[--linelen] = '\0'; // cut off the newline + + char window[NUM_CHARS]; + + // Grab first NUM_CHARS chars + for (int i = 0; i < NUM_CHARS; i++) + window[i] = line[i]; + + // Then check if they are unique and test the rest of the text + for (int i = NUM_CHARS; i <= linelen; i++) + { + for (int j = 0; j < NUM_CHARS; j++) + for (int k = j+1; k < NUM_CHARS; k++) + if (window[j] == window[k]) + goto next_char; + + printf("A marker appears after %d characters.\n", i); + break; + + next_char: + window[i % NUM_CHARS] = line[i]; + } + free(line); + + exit(EXIT_SUCCESS); +} diff --git a/2022/day7/input.txt b/2022/day7/input.txt @@ -0,0 +1,1013 @@ +$ ls +dir bjrbjh +dir dppgvlh +dir fcfqp +dir mtbt +95962 mzvb +dir qtfmf +dir sfjrs +dir trtl +$ cd bjrbjh +$ ls +80731 ctprm.bpc +$ cd .. +$ cd dppgvlh +$ ls +180122 bbjw +210923 ctprm.bpc +304465 hhg +dir rtdnhb +$ cd rtdnhb +$ ls +295880 ctprm.bpc +$ cd .. +$ cd .. +$ cd fcfqp +$ ls +dir cts +dir gjzdf +61601 hqvhrpnv +27922 hqvhrpnv.sgf +dir hvsnr +191405 mzvb +263646 nbjp.fdm +dir qmsllmtw +dir rpvstz +dir vbhh +dir zwllwsq +$ cd cts +$ ls +47983 nhzpb +$ cd .. +$ cd gjzdf +$ ls +dir fcfqp +161310 vtrhs.mlh +$ cd fcfqp +$ ls +145412 vcbnl +$ cd .. +$ cd .. +$ cd hvsnr +$ ls +dir sftjlqbm +$ cd sftjlqbm +$ ls +96517 qlgp +$ cd .. +$ cd .. +$ cd qmsllmtw +$ ls +172998 hhg +$ cd .. +$ cd rpvstz +$ ls +dir nccm +dir tsstr +$ cd nccm +$ ls +181998 fcfqp +$ cd .. +$ cd tsstr +$ ls +258571 tsstr +$ cd .. +$ cd .. +$ cd vbhh +$ ls +317169 ctprm.bpc +dir rpvstz +dir scsclh +307868 vcbnl +118337 zbltwtj +$ cd rpvstz +$ ls +dir fcfqp +dir nzg +dir pmhprnbb +dir szshbn +$ cd fcfqp +$ ls +dir hrhftz +$ cd hrhftz +$ ls +163301 vlrjptvv.fsr +$ cd .. +$ cd .. +$ cd nzg +$ ls +230765 hhg +235220 ptg.dbs +dir qsbpc +220737 vtttmhf.dcl +$ cd qsbpc +$ ls +74559 hqvhrpnv.phh +322306 tqb.wnl +$ cd .. +$ cd .. +$ cd pmhprnbb +$ ls +293892 hhg +$ cd .. +$ cd szshbn +$ ls +303519 tsstr.pml +$ cd .. +$ cd .. +$ cd scsclh +$ ls +dir rdznzm +$ cd rdznzm +$ ls +dir hthgb +dir vgfhz +270085 zjlcp +$ cd hthgb +$ ls +2762 gjgczrpm.hlw +$ cd .. +$ cd vgfhz +$ ls +160621 svplfhqh.rfr +27592 vcbnl +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd zwllwsq +$ ls +dir bmgpnmbt +dir fbphbmbb +217873 hhg +dir hqvhrpnv +$ cd bmgpnmbt +$ ls +dir fcfqp +67327 hhg +175579 qrgrtg.gtn +185356 rdrtvn +$ cd fcfqp +$ ls +16285 wqtnzw.cvj +$ cd .. +$ cd .. +$ cd fbphbmbb +$ ls +dir fpnwjb +dir hqvhrpnv +dir jbm +dir jsfscjd +dir qmpbb +dir sjhrg +dir tsstr +79487 tsstr.czd +226172 tvdpb.vss +$ cd fpnwjb +$ ls +78102 pgv.snz +32886 wqtnzw.szw +$ cd .. +$ cd hqvhrpnv +$ ls +247435 ctprm.bpc +dir drpffn +dir fvqzjjhp +312573 hqvhrpnv +103964 swbvbwd +dir szdbbtw +129434 trjpn.htm +33772 vgjnhmbc.hcr +dir vvrhmrbs +$ cd drpffn +$ ls +204660 wdhl.dgs +$ cd .. +$ cd fvqzjjhp +$ ls +dir qtc +$ cd qtc +$ ls +74576 wgfffz +$ cd .. +$ cd .. +$ cd szdbbtw +$ ls +dir fcfqp +$ cd fcfqp +$ ls +56006 bthq.wlm +$ cd .. +$ cd .. +$ cd vvrhmrbs +$ ls +dir sqwfj +21051 vcbnl +$ cd sqwfj +$ ls +dir chjpwmb +170226 fcfqp.wrn +$ cd chjpwmb +$ ls +305407 ctprm.bpc +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd jbm +$ ls +138219 fcfqp +dir jwbrhmzj +67905 mbvpfmgs.lbq +125465 mzvb +dir tsstr +211711 vrqpss.qtr +$ cd jwbrhmzj +$ ls +22219 zqmldh.jwc +$ cd .. +$ cd tsstr +$ ls +141377 ctprm.bpc +28965 jhwr.tvf +dir rmbpb +$ cd rmbpb +$ ls +185176 mzvb +204877 vcbnl +$ cd .. +$ cd .. +$ cd .. +$ cd jsfscjd +$ ls +210670 jqwcrvg +263796 qcv.crz +23224 slcgw.hmz +dir tws +$ cd tws +$ ls +244925 dnrswnh +307737 vcbnl +$ cd .. +$ cd .. +$ cd qmpbb +$ ls +175766 fcfqp.pgc +155950 nnrgl.qtd +215226 rjjw +218856 rpvstz.cls +$ cd .. +$ cd sjhrg +$ ls +dir dbdc +dir jprmnvv +dir wbqrzrcd +$ cd dbdc +$ ls +159323 cqfmgtr.fpp +$ cd .. +$ cd jprmnvv +$ ls +314162 ctprm.bpc +$ cd .. +$ cd wbqrzrcd +$ ls +177804 mzvb +11757 wfmhd.srn +$ cd .. +$ cd .. +$ cd tsstr +$ ls +303871 hhg +311124 mzvb +$ cd .. +$ cd .. +$ cd hqvhrpnv +$ ls +75235 mzvb +$ cd .. +$ cd .. +$ cd .. +$ cd mtbt +$ ls +277478 bqvdgj.tdt +dir gtgdhl +100015 hnmvhb.dqb +dir hqvhrpnv +dir pdhn +dir ptmpqgj +dir qfdmhqhm +dir tsstr +178843 tsstr.crd +dir ttjltb +45660 vbqjdj.znj +204359 vch.zrz +$ cd gtgdhl +$ ls +305753 hsjc +$ cd .. +$ cd hqvhrpnv +$ ls +131065 cvt.pwb +dir dzrlrdc +dir fcfqp +175755 hqvhrpnv.gjg +313719 jjpnjhqz.wtf +3622 nhr.vtv +dir spz +$ cd dzrlrdc +$ ls +65797 wqtnzw.tpr +$ cd .. +$ cd fcfqp +$ ls +141146 hhg +$ cd .. +$ cd spz +$ ls +292906 gjgczrpm.hlw +202319 gjn.ptw +311120 vcbnl +93748 wqtnzw +$ cd .. +$ cd .. +$ cd pdhn +$ ls +dir rjgsq +$ cd rjgsq +$ ls +424 ctprm.bpc +$ cd .. +$ cd .. +$ cd ptmpqgj +$ ls +10127 mdnllcs +178992 npbr +dir rpvstz +dir wqtnzw +$ cd rpvstz +$ ls +248549 gjnpwldn.jsh +31443 nstp.jpj +$ cd .. +$ cd wqtnzw +$ ls +47276 mzvb +$ cd .. +$ cd .. +$ cd qfdmhqhm +$ ls +254322 wqtnzw.qrn +$ cd .. +$ cd tsstr +$ ls +265555 cqbs.thq +273707 ggnr +$ cd .. +$ cd ttjltb +$ ls +82810 cmln.qlj +23429 mzvb +$ cd .. +$ cd .. +$ cd qtfmf +$ ls +108080 hqvhrpnv +dir hrl +dir mnb +85284 qrfjg +dir rfghjdj +dir tsstr +dir wqtnzw +$ cd hrl +$ ls +dir dwgnv +dir lgn +dir qhcjcc +dir rpvstz +dir zrqf +$ cd dwgnv +$ ls +dir mrhvqqc +$ cd mrhvqqc +$ ls +113150 qbhmdfwg.wrt +$ cd .. +$ cd .. +$ cd lgn +$ ls +293311 gzzdwd.wnn +$ cd .. +$ cd qhcjcc +$ ls +298893 fcfqp +253573 lvb.brw +301515 ndbrjbw.ssq +236001 vmdgmm.gmh +124715 wqtnzw.cqd +$ cd .. +$ cd rpvstz +$ ls +dir hqvhrpnv +dir rpvstz +dir tbdnjbmr +$ cd hqvhrpnv +$ ls +271956 vrtgr.vdt +$ cd .. +$ cd rpvstz +$ ls +185836 cjcf.rwc +289321 vcbnl +$ cd .. +$ cd tbdnjbmr +$ ls +124297 ctprm.bpc +323010 hhg +$ cd .. +$ cd .. +$ cd zrqf +$ ls +dir fcfqp +dir vrp +$ cd fcfqp +$ ls +1896 cdfldlv.ptw +$ cd .. +$ cd vrp +$ ls +5610 fmmvbft.rjq +$ cd .. +$ cd .. +$ cd .. +$ cd mnb +$ ls +64404 ctprm.bpc +dir tsstr +$ cd tsstr +$ ls +172859 gjgczrpm.hlw +$ cd .. +$ cd .. +$ cd rfghjdj +$ ls +78613 cpfclgj.wqh +$ cd .. +$ cd tsstr +$ ls +dir cjdbwvn +135418 gjgczrpm.hlw +dir lsv +$ cd cjdbwvn +$ ls +dir hqvhrpnv +264636 wqtnzw.gcq +124125 zvpwlrbr.nnz +$ cd hqvhrpnv +$ ls +43899 tdzsmzw +$ cd .. +$ cd .. +$ cd lsv +$ ls +202987 hhg +251479 vcbnl +dir vdltrlzg +$ cd vdltrlzg +$ ls +dir nlchndbr +$ cd nlchndbr +$ ls +134010 vcbnl +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd wqtnzw +$ ls +dir hqvhrpnv +$ cd hqvhrpnv +$ ls +50703 ctprm.bpc +$ cd .. +$ cd .. +$ cd .. +$ cd sfjrs +$ ls +dir cgl +dir cjjls +184472 hhg +241700 pdswzfq.scs +dir rfwnnr +dir rpvstz +dir tsstr +$ cd cgl +$ ls +dir tdwn +$ cd tdwn +$ ls +119119 lzqfb.tgc +$ cd .. +$ cd .. +$ cd cjjls +$ ls +dir hgppstcv +$ cd hgppstcv +$ ls +223932 bvt +85898 hhg +195499 ppbrb.vtq +$ cd .. +$ cd .. +$ cd rfwnnr +$ ls +96712 gmzchms.wst +271527 mzvb +$ cd .. +$ cd rpvstz +$ ls +78920 cbj.mlh +$ cd .. +$ cd tsstr +$ ls +17373 bdlrvwv +50170 vcbnl +$ cd .. +$ cd .. +$ cd trtl +$ ls +dir dflwctzs +dir gsl +168166 hhg +dir jlfrcp +97874 mzvb +dir pfnlc +dir pjj +186837 qzlwgts +132833 tnb.mpv +dir vmjljc +$ cd dflwctzs +$ ls +6196 fcfqp.qcg +181057 mrjvtvl.hmm +$ cd .. +$ cd gsl +$ ls +dir gmlp +dir mjrs +dir rpvstz +91777 wqtnzw.hpl +$ cd gmlp +$ ls +167819 qmtmtppc +254187 sbn.chs +119156 tbhhgmz.vqs +31966 tptqzrqr.zgh +$ cd .. +$ cd mjrs +$ ls +167756 fcfqp.jgz +221559 tzj.rfb +$ cd .. +$ cd rpvstz +$ ls +93065 hhg +$ cd .. +$ cd .. +$ cd jlfrcp +$ ls +dir fcfqp +dir hmlmvqc +256413 mrtwp +141186 mzvb +106340 nvwfhv.rvt +dir pqmfswh +dir qghq +274275 qmmgnjh +dir trhs +dir tsstr +55881 wbtf +$ cd fcfqp +$ ls +265006 fcfqp.dtr +dir tdflqz +dir thjdsqmw +136055 vshqb +$ cd tdflqz +$ ls +dir dtvnrl +$ cd dtvnrl +$ ls +277524 mzvb +$ cd .. +$ cd .. +$ cd thjdsqmw +$ ls +dir chhf +dir hsnrrc +218719 qgt.sdb +dir tsstr +134210 twvn +dir wqtnzw +$ cd chhf +$ ls +6278 ctprm.bpc +$ cd .. +$ cd hsnrrc +$ ls +dir bphrpw +dir dmjvnrhz +dir rpvstz +dir wqtnzw +$ cd bphrpw +$ ls +dir brj +dir hqvhrpnv +dir rpvstz +$ cd brj +$ ls +13067 rpvstz.bqh +$ cd .. +$ cd hqvhrpnv +$ ls +148840 ctprm.bpc +dir fsvgbd +94116 hghq.gqm +$ cd fsvgbd +$ ls +222743 ctprm.bpc +80460 vwp.nnq +$ cd .. +$ cd .. +$ cd rpvstz +$ ls +287030 ctprm.bpc +145932 rhpt +$ cd .. +$ cd .. +$ cd dmjvnrhz +$ ls +dir mlh +$ cd mlh +$ ls +238301 tsstr.vfl +$ cd .. +$ cd .. +$ cd rpvstz +$ ls +59172 mcv +$ cd .. +$ cd wqtnzw +$ ls +50806 tlwnzqgb.dqq +$ cd .. +$ cd .. +$ cd tsstr +$ ls +dir rpvstz +$ cd rpvstz +$ ls +109284 mzvb +$ cd .. +$ cd .. +$ cd wqtnzw +$ ls +dir hwpdsfg +dir mrn +183742 rpvstz.pdq +$ cd hwpdsfg +$ ls +295333 vbbrvhqm.mvj +$ cd .. +$ cd mrn +$ ls +46589 mzvb +235773 pppwz +69304 rsrbq.qdl +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd hmlmvqc +$ ls +77344 ctprm.bpc +291828 hhg +226384 rpvstz.qfl +10758 tpmdrpg.hcj +$ cd .. +$ cd pqmfswh +$ ls +127361 bczcpjln.ffs +208033 ctprm.bpc +144048 hqvhrpnv.bzm +88561 pgf.ltz +149879 twj.drs +$ cd .. +$ cd qghq +$ ls +165758 hqvhrpnv.mqb +$ cd .. +$ cd trhs +$ ls +dir bnrqbrv +177929 rlnln.gcv +60631 rpvstz +dir snd +$ cd bnrqbrv +$ ls +150034 ltjjfp +$ cd .. +$ cd snd +$ ls +21614 hqvhrpnv +95241 wvtc +$ cd .. +$ cd .. +$ cd tsstr +$ ls +dir cdvgvc +dir mbmn +dir mprbjtnd +dir rvwqgn +8263 tsstr +dir tvtddch +$ cd cdvgvc +$ ls +dir gvssnh +121417 hqvhrpnv +180690 hqvhrpnv.dnl +dir nhrvzn +266104 psj.fdv +dir rpvstz +278434 rww.nbt +dir wqtnzw +$ cd gvssnh +$ ls +20979 fpqgb.nbl +$ cd .. +$ cd nhrvzn +$ ls +dir hqvhrpnv +dir nlv +$ cd hqvhrpnv +$ ls +202973 hhg +$ cd .. +$ cd nlv +$ ls +235158 hqvhrpnv +246378 hqvhrpnv.vmg +278166 rfzjcbpv.crc +$ cd .. +$ cd .. +$ cd rpvstz +$ ls +181642 tsstr.wjh +$ cd .. +$ cd wqtnzw +$ ls +dir pgnzftwn +$ cd pgnzftwn +$ ls +dir njtgddhw +217983 sqwmjb +149152 wqtnzw.pzv +$ cd njtgddhw +$ ls +75203 fvbr +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd mbmn +$ ls +82237 hhg +36010 hqvhrpnv.hcf +$ cd .. +$ cd mprbjtnd +$ ls +23927 gbrvbz.rgc +20857 hqvhrpnv +203364 zpbhr.svj +$ cd .. +$ cd rvwqgn +$ ls +291142 fcfqp.mdf +124256 gjgczrpm.hlw +235579 mzvb +$ cd .. +$ cd tvtddch +$ ls +317689 dshzl.plf +44518 vphnqd +$ cd .. +$ cd .. +$ cd .. +$ cd pfnlc +$ ls +dir vdsjh +dir wfvgc +$ cd vdsjh +$ ls +202584 pzs +$ cd .. +$ cd wfvgc +$ ls +315910 ctmrb.qsq +$ cd .. +$ cd .. +$ cd pjj +$ ls +242824 qhngjlvt.pcb +dir wzv +$ cd wzv +$ ls +221150 clrchzw.vbj +$ cd .. +$ cd .. +$ cd vmjljc +$ ls +dir blm +31640 dbtnn +dir fcfqp +dir frjdzh +dir scjhzc +dir tmz +dir vpvh +dir zcwpb +$ cd blm +$ ls +249527 hjsjqbw.rst +dir hqvhrpnv +dir pvnrmvb +dir pvt +dir rjpcb +161576 slp.gbn +dir srrzgt +97355 wtps.brr +$ cd hqvhrpnv +$ ls +dir tsstr +dir wqtnzw +dir zphvcch +$ cd tsstr +$ ls +dir fzrpgcn +$ cd fzrpgcn +$ ls +10603 hhg +$ cd .. +$ cd .. +$ cd wqtnzw +$ ls +56612 ctprm.bpc +22521 fljtffmz.rcl +$ cd .. +$ cd zphvcch +$ ls +dir fcfqp +$ cd fcfqp +$ ls +171787 ctprm.bpc +134010 mzvb +$ cd .. +$ cd .. +$ cd .. +$ cd pvnrmvb +$ ls +100624 fcfqp.nzt +120945 mzvb +$ cd .. +$ cd pvt +$ ls +dir lslcvfv +8511 mzvb +81176 srnqlrn.lvg +315976 ttzwwnn.fmz +dir wvhrf +$ cd lslcvfv +$ ls +dir gmj +13666 tcbf +$ cd gmj +$ ls +174220 drpztfvs.shg +$ cd .. +$ cd .. +$ cd wvhrf +$ ls +19033 gqggsrc +270841 hhg +dir nvvpbmgm +$ cd nvvpbmgm +$ ls +241543 pzht +72906 rmhrd +dir tllpvrdv +$ cd tllpvrdv +$ ls +309970 lmqcbbz +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd rjpcb +$ ls +215654 fcfqp.gjw +241891 gjgczrpm.hlw +dir gqql +101620 sqjtc +$ cd gqql +$ ls +214991 wctr.qnb +$ cd .. +$ cd .. +$ cd srrzgt +$ ls +dir bwfnrbs +147738 hhg +dir qdctrw +dir svpm +dir tsstr +$ cd bwfnrbs +$ ls +133922 gpsmlzbp.ghd +$ cd .. +$ cd qdctrw +$ ls +56213 ppp +$ cd .. +$ cd svpm +$ ls +205352 hhg +$ cd .. +$ cd tsstr +$ ls +248839 fcfqp.hlg +299835 lfdlfrhs.vjz +$ cd .. +$ cd .. +$ cd .. +$ cd fcfqp +$ ls +dir ctbbgw +dir rpvstz +265076 wqtnzw.cts +$ cd ctbbgw +$ ls +306172 vcbnl +$ cd .. +$ cd rpvstz +$ ls +40277 hhg +225110 mllhtfm.jwd +$ cd .. +$ cd .. +$ cd frjdzh +$ ls +69285 jlscvz.lsw +$ cd .. +$ cd scjhzc +$ ls +221073 gjgczrpm.hlw +$ cd .. +$ cd tmz +$ ls +35287 fcfqp +146224 mnz.zwd +189997 mzvb +dir tsn +31655 ttwjfbl.jbb +dir wjszmnp +170705 wqtnzw.slq +$ cd tsn +$ ls +196121 mzvb +$ cd .. +$ cd wjszmnp +$ ls +dir pbtmjghf +dir smbbnl +$ cd pbtmjghf +$ ls +151036 vcbnl +$ cd .. +$ cd smbbnl +$ ls +299800 mbt +dir wqtnzw +$ cd wqtnzw +$ ls +203547 hhg +220352 wqtnzw.dgs +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd vpvh +$ ls +69506 vbgrhzjs.tdb +$ cd .. +$ cd zcwpb +$ ls +73403 fcfqp.hpq diff --git a/2022/day7/main.c b/2022/day7/main.c @@ -0,0 +1,161 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <err.h> + +int unusedSpace = 0; +int smallestDirSize = 30000000; + +struct node { + char name[32]; + char type; + int size; + int numChildren; + struct node *parent; + struct node *children[16]; +}; + +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 +sumDirSizeLT100k(struct node *node) +{ + int totalSize = 0; + for (int i = 0; i < node->numChildren; i++) + totalSize += sumDirSizeLT100k(node->children[i]); + + if (node->type == 'd' && node->size <= 100000) + totalSize += node->size; + return totalSize; +} + +void +findSmallestDirToDelete(struct node *node) +{ + for (int i = 0; i < node->numChildren; i++) + findSmallestDirToDelete(node->children[i]); + + if (node->type == 'd' && node->size+unusedSpace >= 30000000 + && node->size <= smallestDirSize) + smallestDirSize = node->size; +} + +void +calculateDirSizes(struct node *node) +{ + for (int i = 0; i < node->numChildren; i++) + { + calculateDirSizes(node->children[i]); + node->size += node->children[i]->size; + } +} + +int +main (void) +{ + FILE *fp = fopen("input.txt", "r"); + if (fp == NULL) + { + err(1, "Failed to open input.txt"); + exit(EXIT_FAILURE); + } + + struct node *rootDir = (struct node*)malloc(sizeof(struct node)); + strcpy(rootDir->name, "/"); + rootDir->type = 'd'; + rootDir->size = 0; + rootDir->numChildren = 0; + rootDir->parent = NULL; + + struct node *currentDir = rootDir; + + char *line = NULL; + size_t linesize = 0; + ssize_t linelen = 0; + while ((linelen = getline(&line, &linesize, fp)) != -1) + { + line[--linelen] = '\0'; + + char *strings[3]; + strings[0] = (char *)malloc(32); + strings[1] = (char *)malloc(32); + strings[2] = (char *)malloc(32); + + // Line is either: + // $ cd <dir> + // $ ls + // dir name + // <size> filename + if (line[0] == '$' && line[2] == 'c') + { + if (line[5] == '.') + { + currentDir = currentDir->parent; + } + else + { + splitString(line, ' ', strings); + for (int i = 0; i < currentDir->numChildren; i++) + { + if (!strcmp(currentDir->children[i]->name, strings[2])) + { + currentDir = currentDir->children[i]; + break; + } + } + } + + } + else if (line[0] == '$' && line[2] == 'l') + { + continue; + } + else + { + splitString(line, ' ', strings); + + struct node *newNode = (struct node*)malloc(sizeof(struct node)); + strcpy(newNode->name, strings[1]); + newNode->numChildren = 0; + newNode->parent = currentDir; + currentDir->children[currentDir->numChildren++] = newNode; + + if (strings[0][0] == 'd') + { + newNode->type = 'd'; + newNode->size = 0; + } + else + { + newNode->type = 'f'; + newNode->size = atoi(strings[0]); + } + } + } + free(line); + if (ferror(fp)) + err(1, "getline"); + fclose(fp); + + calculateDirSizes(rootDir); + + int totalSize = sumDirSizeLT100k(rootDir); + printf("Part 1: %d\n", totalSize); + + unusedSpace = 70000000 - rootDir->size; + findSmallestDirToDelete(rootDir); + printf("Part 2: %d\n", smallestDirSize); + + exit(EXIT_SUCCESS); +} diff --git a/2022/day8/input.txt b/2022/day8/input.txt @@ -0,0 +1,99 @@ +231123311143433334111323513140220312430352020530355453134243354254345525504344520412234022441201231 +002313303032210031340314312003010320224404022622605552013026333142351022440040305400004242132311121 +220330201413404422305112240330213320021136124315415335033123035640315511030034514131440424021313030 +201320204042120231444423450514444264034132022532310222544264033660224245455501245254224301113033202 +100230132320401153153211332210134066253434230603403642463504006006105221540233444530342314043014303 +122032442431414332522045441015500404344203644233226634442443603306655031345051541032504421424133033 +202321031434355013211523304504664140162101106663501334114500001506012426201040202213400414410420443 +033422404010423322510522632054006500415024100365442766531456202021112062035410413353104310042243312 +110443431341401301233403206566150220000024461521454711652314543050111414550422244110103411011111044 +201203422242133202032302263406105000637616447267624727521265126530162241350166442154043253234041313 +104022033221031042241034106164466623536612244376774564121253574772146361562004241421223514023220400 +440414310500545052512332656122163241446422624364551456541317446567332305362006013160044233035333032 +210004115150024101145654165555423566515754711264245263541734173357434433263303155546120144222240002 +030042432414255562126315563211647645153362721512142763677766742114561322465216641361020150545534412 +403123015321050535251565357633562474213343726145764737476652716513162657627334001426643231115312122 +114301034250403323442564327645166772164175757584482225242282452331322551462150315355210411522315334 +342135502250325230031455573651146635678748778856468367545852536126334772633520613166345055125343121 +240331414023533433311117124527727134563268363765626876836874354585566662473377025500230041031012433 +435521420116206160651166217552425745555244872556778684862854644355351636153154772201634233245254141 +430142200342652562361156566471575782662428747225633244384252643488668173133776753050305455521012404 +101154055404461145474553357357435226287655284867536556382736847785686582546336527411161654600510522 +125205523012146541452325155713277563445585483226638265875362343584672325666362655372014353034500103 +152205531442652200416553617228567268556458664338963688659762235834558825512465454776303604222502325 +233343103643212413761233124526836322436345686967999467774633437656482385721431646313026222325053512 +501320340461623523537621263368458686388686939337494776585685787477573454446327641637323555161002134 +515322360042621777412121437723477582693859378967469987843385364968522276542363545157536002411505525 +205513414500467712163114548784778857594533965478846483595953669558926378465254653724652136460101315 +041100604001336362642258742846457779439797634646658538538454373556384447328632577227354404322115241 +330432606365061456254442526383759337794885884574763495378836935647477758875783835723357120346646344 +033216032220115616576773874887677934937366554498565984797377599998564672852636275344552444234312443 +215111363343756276436445667785966574594365559764868559544885789668635945537522848776576443061535203 +415106311514725425418676378769957879764874855455454784746657855964963336748226643736534746104406622 +152263045613177665648375528588468936695497559865474888988744766995756979628325554634164515561435210 +311405136106436751167488647478374739958766847885898896654578454764947876647224765542171351625331402 +131443421361717252678663268356687689566784598894969544588495896879354855698683874233631212742011640 +114520223332777465223364875648649544757874847599865776469577589798756533866786726347542316613434561 +156464406534124114847235838367885658894975444878866689568885867449774994866957448867553672323050532 +341262503143467744668835539889446596657688869875588789855844648987585544678955565272321116365045416 +354062244564521725625863867798447588968959666555598779795685694965878538389768366485677142523000201 +035003352561512182538484597854465568656549866688667696565599674549859567576467434286677737322650166 +113555506117316553752425399466445855596576885595765599877985868784676496886647746336332614245114064 +260525047433675637362744744746965596745875587686787655888585764566965659969486384433847371754462664 +213036656715765765336849567376558777645985876768858575697557669689449869597695848484552253325663114 +225226243535142385386537866646748879665589678668868867676957765864677746465394355566587567413633214 +652500055731517563452799369639476546488879888978888969876696578989794546483797652865282261755565063 +024003237232476584354267844688644488895875886897676966666978587554448587637653957856324154444551030 +211124217521676866657866967469554969776798799797689778796897569888647694545836942562787275723446131 +510442553712443855247736378387867488559767587798878667777576886666856475837476734748582622223223501 +220055237372748256763577645667969449698885959788976987676895869577946945433688788847875644234514060 +050143342621224332656354344358485996676998697799776866879788595858756757845396823522835523664231326 +316046134444356827463638837499878598587857688887687679866679788667679468887353632246342771363423252 +641355624314462855644343484387958776657888786678877699879886989985667894575797876524556757272165011 +423566165222552367487595663974765677799955758979788776886679966565888579769359357224677534537141651 +240143127214453852854697498688544489889658768667779776696897657765756684836674633574744751223604160 +104532363252255383665298956339597974787695766667666978979989695967556679858735728553776467644562216 +160252112156645687864737579876579556659787688786899869685678598778546447383854634524285537422561544 +144631662137632684444797985884675589666896557778686979865796777678645854558456577835321115175636202 +444031263653662743278288954355878959587987677987886889875889867959766986673565324856524455513626620 +452205301637437747724748457543645895655589955779998675577599655454594858884369778248235144135565401 +234535635453237468746769753649999785595567569977657769578765567659965434939874344677212223116135166 +444105464456237738335639358986997765965755775795789756667588966656447534349493346336265231236016106 +503403266731262767562768396436564565885788896959978587865898986564897945435443888864226345570560536 +103660362732546153332236469356634769484684955899875998959669449899894843593367345252531154430031141 +433313341743147116622563299974444465696459985877785895698975778574955544587934636354374541670342234 +104336613227653354433482854646447388478769484788767768848459746995748858889722867652664746756650532 +124455315366571475275687734947899445845697785949895887444748948474379964335877536646374747202233561 +122315236055236642327253763779969358698655997694647598666594646559644756476585247722143334262161245 +345552103454725471248265478834399638774649598795666886868748785677755789387875583563314137065031002 +410322332227162535482236366633968648797455484884875946656868448359466347548777783274637756643120140 +403311304541576275564322776478376978759955444596858985968559893674539657357256874326242333044431503 +343563440503243435458363728735763359357947785769684969489758498534634678545673786613447310065143224 +113152516545452341611442573535284843994635866487445768694794596986745755247432267662136265543340113 +154353641300537551455688753472457857675989577763574873858487488936987572523488633414765434041263333 +242422354645546262556563346488438493576836888373556749869537946778585772522787163331611051133204335 +303235665440603551575424253236433534846667769999767585833386583775345834273536341323426263551352510 +053030406033265327572256285227386337843368688683848398367483486368775387544552557427526262124232115 +102311254415365421253455216342557452425939763556975494953887935476865466466552773335155141256501043 +544415223520360203126673345687523838428633677773987658675364824355522532266552771672433466302033554 +444445546303415444242143251255326422583427368356749366396356862636866376422762775321530066462133350 +335014151363326422315426733476578288542533458836887285533354524248552822114455466545010246505051525 +025113253162211635452547563652488548824353262345325252722658275255427883666252736546213552414312322 +403352135452042501437331142677262273453326783245758487233657647242354557216175336246362031212324354 +334512051451011330411642277436174585456448264622345773745358252527373173277211514531553566055210450 +443525552450034602520313313415545646578283527627247267654435357633251644633137546562565211320440433 +144442150012541262552434267445764164122522886624765246373687767154314415153110143502445150443431031 +202425401220411403326146273315422521647643672468433378637542765356644551525410533164460005203522121 +440445442434522260350134113673244167326723517373484263521722456627265561124205112001040324024302200 +133241444214340263233345225362331635776735317726464122445365756467666216601624122203641551304332133 +212333420124003410012341433556217433677653363453333271575214145154125371146442146630233421151232024 +324240103540013240405025100243405535224233412432636577232175773454226631111265416431321002353231404 +330042444125504323431136353162410146747642365212471233563311373471322005154000234515441441033000211 +311222123304343401241325355353252011244755553111776513357461242252101665160165003031522555503110121 +222423234211032113215030002041036266501244776723164247523537502152150623662614404111432355402044330 +120223310302022255244105120215044423010054135641724271263340546024016502036452454514514012131010434 +001000231212004145205543502526310535133132351322613330166226622135343154016155251111332512413400430 +300023321142325420044420540430554235664134336551105200015263030025634562522251053500542342334114401 +020110224214134125154505403134561022633204410210043364116436221455124661035142022431003221032202111 +110333213443142104354013214103233265610323413403454633321354646456612411442532034115333321011400012 +202023244334034212011253451025324523366442241613146204642341061310011354032001004431232121344412112 diff --git a/2022/day8/main.c b/2022/day8/main.c @@ -0,0 +1,99 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <err.h> + +#define DIMENSION 99 + +int +main (void) +{ + FILE *fp = fopen("input.txt", "r"); + if (fp == NULL) + { + err(1, "Failed to open input.txt"); + exit(EXIT_FAILURE); + } + + int trees[DIMENSION][DIMENSION] = { 0 }; + int numTreesVisible = 0; + int mostScenicTree = 0; + + int linecount = 0; + char *line = NULL; + size_t linesize = 0; + ssize_t linelen = 0; + while ((linelen = getline(&line, &linesize, fp)) != -1) + { + line[--linelen] = '\0'; // cut off the newline + for (int i = 0; i < DIMENSION; i++) + trees[linecount][i] = line[i] - 48; + linecount++; + } + free(line); + if (ferror(fp)) + err(1, "getline"); + fclose(fp); + + for (int i = 0; i < DIMENSION; i++) + { + for (int j = 0; j < DIMENSION; j++) + { + int visibleDirections = 4; + int scenicValues[4] = { 0 }; + int scenicScore = 0; + // North + for (int k = j-1; k >= 0; k--) + { + scenicValues[0]++; + if (trees[i][j] <= trees[i][k]) + { + visibleDirections--; + break; + } + } + // South + for (int k = j+1; k < DIMENSION; k++) + { + scenicValues[1]++; + if (trees[i][j] <= trees[i][k]) + { + visibleDirections--; + break; + } + } + // East + for (int k = i+1; k < DIMENSION; k++) + { + scenicValues[2]++; + if (trees[i][j] <= trees[k][j]) + { + visibleDirections--; + break; + } + } + // West + for (int k = i-1; k >= 0; k--) + { + scenicValues[3]++; + if (trees[i][j] <= trees[k][j]) + { + visibleDirections--; + break; + } + } + + if (visibleDirections) + numTreesVisible++; + scenicScore = scenicValues[0] * scenicValues[1] + * scenicValues[2] * scenicValues[3]; + + if (scenicScore > mostScenicTree) + mostScenicTree = scenicScore; + } + } + printf("PART 1: The number of trees visible from the edge is %d\n", numTreesVisible); + printf("PART 2: The most scenic tree has a scenic value of %d\n", mostScenicTree); + + exit(EXIT_SUCCESS); +} diff --git a/2022/day9/input.txt b/2022/day9/input.txt @@ -0,0 +1,2000 @@ +U 1 +R 2 +D 2 +U 2 +L 1 +U 1 +R 1 +U 2 +R 1 +D 1 +U 2 +L 1 +R 1 +U 1 +L 1 +D 1 +L 1 +D 1 +U 2 +R 2 +U 1 +L 1 +R 2 +U 2 +L 2 +U 2 +R 2 +D 2 +R 1 +U 2 +L 2 +U 1 +R 1 +L 2 +U 2 +R 2 +L 1 +R 2 +L 1 +D 1 +R 2 +L 2 +D 2 +L 1 +D 1 +L 2 +U 1 +R 2 +L 2 +U 2 +D 2 +R 2 +D 2 +L 2 +D 2 +L 1 +D 2 +L 1 +R 2 +L 2 +R 1 +D 2 +U 1 +R 2 +L 1 +R 2 +D 1 +U 1 +R 1 +U 2 +D 2 +R 1 +D 1 +U 1 +L 2 +D 2 +U 1 +R 2 +L 2 +R 1 +D 2 +L 1 +U 1 +D 1 +U 2 +D 1 +U 1 +D 2 +R 2 +L 1 +D 1 +U 1 +R 1 +L 2 +D 1 +L 2 +R 1 +L 2 +U 2 +R 1 +L 2 +D 2 +L 2 +U 2 +L 1 +U 2 +R 2 +L 2 +R 2 +L 2 +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 9 +D 16 +R 8 +D 13 +R 19 +U 14 +L 10 +R 13 +U 3 +R 2 +U 11 +L 2 +R 2 +U 12 +L 14 +U 15 +R 6 +D 10 +R 8 +U 14 +D 9 +L 2 +U 3 +D 8 +U 7 +R 10 +U 16 +R 3 +U 9 +L 16 +D 1 +R 1 +U 12 +R 12 +U 8 +L 19 +U 14 +R 9 +U 5 +L 15 +U 7 +L 17 +U 16 +R 19 +U 17 +L 1 +D 7 diff --git a/2022/day9/main.c b/2022/day9/main.c @@ -0,0 +1,124 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <err.h> + +#define DIMENSION 2000 // Brute forcing because idk the starting point + +// Notes if I want to revisit this: +// Keep track of all 10 segments like this, modify segment[0] for head and loop +// through rest of them, comparing the current position to the previous +// segment's position and applying the same move and visit rules. +// Perhaps just give up and make a separate grid that keeps track of tiles +// visited because honestly that's way easier + +struct segment { + int x; + int y; +}; + +int +main (void) +{ + FILE *fp = fopen("input.txt", "r"); + if (fp == NULL) + { + err(1, "Failed to open input.txt"); + exit(EXIT_FAILURE); + } + + int positionsVisited = 1; + char grid[DIMENSION][DIMENSION] = {{ 0 }}; + + // Coords are Y,X + struct segment segments[10] = { 0 }; + int hpos[2] = {DIMENSION/2, DIMENSION/2}; + int tpos[2] = {DIMENSION/2, DIMENSION/2}; + + grid[tpos[0]][tpos[1]] = 'T'; + grid[hpos[0]][hpos[1]] = 'H'; + int headOnVisitedTile = 0; + + 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 + + char direction = line[0]; + int numMoves = atoi(&line[2]); + + for (int i = 0; i < numMoves; i++) + { + if (headOnVisitedTile) + grid[hpos[0]][hpos[1]] = '#'; + else + grid[hpos[0]][hpos[1]] = 0; + grid[tpos[0]][tpos[1]] = '#'; + + switch (direction) { + case 'U': hpos[0]--; break; + case 'D': hpos[0]++; break; + case 'L': hpos[1]--; break; + case 'R': hpos[1]++; break; + } + + if (abs(hpos[0] - tpos[0]) > 1 || abs(hpos[1] - tpos[1]) > 1) + { + if (hpos[0] > tpos[0] && hpos[1] < tpos[1]) + { + tpos[1]--; + tpos[0]++; + } + else if (hpos[0] > tpos[0] && hpos[1] > tpos[1]) + { + tpos[1]++; + tpos[0]++; + } + else if (hpos[0] < tpos[0] && hpos[1] < tpos[1]) + { + tpos[1]--; + tpos[0]--; + } + else if (hpos[0] < tpos[0] && hpos[1] > tpos[1]) + { + tpos[1]++; + tpos[0]--; + } + else if (hpos[0] > tpos[0]) + tpos[0]++; + else if (hpos[0] < tpos[0]) + tpos[0]--; + else if (hpos[1] > tpos[1]) + tpos[1]++; + else if (hpos[1] < tpos[1]) + tpos[1]--; + } + + if (grid[hpos[0]][hpos[1]] == '#') + headOnVisitedTile = 1; + else + headOnVisitedTile = 0; + + grid[hpos[0]][hpos[1]] = 'H'; + grid[tpos[0]][tpos[1]] = 'T'; + } + } + free(line); + if (ferror(fp)) + err(1, "getline"); + fclose(fp); + + for (int i = 0; i < DIMENSION; i++) + for (int j = 0; j < DIMENSION; j++) + if (grid[i][j] == '#') + positionsVisited++; + if (headOnVisitedTile) + positionsVisited++; + + printf("PART 1: The tail visits %d positions at least once.\n", positionsVisited); + printf("PART 2: No\n"); + + exit(EXIT_SUCCESS); +} diff --git a/LICENSE b/LICENSE @@ -0,0 +1,15 @@ +ISC License + +Copyright 2022 Jake Bauer <jbauer@paritybit.ca> + +Permission to use, copy, modify, and/or distribute this software for any purpose +with or without fee is hereby granted, provided that the above copyright notice +and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER +TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF +THIS SOFTWARE. diff --git a/README.md b/README.md @@ -0,0 +1,5 @@ +# Advent of Code 2022 + +My solutions for Advent of Code 2022, written in C. + +Writeups on [my website](https://www.paritybit.ca/projects/advent-of-code-2022).