Skip to content

Instantly share code, notes, and snippets.

@Ciro23
Last active November 28, 2023 22:09
Show Gist options
  • Save Ciro23/1c4edac664aaf32961a0e68c0653ba9c to your computer and use it in GitHub Desktop.
Save Ciro23/1c4edac664aaf32961a0e68c0653ba9c to your computer and use it in GitHub Desktop.
part 1 of day 2 of advent of code 2022 in c
#include <stdio.h>
#include <string.h>
typedef struct {
/*
* Each shape is identified by a character.
* A for rock,
* B for paper
* C for scissors
*/
char id;
/*
* Each shape allows to obtain a score
* only for using them
*/
int score;
char beatable;
char not_beatable;
} Shape;
int calculate_shape_score(Shape shape) {
switch (shape.id) {
case 'A':
return 1;
case 'B':
return 2;
case 'C':
return 3;
}
// In case of unkown shapes
return 0;
}
Shape get_shape_from_id(char id) {
Shape shape;
shape.id = id;
shape.score = calculate_shape_score(shape);
/*
* Rock beats scissors,
* paper beats rock,
* scissors beats paper
*/
switch (shape.id) {
case 'A':
shape.beatable = 'C';
shape.not_beatable = 'B';
break;
case 'B':
shape.beatable = 'A';
shape.not_beatable = 'C';
break;
case 'C':
shape.beatable = 'B';
shape.not_beatable = 'A';
break;
}
return shape;
}
/*
* Returns 0 if they're equal,
* 1 if the first is more important than the second
* -1 otherwise
*/
int compare_shapes(Shape reference, Shape to_compare) {
if (reference.id == to_compare.id) {
return 0;
}
if (reference.beatable == to_compare.id) {
return 1;
}
return -1;
}
/*
* A struct is used because of the difficulty
* about returning multidimensional arrays
*/
typedef struct {
/*
* The second array has length 4, insted of 3,
* because one byte is reserved for null byte
*/
char lines[3][4];
} Lines;
/*
* Mocks lines read from a text file
*/
Lines read_lines_from_file(char* filename) {
Lines lines;
strcpy(lines.lines[0], "A B");
strcpy(lines.lines[1], "C C");
strcpy(lines.lines[2], "B C");
return lines;
}
int find_score_according_to_strategy_guide(char lines[][4], int number_of_lines) {
int score_for_shape = 0;
int score_for_match_outcome = 0;
for (int i = 0; i < number_of_lines; i++) {
char* line = lines[i];
printf("line: %s\n", line);
Shape shape_selected_by_opponent = get_shape_from_id(line[0]);
Shape shape_to_respond_with = get_shape_from_id(line[2]);
score_for_shape += shape_to_respond_with.score;
int match_outcome = compare_shapes(shape_selected_by_opponent, shape_to_respond_with);
if (match_outcome > 0) {
score_for_match_outcome += 6;
} else if (match_outcome == 0) {
score_for_match_outcome += 3;
}
printf("score for shape: %d, score for match: %d\n", score_for_shape, score_for_match_outcome);
printf("shape opponent: %c, shape to respond: %c\n", shape_selected_by_opponent.id, shape_to_respond_with.id);
}
return score_for_shape + score_for_match_outcome;
}
int main(int argc, const char * argv[]) {
Lines lines = read_lines_from_file("filename");
int score = find_score_according_to_strategy_guide(lines.lines, 3);
printf("score: %d", score);
return 0;
}
@Ciro23
Copy link
Author

Ciro23 commented Nov 28, 2023

The file reading code is not actualing working, as the program actually just use some mock data for test.

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