fixed wires puzzle

This commit is contained in:
Mitchell Marino 2024-08-04 22:34:57 -05:00
parent ce54b8cda3
commit 370b28813b

View File

@ -27,10 +27,10 @@ void wires_to_string(WireColor* wires, char* out_wires_string) {
out_wires_string[i] = 'g'; out_wires_string[i] = 'g';
break; break;
case WireColor::blue: case WireColor::blue:
out_wires_string[i] = 'u'; out_wires_string[i] = 'b';
break; break;
case WireColor::black: case WireColor::black:
out_wires_string[i] = 'b'; out_wires_string[i] = 'k';
break; break;
case WireColor::white: case WireColor::white:
out_wires_string[i] = 'w'; out_wires_string[i] = 'w';
@ -60,10 +60,10 @@ void string_to_wires(char* wires_string, WireColor* out_wires) {
case 'g': case 'g':
out_wires[i] = WireColor::green; out_wires[i] = WireColor::green;
break; break;
case 'u': case 'b':
out_wires[i] = WireColor::blue; out_wires[i] = WireColor::blue;
break; break;
case 'b': case 'k':
out_wires[i] = WireColor::black; out_wires[i] = WireColor::black;
break; break;
case 'w': case 'w':
@ -176,37 +176,45 @@ void solve_wires(WireColor* wires, bool* out_cut) {
// CUT CHECKS // CUT CHECKS
// cut the second wire of all most common colors // 1. cut the second wire of all most common colors
int max_len = max(red_pos_len, yellow_pos_len, green_pos_len, blue_pos_len, black_pos_len, white_pos_len); int max_len = max(red_pos_len, yellow_pos_len, green_pos_len, blue_pos_len, black_pos_len, white_pos_len);
if (max_len >= 2) { if (max_len >= 2) {
for (int i = 0; i < NUM_COLORS; i++) { for (int i = 0; i < NUM_COLORS; i++) {
if (list_pos_len[i] == max_len) { if (list_pos_len[i] == max_len) {
int idx = list_pos[i][1]; int idx = list_pos[i][1];
out_cut[idx] = true; out_cut[idx] = true;
ESP_LOGI(TAG, "1. cutting %d", idx);
} }
} }
} }
// cut the first wire if it is green or white // 2. cut the first wire if it is green or white
if (wires[0] == WireColor::green || wires[0] == WireColor::white) out_cut[0] = true; if (wires[0] == WireColor::green || wires[0] == WireColor::white) {
out_cut[0] = true;
// cut blue wires in even positions (odd indexes) ESP_LOGI(TAG, "2. cutting %d", 0);
for (int i = 1; i < NUM_WIRES; i += 2) {
if (wires[i] == WireColor::blue) out_cut[i] = true;
} }
// cut black and yellow wires next to black and yellow wires // 3. cut blue wires in even positions (odd indexes)
for (int i = 1; i < NUM_WIRES; i += 2) {
if (wires[i] == WireColor::blue) {
out_cut[i] = true;
ESP_LOGI(TAG, "3. cutting %d", i);
}
}
// 4. cut black and yellow wires next to black and yellow wires
for (int i = 0; i < NUM_WIRES-1; i++) { for (int i = 0; i < NUM_WIRES-1; i++) {
if ( if (
(wires[i] == WireColor::yellow || wires[i] == WireColor::black) && (wires[i] == WireColor::yellow || wires[i] == WireColor::black) &&
(wires[i+1] == WireColor::yellow || wires[i+1] == WireColor::black) (wires[i+1] == WireColor::yellow || wires[i+1] == WireColor::black)
) { ) {
out_cut[i] = true; out_cut[i] = true;
out_cut[1] = true; out_cut[i+1] = true;
ESP_LOGI(TAG, "4. cutting %d, %d", i, i+1);
} }
} }
// cut the last green wire next to a yellow or white wire // 5. cut the last green wire next to a yellow or white wire
for (int green_idx = green_pos_len-1; green_idx >= 0; green_idx--) { for (int green_idx = green_pos_len-1; green_idx >= 0; green_idx--) {
int pos = green_pos[green_idx]; int pos = green_pos[green_idx];
if ( if (
@ -216,23 +224,26 @@ void solve_wires(WireColor* wires, bool* out_cut) {
wires[pos+1] == WireColor::white wires[pos+1] == WireColor::white
) { ) {
out_cut[pos] = true; out_cut[pos] = true;
ESP_LOGI(TAG, "5. cutting %d", pos);
break; break;
} }
} }
// cut all white wires if there is a red wire in the 5th position // 6. cut all white wires if there is a red wire in the 5th position
if (wires[4] == WireColor::red) { if (wires[4] == WireColor::red) {
for (int white_idx = 0; white_idx < white_pos_len; white_idx++) { for (int white_idx = 0; white_idx < white_pos_len; white_idx++) {
out_cut[white_pos[white_idx]] = true; out_cut[white_pos[white_idx]] = true;
ESP_LOGI(TAG, "6. cutting %d", white_pos[white_idx]);
} }
} }
// cut the first black wire if there are more white wires than green wires // 7. cut the first black wire if there are more white wires than green wires
if (white_pos_len > green_pos_len && black_pos_len > 0) { if (white_pos_len > green_pos_len && black_pos_len > 0) {
out_cut[black_pos[0]] = true; out_cut[black_pos[0]] = true;
ESP_LOGI(TAG, "7. cutting %d", black_pos[0]);
} }
// cut all wires in an alternating pattern of 2 colors at least 4 wires long // 8. cut all wires in an alternating pattern of 2 colors at least 4 wires long
for (int i = 0; i < NUM_WIRES-3; i++) { for (int i = 0; i < NUM_WIRES-3; i++) {
if ( if (
wires[i] == wires[i+2] && wires[i] == wires[i+2] &&
@ -243,35 +254,40 @@ void solve_wires(WireColor* wires, bool* out_cut) {
out_cut[i+1] = true; out_cut[i+1] = true;
out_cut[i+2] = true; out_cut[i+2] = true;
out_cut[i+3] = true; out_cut[i+3] = true;
ESP_LOGI(TAG, "8. cutting %d, %d, %d, %d", i, i+1, i+2, i+3);
} }
} }
// cut any wires if their position matches the number of letters in the color's name // 9. cut any wires if their position matches the number of letters in the color's name
for (int i = 0; i < NUM_WIRES; i++) { for (int i = 0; i < NUM_WIRES; i++) {
if (color_name_len[i] == i+1) { if (color_name_len[wires[i]] == i+1) {
out_cut[i] = true; out_cut[i] = true;
ESP_LOGI(TAG, "9. cutting %d", i);
} }
} }
// cut all red wires if there are no more than 2 wires of the same color // 10. cut all red wires if there are no more than 2 wires of the same color
if (max_len <= 2) { if (max_len <= 2) {
for (int i = 0; i < red_pos_len; i++) { for (int i = 0; i < red_pos_len; i++) {
out_cut[red_pos[i]] = true; out_cut[red_pos[i]] = true;
ESP_LOGI(TAG, "10. cutting %d", red_pos[i]);
} }
} }
// cut the last wire if it is the same color as the first wire // 11. cut the last wire if it is the same color as the first wire
if (wires[0] == wires[NUM_WIRES-1]) { if (wires[0] == wires[NUM_WIRES-1]) {
out_cut[NUM_WIRES-1] = true; out_cut[NUM_WIRES-1] = true;
ESP_LOGI(TAG, "11. cutting %d", NUM_WIRES-1);
} }
// cut any wire adjacent to both a yellow and blue wire // 12. cut any wire adjacent to both a yellow and blue wire
for (int i = 0; i < NUM_WIRES-2; i++) { for (int i = 0; i < NUM_WIRES-2; i++) {
if ( if (
(wires[i] == WireColor::yellow && wires[i+2] == WireColor::blue) || (wires[i] == WireColor::yellow && wires[i+2] == WireColor::blue) ||
(wires[i] == WireColor::blue && wires[i+2] == WireColor::white) (wires[i] == WireColor::blue && wires[i+2] == WireColor::white)
) { ) {
out_cut[i+1] = true; out_cut[i+1] = true;
ESP_LOGI(TAG, "12. cutting %d", i+1);
} }
} }