-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2004 S3.cpp
165 lines (113 loc) · 5.51 KB
/
2004 S3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
2004 S3 - Spreadsheet
Difficulty: Medium
This question isn't too difficult, just very time consuming to implement.
Notice that the spreadsheet is only 10 x 9 therefore it suffices to brute force fill in the spreadsheet repeatedly.
1. Create two spreadsheets, one that stores the actual values and the other that stores the instructions
2. Iterate over the integer spreadsheet, if a value is undefined, attempt to solve for it. If you can't skip over it temporarily.
3. Keep repeating step 2 until no change is made after a full traversal. This means you've solved it to the best of your ability. Output the spreadsheet.
*/
#include <iostream>
#include <string>
#include <ctype.h>
#include <vector>
#include <map>
#define UNDEFINED -999999999 //Placeholder value indicating this cell is undefined
#define SOLVED "." //In the instructions spreadsheet, filler value
int main(){
std::vector<std::vector<int>> actual_spreadsheet (10, std::vector<int> (9, UNDEFINED)); //Store actual integer values to perform operations
std::vector<std::vector<std::string>> theoretical_spreadsheet (10, std::vector<std::string> (9, SOLVED)); //Store the string instructions
//I will be converting the instructions into something much easier to traverse, I will be ignoring addition signs thus creating a string that consists of pairs of integers.
//Eg. A1 -> 01, A1+A2+A3 -> 010203
std::map<char, char> letterstonum;
letterstonum['A'] = '0';
letterstonum['B'] = '1';
letterstonum['C'] = '2';
letterstonum['D'] = '3';
letterstonum['E'] = '4';
letterstonum['F'] = '5';
letterstonum['G'] = '6';
letterstonum['H'] = '7';
letterstonum['I'] = '8';
letterstonum['J'] = '9';
std::string value; //Store the given string
for (int i = 0; i < 10; i++){
for (int j = 0; j < 9; j++){
std::cin >> value;
//If the first letter of the string is a number, we know it's not an instruction
if (std::isdigit(value[0])){
actual_spreadsheet[i][j] = std::stoi(value);
theoretical_spreadsheet[i][j] = SOLVED;
}
else{
//Translate the string
std::string translated;
for (int k = 0; k < value.length(); k++){
//View the string as batches of 3, the first character will be the letter, the second is a number and the last is a +
if (k % 3 == 0){
translated += letterstonum[value[k]];
}
else if (k % 3 == 1){
translated += value[k];
}
}
theoretical_spreadsheet[i][j] = translated;
}
}
}
bool global_updated = true; //Indicates something in the spreadsheet was updated after that traversal
bool updated = false; //Indicates that when solving for a single cell, it is possible or not
while (global_updated){
global_updated = false;
//For each row
for (int i = 0; i < 10; i++){
//For each column
for (int j = 0; j < 9; j++){
//If that cell is undefined
if (actual_spreadsheet[i][j] == UNDEFINED){
//After translation, our instructions are now pairs of integers
int row, column, sum = 0; //Pair row and column
//For each character in string
for (int k = 0; k < theoretical_spreadsheet[i][j].length(); k++){
//The first number in the pair eg the row
if (k % 2 == 0){
row = theoretical_spreadsheet[i][j][k] - '0'; //We're performing this based on ASCII codes, '0' has code 48, minusing our char by '0' returns its int value
}
//The second number in the pair eg the column
else{
column = theoretical_spreadsheet[i][j][k] - '0' - 1; //-1 due to 1 indexing of the column input
//We know that the second number of each pair signals the end of that instruction/cell, attempt to figure out if the cell is defined
if (actual_spreadsheet[row][column] != UNDEFINED){
sum += actual_spreadsheet[row][column];
updated = true; //Possible
}
//Instruction cell is undefined
else{
updated = false; //Not possible to solve
break;
}
}
}
//If possible aka all instruction cells are defined
if (updated == true){
actual_spreadsheet[i][j] = sum;
global_updated = true; //Something was changed, search again to see if something is solvable with the new information
}
}
}
}
}
//Output spreadsheet
for (int i = 0; i < 10; i++){
for (int j = 0; j < 9; j++){
if (actual_spreadsheet[i][j] == UNDEFINED){
std::cout << '*' << ' ';
}
else{
std::cout << actual_spreadsheet[i][j] << ' ';
}
}
std::cout << '\n';
}
return 0;
}