Last active
July 9, 2019 05:11
-
-
Save efruchter/4e748f9afe8e9073630bf8f075555d86 to your computer and use it in GitHub Desktop.
Load data file with info about who stayed on what night, and distribute a cost among everyone.
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 <iostream> | |
#include <fstream> | |
#include <string> | |
// convert 2D coordinates to 1D array index | |
int flat_idx(const int row, const int col, const int row_length) | |
{ | |
return (row * row_length) + col; | |
} | |
// Convert points into fractions of the whole | |
void as_proportions(const float* input, float* output, const int length) | |
{ | |
float sum = 0; | |
for (int i = 0; i < length; i++) | |
sum += input[i]; | |
for (int i = 0; i < length; i++) | |
output[i] /= sum; | |
} | |
int main() | |
{ | |
std::ifstream input("data.csv"); | |
float total_cost; | |
int total_days; | |
int total_people; | |
input >> total_cost; | |
input >> total_days; | |
input >> total_people; | |
std::string* names = new std::string[total_people]; | |
float* points = new float[total_people]; | |
int* daily_beds = new int[total_days * total_people]; | |
for (int i = 0; i < total_people; i++) | |
points[i] = 0; | |
for (int p = 0; p < total_people; p++) | |
{ | |
input >> names[p]; | |
for (int d = 0; d < total_days; d++) | |
{ | |
int bed; | |
input >> bed; | |
daily_beds[flat_idx(p, d, total_days)] = bed; | |
} | |
} | |
const int bed_locations = 5; | |
int beds[bed_locations]; | |
float bed_scale[bed_locations]; | |
for (int i = 1; i < bed_locations; i++) | |
input >> bed_scale[i]; | |
for (int day = 0; day < total_days; day++) | |
{ | |
for (int bed_idx = 0; bed_idx < bed_locations; bed_idx++) | |
beds[bed_idx] = 0; | |
for (int pIndex = 0; pIndex < total_people; pIndex++) | |
beds[daily_beds[flat_idx(pIndex, day, total_days)]]++; | |
for (int p = 0; p < total_people; p++) | |
{ | |
int bed_idx = daily_beds[flat_idx(p, day, total_days)]; | |
if (bed_idx != 0) | |
points[p] += bed_scale[bed_idx] / (float)sqrt(beds[bed_idx]); | |
} | |
} | |
as_proportions(points, points, total_people); | |
std::cout << std::endl << "Per Person Cost: " << std::endl; | |
for (int p = 0; p < total_people; p++) | |
std::cout << names[p] << ": $" << (points[p] * total_cost) << std::endl; | |
std::cout << "Raw Cost: " << total_cost << std::endl; | |
} |
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
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
4321.32 | |
6 | |
12 | |
AAA 1 1 1 1 1 1 | |
BBB 0 1 1 1 1 1 | |
CCC 2 2 2 2 2 2 | |
DDD 2 2 2 2 0 0 | |
EEE 2 2 2 2 2 2 | |
FFF 0 2 2 2 2 2 | |
GGG 0 2 2 2 2 2 | |
HHH 3 3 3 3 3 3 | |
JJJ 0 0 3 3 3 0 | |
KKK 4 4 4 4 2 2 | |
LLL 4 4 4 4 4 4 | |
MMM 4 4 4 4 4 4 | |
1 | |
1 | |
1 | |
0.5 |
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
// Key: | |
// Total Cost | |
// # nights | |
// # people | |
// Names + beds | |
// ... | |
// Biases per room (0 is skipped, represents "no room") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment