Skip to content

Instantly share code, notes, and snippets.

@RobertWarrenGilmore
Last active October 2, 2015 22:53
Show Gist options
  • Save RobertWarrenGilmore/2a0843a53962ab717c26 to your computer and use it in GitHub Desktop.
Save RobertWarrenGilmore/2a0843a53962ab717c26 to your computer and use it in GitHub Desktop.
Find the optimal PugetPass value to buy, based on what tickets make up your commute.
'use strict';
let PASS_MINIMUM = 50;
let PASS_MAXIMUM = 550;
let PASS_INCREMENT = 25;
// Loop over the possible pass values.
let perPassValue = function(callback) {
for (let passValue = PASS_MINIMUM; passValue <= PASS_MAXIMUM; passValue += PASS_INCREMENT) {
callback(passValue);
}
};
// Loop over the possible commute day counts per month.
let perDays = function(callback) {
for (let days = 1; days <= 31; ++days) {
callback(days);
}
};
let passCost = function(passValue) {
return passValue * 36;
};
let dailyCost = function(trips, passValue) {
let cost = 0;
for (let trip of trips) {
cost += Math.max(trip - passValue, 0);
}
return cost;
};
let monthlyCost = function(days, trips, passValue) {
return (days * dailyCost(trips, passValue)) + passCost(passValue);
};
let monthlySavings = function(days, trips, passValue) {
// Compare a pass of value 0 (no pass) with the given pass.
return monthlyCost(days, trips, 0) - monthlyCost(days, trips, passValue);
};
let monthlySavingsTable = function(trips) {
let table = {};
perDays(function(days) {
table[days] = {};
let optimalPassValue = 0;
let optimalAmount = 0;
perPassValue(function(passValue) {
let amount = monthlySavings(days, trips, passValue);
table[days][passValue] = {
amount: amount,
percentage: Math.round(amount / monthlyCost(days, trips, 0) * 100),
noSavings: amount <= 0
};
if (amount > optimalAmount) {
optimalPassValue = passValue;
optimalAmount = amount;
}
});
if (optimalPassValue) {
table[days][optimalPassValue].isOptimal = true;
}
});
return table;
};
let dollars = function(cents) {
return (cents / 100).toFixed(2);
}
let print = function(message) {
process.stdout.write(message);
};
// Print the table.
let printTable = function(trips) {
let savingsTable = monthlySavingsTable(trips);
print('\tPugetPass value\n');
perPassValue(function(passValue) {
print('\t' + dollars(passValue));
});
print('\ncommutes per month\n');
perDays(function(days) {
print(days.toString());
perPassValue(function(passValue) {
print('\t');
let savings = savingsTable[days][passValue]
if (savings.isOptimal) {
print('\x1b[32m'); // Turn green if optimal.
} else if (savings.noSavings) {
print('\x1b[31m'); // Turn red if not positive.
}
print(dollars(savings.amount));
print('\x1b[0m'); // Clear colour.
});
print('\n');
perPassValue(function(passValue) {
print('\t');
let savings = savingsTable[days][passValue]
if (savings.isOptimal) {
print('\x1b[32m'); // Turn green if optimal.
} else if (savings.noSavings) {
print('\x1b[31m'); // Turn red if not positive.
}
print(savings.percentage + '%');
print('\x1b[0m'); // Clear colour.
});
print('\n\n');
});
};
print('On days when you commute, what is the price of each transit ride that you take? Include only rides on Community Transit, Everett Transit, King County Metro Transit, Kitsap Transit, Pierce Transit, or Sound Transit. On each line, enter a transit ticket price in the format #[.##] (example: 2.75 or 6). If you take a ride twice in a day, enter it twice. When you are done, enter a blank line.\n');
let trips = [];
process.stdin.on('readable', function() {
let chunk = process.stdin.read();
if (chunk != null) {
let line = String(chunk).trim();
if (!line.length) {
if (trips.length) {
print('The following table shows how much money you could save per month by buying a PugetPass of a given value. Red means no savings, and green means optimal savings.\n');
printTable(trips);
process.exit(0);
} else {
print('Please enter at least one trip.\n');
}
} else if (!line.match(/^\d{1,2}(\.\d{2})?$/)) {
print('That is an invalid format.\n');
} else {
// Add a trip.
trips.push(Math.round(Number.parseFloat(line) * 100));
}
}
});
@RobertWarrenGilmore
Copy link
Author

example output screenshot:

example output screenshot

@RobertWarrenGilmore
Copy link
Author

updated format:

pp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment