Skip to content

Instantly share code, notes, and snippets.

@TylerLeite
Created July 22, 2020 22:44
Show Gist options
  • Save TylerLeite/9fa5edc60d4db70e341f65c5d4b79155 to your computer and use it in GitHub Desktop.
Save TylerLeite/9fa5edc60d4db70e341f65c5d4b79155 to your computer and use it in GitHub Desktop.
rent 2.0: now featuring comments!
// Enter data here:
const total_rent = 0;
// A measure of the relative quality between different rooms in the same type.
// Probably will just go with area for these, but if theres like e.g. a really
// shitty bathroom no one wants to use, it might make sense to lower its quality
// NOTE: quality of two different types (e.g. a bedroom and bathroom) have nothing
// to do with each other and will never be compared. Because of this, rooms
// that are one-of-a-type can have their quality left null
const room_qualities = {
bedroom_1: null,
bedroom_2: null,
bedroom_3: null,
bathroom_1: null,
bathroom_2: null,
kitchen_1: null,
livingroom_1: null,
livingroom_2: null,
livingroom_3: null,
gym_1: null,
patio_1: null,
laundry_1: null,
}
// Weights for the total rent should be split up by room. E.g. if everyone decides
// bedroom size/quality is the most important factor in rent-paying, bedrooms should
// be weighted higher
const room_type_weights = {
bedroom: 0.45,
bathroom: 0.20,
kitchen: 0.05,
livingroom: 0.07,
patio: 0.03,
yard: 0.10,
laundry: 0.03,
gym: 0.07,
}
// NOTE: weights must sum to one. just in case, they will be normalized here:
let room_type_weight_sum = 0;
for (const type in room_type_weights) {
room_type_weight_sum += room_type_weights[type];
}
for (const type in room_type_weights) {
room_type_weights[type] /= room_type_weight_sum;
}
// For each room, list all people who have "ownership" over that room. e.g. two
// people sharing a bathroom would both be in that bathroom's list. someone with
// their own bedroom should be the only person in that bedroom's list.
// To make life easier and help prevent typos, make consts for each person, e.g.
// const a = 'alice';
// const b = 'bob';
// const c = 'cathy';
// etc.
const room_shares = {
bedroom_1: [],
bedroom_2: [],
bedroom_3: [],
bathroom_1: [],
bathroom_2: [],
kitchen_1: [],
livingroom_1: [],
patio_1: [],
laundry_1: [],
}
// And then the magic happens
// Generate a map from room types to a list of specific rooms of that type
const room_type_map = {};
for (const room in room_shares) {
const type = room.split('_')[0];
if (typeof room_type_map[type] === 'undefined') {
room_type_map[type] = [];
}
room_type_map[type].push(room);
}
// Calculate the percentage of each room type associated with each room of that type
const room_percents = {}
for (const type in room_type_map) {
const rooms = room_type_map[type]
if (room_qualities[rooms[0]] === null) {
for (const room of rooms) {
room_percents[room] = room_type_weights[type] * 1/room_type_map[type].length;
}
} else {
let sum = rooms.slice().map(room => room_qualities[room]).reduce((a, b) => a+b);
console.log(sum);
for (const room of rooms) {
room_percents[room] = room_type_weights[type] * room_qualities[room]/sum;
}
}
}
// Calculate how much of each room each person "owns"
const person_shares = {}
for (const room in room_shares) {
const people = room_shares[room];
for (let person of people) {
if (typeof person_shares[person] === 'undefined') {
person_shares[person] = 0;
}
person_shares[person] += room_percents[room] / people.length
}
}
// Map those ownership percentages to the total rent
const final_rents = {}
for (const person in person_shares) {
const exact_rent = total_rent * person_shares[person];
final_rents[person] = Math.round(exact_rent*100)/100; // round to the nearest cent
}
// The moment of truth:
console.log(final_rents);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment