Skip to content

Instantly share code, notes, and snippets.

@aidswidjaja
Last active June 9, 2023 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aidswidjaja/0ac7858c3319720e1a4c69481db523d3 to your computer and use it in GitHub Desktop.
Save aidswidjaja/0ac7858c3319720e1a4c69481db523d3 to your computer and use it in GitHub Desktop.
idiot potato
#include <stdio.h>
/*
Here is my best solution for the thing Aaron posted.
Basically, it follows everything we talked about in VC, including creating a mapper. We also print using the grid concept which Lukas' dad pointed out (conceptually line by line but in terms of calls, one printf), but although I don't necessarily use a grid, I just substitute into my printf template on lines 59-63.
So, what were some of the problems yesterday?
* Trying to create a generalised mapping function which you might do in an object-oriented style (OOP). However, the key issue was that you can't return a char[] (aka a string) in C because it's too primitive >_< and thus this would not work unless we went WAYYY out of syllabus into memory allocation and pointers and that stuff. The solution was to take a PROCEDURAL approach and simply just directly link the mapper and the index, as you can see below. This was pretty straight forward to figure out once I woke up.
* So.... i may have accidentally used single quotes in the output[] string array, which would have represented *chars* and not *string literals*. Whoops.... :facepalmcry:
Otherwise, basically everything we discussed, including shifting the ASCII values by 97 (and not 65 lmao) to map indexes to a 1-D array, then substituting it in our print, worked out nicely. I've also done some syntax improvements with respect to variable declarations (and of course, the printf into one call).
Still can't believe I used the wrong quotation marks, because I'm pretty sure I've made that mistake before. https://twitter.com/Emojipedia/status/1380065374155194370
Lines of code without comments: 31
*/
int main(void) {
char p, q, r, s;
printf("How do you want to build a face?: ");
scanf("%c%c%c%c", &p, &q, &r, &s); // no idea why this wasn't working yesterday but going to bed and sleeping seemed to fix it :facepalmcry:
// our indexes (to store the ith element of the array our program needs to pull from)
int eye1, eye2, nose, mouth;
// There is a way to do this with less lines of code. But don't be Elon and count everything in lines of code, or don't be Adrian and try to unnecessarily abstract everything.
// I'm quite sure this is not ONLY the most READABLE way to do indexes, it's also still the most PERFORMANT (you're simply assigning such that O(x) where x = number of vars, which you'd get in a loop anyways)
// Shift by the ASCII value to translate alpha to numerals with zero-indexing
if (s == '\n') {
eye1 = p - 97;
eye2 = p - 97;
nose = q - 97;
mouth = r - 97;
} else {
eye1 = p - 97;
eye2 = q - 97;
nose = r - 97;
mouth = s - 97;
}
// Mapper
char output[9][5] = {"_", "~", "\\", "/", "^", "~", "\\_/", "/-\\", " o "}; // DOUBLE QUOTES for string literal, i learnt this the hard way
// Rendering via substitution
// I styled it like this and I think it is the most readable format
// Single printf reduces the number of calls --> increases performance
printf("%s %s\n"
"O O\n"
" %s"
"\n%s",
output[eye1], output[eye2], output[nose], output[mouth]);
return 0; // 死ぬ!
}
// https://xkcd.com/1513/
@aidswidjaja
Copy link
Author

The moral of the story is to always check the quotation marks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment