Skip to content

Instantly share code, notes, and snippets.

@nathggns
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathggns/5d1d70a6f2369c4b8df8 to your computer and use it in GitHub Desktop.
Save nathggns/5d1d70a6f2369c4b8df8 to your computer and use it in GitHub Desktop.
Calculate which Conservative seats Labour would have won with the votes from the Green Party. #GE2015

Calculate which Conservative seats Labour would have won with the votes from the Green Party.

  • Requires babel with the setting of stage 0.
  • Requires q-io && lodash
  • Requires my results.json compilation of the 2015 General Election (#GE2015)

You can plug in a different json file in the same format as mine, or change the variables on lines 4-6 to work out the results for different combinations and elections.

Example:

Nathaniels-MacBook-Pro:calc Nathaniel$ node run.js 
Amount of seats: 10 [ 'Bedford',
  'Brighton Kemptown',
  'Bury North',
  'Croydon Central',
  'Derby North',
  'Gower',
  'Morley & Outwood',
  'Plymouth Sutton & Devonport',
  'Telford',
  'Weaver Vale' ]
require('babel/register')({ stage : 0 });
require('./seats.es7');
import fs from 'q-io/fs';
import { pairs } from 'lodash';
const wouldVote = ['(LAB)'];
const didVote = '(GRN)';
const didWin = '(CON)';
function wouldWinIfVotedTactically([name, results]) {
const getVotes = name => ([for (result of results) if (result.name === name) result.votes][0]);
const didVoteVotes = getVotes(didVote);
const didWinVotes = getVotes(didWin);
return [
for (party of wouldVote)
(getVotes(party) + didVoteVotes) > didWinVotes
].indexOf(true) > -1
}
(async function() {
try {
const consts = JSON.parse(await fs.read('results.json'));
const seats = [
for ([name, results] of pairs(consts))
if (results[0].name === didWin && wouldWinIfVotedTactically([name, results]))
name
];
console.log(`Amount of seats: ${seats.length}`, seats);
} catch (e) {
console.error(e.stack || e);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment