Created
July 16, 2022 13:10
-
-
Save spidey/8124d8a98b68dca2c7b6f7f8d5c105d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <unistd.h> | |
| #include <stdlib.h> | |
| /* | |
| * board indexes | |
| * | |
| * 00 01 02 03 | |
| * 04 05 06 07 | |
| * 08 09 10 11 | |
| * 12 13 14 15 | |
| */ | |
| int view_indexes[][4] = { | |
| {0, 4, 8, 12}, | |
| {1, 5, 9, 13}, | |
| {2, 6, 10, 14}, | |
| {3, 7, 11, 15}, | |
| {12, 8, 4, 0}, | |
| {13, 9, 5, 1}, | |
| {14, 10, 6, 2}, | |
| {15, 11, 7, 3}, | |
| {0, 1, 2, 3}, | |
| {4, 5, 6, 7}, | |
| {8, 9, 10, 11}, | |
| {12, 13, 14, 15}, | |
| {3, 2, 1, 0}, | |
| {7, 6, 5, 4}, | |
| {11, 10, 9, 8}, | |
| {15, 14, 13, 12} | |
| }; | |
| void error(int code) | |
| { | |
| write(1, "Error\n", 6); | |
| exit(code); | |
| } | |
| void parse_input(char *argv[], int views[16]) | |
| { | |
| int i; | |
| i = 1; | |
| while (i <= 16) | |
| { | |
| if (argv[i][0] < '1' || argv[i][0] > '4' || argv[i][1] != '\0') | |
| { | |
| error(100 + i); | |
| } | |
| views[i - 1] = (argv[i][0] - '0'); | |
| i++; | |
| } | |
| } | |
| void print_board(int board[16]) | |
| { | |
| int row; | |
| row = 0; | |
| while (row < 4) | |
| { | |
| char c; | |
| int col; | |
| col = 0; | |
| while (col < 3) | |
| { | |
| c = '0' + board[4*row + col]; | |
| write(1, &c, 1); | |
| write(1, " ", 1); | |
| col++; | |
| } | |
| c = '0' + board[4*row + col]; | |
| write(1, &c, 1); | |
| write(1, "\n", 1); | |
| row++; | |
| } | |
| } | |
| void generate_board(long n, int board[16]) | |
| { | |
| int i; | |
| i = 0; | |
| while (i < 16) | |
| { | |
| int cell; | |
| cell = (n >> (30 - 2*i)); | |
| cell &= 0x03; | |
| board[i] = (cell + 1); | |
| i++; | |
| } | |
| } | |
| void check_views(int board[16], int views[16]) | |
| { | |
| int view; | |
| view = 0; | |
| while (view < 16) | |
| { | |
| int cell_index_list_index; | |
| int visible_boxes; | |
| int higher_box; | |
| visible_boxes = 1; | |
| higher_box = board[view_indexes[view][0]]; | |
| cell_index_list_index = 1; | |
| while (cell_index_list_index < 4) | |
| { | |
| int box_height = board[view_indexes[view][cell_index_list_index]]; | |
| if (box_height > higher_box) | |
| { | |
| higher_box = box_height; | |
| visible_boxes++; | |
| } | |
| cell_index_list_index++; | |
| } | |
| if (visible_boxes != views[view]) | |
| { | |
| break; | |
| } | |
| view++; | |
| } | |
| if (view == 16) | |
| { | |
| print_board(board); | |
| exit(0); | |
| } | |
| } | |
| void find_solution(int views[16]) | |
| { | |
| int board[16]; | |
| long n; | |
| n = 0; | |
| while (n < 4294967296) | |
| { | |
| generate_board(n, board); | |
| check_views(board, views); | |
| n++; | |
| } | |
| error(2); | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| if (argc != 17) | |
| { | |
| error(1); | |
| } | |
| int views[16]; | |
| parse_input(argv, views); | |
| find_solution(views); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment