Skip to content

Commit

Permalink
Create simple wire solve executable
Browse files Browse the repository at this point in the history
  • Loading branch information
ahjmorton committed Apr 13, 2020
1 parent c784b8f commit af58b7e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ ADD_EXECUTABLE(password_solve password_solve.c)
ADD_EXECUTABLE(morse_code_solve morse_code_solve.c)
ADD_EXECUTABLE(maze_solve maze_solve.c)
ADD_EXECUTABLE(who_first_solve who_first_solve.c)
ADD_EXECUTABLE(simple_wire_solve simple_wire_solve.c)

target_link_libraries(password_solve PRIVATE lib_password_solve)
target_link_libraries(morse_code_solve PRIVATE lib_morse_code_solve)
target_link_libraries(maze_solve PRIVATE lib_maze_solve)
target_link_libraries(who_first_solve PRIVATE lib_who_first_solve)
target_link_libraries(simple_wire_solve PRIVATE lib_simple_wire_solve)
75 changes: 75 additions & 0 deletions cli/simple_wire_solve.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include "lib_simple_wire_solve.h"

int main(int argc, char **argv)
{
if(argc > 3 || argc < 2) {
return 1;
}

char * wire_str = argv[1];

uint8_t wire_length = strnlen(wire_str, 6);

if(wire_length < 3 || wire_length > 6) {
return 2;
}

enum simple_wire_colours wires[wire_length];
for(uint8_t i = 0; i < wire_length; i++) {
switch(tolower(wire_str[i])) {
case 'r':
wires[i] = C_Red;
break;
case 'w':
wires[i] = C_White;
break;
case 'y':
wires[i] = C_Yellow;
break;
case 'b':
wires[i] = C_Blue;
break;
case 'l':
wires[i] = C_Black;
break;
default :
return 3;
}
}

enum simple_wire_error_code error;
uint8_t result;
if(argc == 2) {
error = simple_wire_solve_no_serial(
wires,
wire_length,
&result
);
} else {
char * serial_number_str = argv[2];
if(strnlen(serial_number_str, 1) != 1) {
return 4;
}
char serial_number_digit = serial_number_str[0];
if(!isdigit(serial_number_digit)) {
return 5;
}
error = simple_wire_solve(
wires,
wire_length,
serial_number_digit - '0',
&result
);
}
if(error != E_None) {
return 10 + error;
} else {
printf("%i\n", result);
}

return 0;
}
2 changes: 1 addition & 1 deletion lib/simple_wire_solve.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static uint8_t solve_5_wire(
const uint8_t last_serial_number_digit) {
set_wire_counts(wire_counts, wires, 5);
if(wires[4] == C_Black && is_odd(last_serial_number_digit)) {
return 4;
return 3;
} else if(wire_counts[C_Red] == 1 && wire_counts[C_Yellow] > 1) {
return 0;
} else if(wire_counts[C_Black] == 0) {
Expand Down

0 comments on commit af58b7e

Please sign in to comment.