|
| 1 | +// DESIGNED FOR C23. |
| 2 | +// @celeste_vandamme (github) |
| 3 | + |
| 4 | +/// --- LIBRARIES |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | + |
| 8 | + |
| 9 | +/// --- STRUCTURES |
| 10 | +typedef struct Instruction { |
| 11 | + char direction; // L or R |
| 12 | + unsigned short int degree; // Amount of rotation/click in said direction |
| 13 | +} Instruction; |
| 14 | + |
| 15 | + |
| 16 | +/// --- FUNCTIONS PROTOTYPES |
| 17 | +int int_wrapper(const int value, const int min, const int max) ; |
| 18 | +void password_0x434C49434B(int *password, const int dial_pos, const Instruction line_instruction) ; |
| 19 | + |
| 20 | +/// --- MAIN |
| 21 | +int main(void) |
| 22 | +{ |
| 23 | + // --- 1. Major variables |
| 24 | + const bool debug_mode = false ; |
| 25 | + constexpr int initial_dial_pos = 50 ; |
| 26 | + const char file_directory[100] = "input.txt" ; |
| 27 | + const int line_max_size = 100 ; |
| 28 | + |
| 29 | + // --- 2. Variables |
| 30 | + int final_password = 0 ; |
| 31 | + int final_password_0x434C49434B = 0 ; |
| 32 | + int dial_pos = initial_dial_pos ; |
| 33 | + |
| 34 | + int i = 0; |
| 35 | + Instruction line_instruction ; |
| 36 | + |
| 37 | + FILE *input_file = NULL ; |
| 38 | + char line_input[line_max_size] ; |
| 39 | + |
| 40 | + |
| 41 | + // --- 2. Process |
| 42 | + input_file = fopen(file_directory, "r" ); |
| 43 | + |
| 44 | + if (input_file == NULL) |
| 45 | + { |
| 46 | + printf(">> File not found/an error happened.\n" |
| 47 | + "Please make sure the file_directory variable is set correctly.\n\n" |
| 48 | + "Directory: '%s'\n", file_directory ) ; |
| 49 | + |
| 50 | + return EXIT_FAILURE ; |
| 51 | + } |
| 52 | + |
| 53 | + else |
| 54 | + { |
| 55 | + /// --- 2.a. Reading the input.txt data |
| 56 | + while ( fgets(line_input, line_max_size, input_file) != NULL ) |
| 57 | + { |
| 58 | + |
| 59 | + if (debug_mode) { |
| 60 | + printf("\n>>> %s", line_input ) ; |
| 61 | + } |
| 62 | + |
| 63 | + // Direction: 'L' or 'R' |
| 64 | + line_instruction.direction = line_input[0] ; |
| 65 | + |
| 66 | + // Degree of rotation? |
| 67 | + line_instruction.degree = 0 ; |
| 68 | + |
| 69 | + for (i=1; line_input[i] != '\n'; i++) |
| 70 | + { |
| 71 | + // Make space for the new value -&- Convert '4' to 4 (char > int) |
| 72 | + line_instruction.degree = (line_instruction.degree * 10) + (line_input[i] - '0') ; |
| 73 | + } |
| 74 | + |
| 75 | + if (debug_mode) |
| 76 | + { |
| 77 | + printf("direction = '%c' / value = '%i'\n", line_instruction.direction, line_instruction.degree) ; |
| 78 | + } |
| 79 | + |
| 80 | + /// 2. d. "Real" password with the method: "0x434C49434B" |
| 81 | + password_0x434C49434B( |
| 82 | + &final_password_0x434C49434B, |
| 83 | + dial_pos, |
| 84 | + line_instruction |
| 85 | + ) ; |
| 86 | + |
| 87 | + if (debug_mode) |
| 88 | + { |
| 89 | + printf("Password v2 end: %i\n", final_password_0x434C49434B ) ; |
| 90 | + } |
| 91 | + |
| 92 | + /// --- 2.b. Applying the instruction |
| 93 | + // Applying the degree to the dial... |
| 94 | + dial_pos += (1 - 2 * (line_instruction.direction == 'L') ) * line_instruction.degree ; |
| 95 | + |
| 96 | + if (debug_mode) |
| 97 | + { |
| 98 | + printf("Dial unwrap = %i\n", dial_pos) ; |
| 99 | + } |
| 100 | + |
| 101 | + // Then restraining the value between [0;99]. |
| 102 | + dial_pos = int_wrapper(dial_pos, 0, 99) ; |
| 103 | + |
| 104 | + if (debug_mode) |
| 105 | + { |
| 106 | + printf("dial = %i\n", dial_pos ) ; |
| 107 | + } |
| 108 | + |
| 109 | + /// 2. c. "Real" password here! |
| 110 | + /// When the dial is = 0°. |
| 111 | + if ( dial_pos == 0 ) |
| 112 | + { |
| 113 | + final_password++ ; |
| 114 | + } |
| 115 | + |
| 116 | + if ( debug_mode ) |
| 117 | + { |
| 118 | + printf("final_password = %i\n", final_password ) ; |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + // --- 3. Result display |
| 126 | + printf("Dial position as told on the instruction:\n" |
| 127 | + "DIAL = %i\n\n", dial_pos) ; |
| 128 | + |
| 129 | + printf("Secret password:\n> '%i'\n\n", final_password); |
| 130 | + |
| 131 | + printf("Secret password (method '0x434C49434B'):\n> '%i'\n\n", final_password_0x434C49434B); |
| 132 | + |
| 133 | + // --- 4. End of program |
| 134 | + return 0; |
| 135 | +} |
| 136 | + |
| 137 | +/// --- FUNCTIONS |
| 138 | + |
| 139 | +int int_wrapper(const int value, const int min, const int max) |
| 140 | +{ |
| 141 | + const int range = max - min + 1; |
| 142 | + return ((((value - min) % range) + range) % range) + min ; |
| 143 | +} |
| 144 | + |
| 145 | +void password_0x434C49434B(int *password, int dial_pos, const Instruction line_instruction) |
| 146 | +{ |
| 147 | + // |
| 148 | + const bool wasAtZero = (dial_pos == 0) ; |
| 149 | + |
| 150 | + // Applying the instruction but not wrapping it directly. |
| 151 | + dial_pos += (1 - 2 * (line_instruction.direction == 'L') ) * line_instruction.degree ; |
| 152 | + |
| 153 | + |
| 154 | + // We add ONE CLICK, then we check whether we still go above 100? (+1 click each time) |
| 155 | + if (dial_pos <= 0 ) |
| 156 | + { |
| 157 | + if ( !wasAtZero ) |
| 158 | + { |
| 159 | + *password += 1 ; |
| 160 | + } |
| 161 | + |
| 162 | + dial_pos = abs(dial_pos) ; |
| 163 | + } |
| 164 | + |
| 165 | + *password += dial_pos / 100 ; |
| 166 | +} |
0 commit comments