Day 2: Fulminating Farmland
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465


C
Based this on other solutions posted here.
Usage:
Part 1 solution
#include <stdio.h> int get_index(char c) { int index; switch (c) { case 'r': index = 0; break; case 'o': index = 1; break; case 'y': index = 2; break; case 'g': index = 3; break; case 'b': index = 4; break; case 'v': index = 5; break; } return index; } int main(int argc, char** argv) { int t[6][6] = { {6, 1, 2, 3, 4, 5}, {5, 6, 1, 2, 3, 4}, {4, 5, 6, 1, 2, 3}, {3, 4, 5, 6, 1, 2}, {2, 3, 4, 5, 6, 1}, {1, 2, 3, 4, 5, 6} }; FILE *fp = fopen(argv[1], "r"); int cur; int ch = 'r'; int next = get_index(ch); int dist = 0; while (true) { ch = fgetc(fp); if (ch == EOF || ch == '\n') break; cur = next; next = get_index(ch); dist += t[cur][next]; } fclose(fp); printf("Distance travelled is: %i\n", dist); return 0; }Tried to keep it as small as possible, so obvious issues like not checking argc and not checking for letters other than roygbv. Which is pretty cursed because index is uninitialised, so
get_indexreturns garbage, thent[cur][next]indexes into more garbage.Pretty standard for aoc to not check for invalid inputs, and just write just enough code to get the result. Indexing off into the weeds would be very frustrating to debug though if it didnt crash :D