Last active
June 3, 2020 05:00
-
-
Save chadluo/b5ee2d1627107d1b806703e36ee68534 to your computer and use it in GitHub Desktop.
enumerate electoral college ties
This file contains 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
"use strict"; | |
const tie_amount = 538 / 2; | |
// see https://en.wikipedia.org/wiki/United_States_Electoral_College#Current_electoral_vote_distribution | |
const counts = { | |
California: 55, | |
Texas: 38, | |
Florida: 29, | |
New_York: 29, | |
Illinois: 20, | |
Pennsylvania: 20, | |
Ohio: 18, | |
Georgia: 16, | |
Michigan: 16, | |
North_Carolina: 15, | |
New_Jersey: 14, | |
Virginia: 13, | |
Washington: 12, | |
Arizona: 11, | |
Indiana: 11, | |
Massachusetts: 11, | |
Tennessee: 11, | |
Maryland: 10, | |
Minnesota: 10, | |
Missouri: 10, | |
Wisconsin: 10, | |
Alabama: 9, | |
Colorado: 9, | |
South_Carolina: 9, | |
Kentucky: 8, | |
Louisiana: 8, | |
Connecticut: 7, | |
Oklahoma: 7, | |
Oregon: 7, | |
Arkansas: 6, | |
Iowa: 6, | |
Kansas: 6, | |
Mississippi: 6, | |
Nevada: 6, | |
Utah: 6, | |
Nebraska: 5, | |
New_Mexico: 5, | |
West_Virginia: 5, | |
Hawaii: 4, | |
Idaho: 4, | |
Maine: 4, | |
New_Hampshire: 4, | |
Rhode_Island: 4, | |
Alaska: 3, | |
Delaware: 3, | |
DC: 3, | |
Montana: 3, | |
North_Dakota: 3, | |
South_Dakota: 3, | |
Vermont: 3, | |
Wyoming: 3, | |
}; | |
function tie() { | |
let entries = Object.entries(counts); | |
let flags = Array(entries.length - 1).fill(0); | |
let tie_combinations = 0; | |
while (!isAllSet(flags)) { | |
let result = 0; | |
let states = []; | |
for (let i = 0; i < entries.length; i++) { | |
if (flags[i] === 1) { | |
states.push(entries[i]); | |
result += entries[i][1]; | |
} | |
} | |
if (result === tie_amount) { | |
tie_combinations++; | |
} | |
inc(flags); | |
} | |
console.log(tie_combinations); | |
} | |
tie(); | |
function inc(flags) { | |
let c = 0; | |
while (flags[c] !== 0) { | |
flags[c] = 0; | |
c++; | |
} | |
flags[c] = 1; | |
} | |
function isAllSet(flags) { | |
for (let i = 0; i < flags.length; i++) if (flags[i] === 0) return false; | |
return true; | |
} |
This file contains 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
import java.util.Set; | |
import java.util.stream.*; | |
public class Ties { | |
// https://en.wikipedia.org/wiki/United_States_Electoral_College#Current_electoral_vote_distribution | |
private static final String[] STATES = { | |
"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", | |
"KS", "KY", "LA", "ME", "ME-1", "ME-2", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NE-1", | |
"NE-2", "NE-3", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", | |
"SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY" | |
}; | |
private static final int[] EC_VOTES = { | |
9, 3, 11, 6, 55, 9, 7, 3, 3, 29, 16, 4, 4, 20, 11, 6, 6, 8, 8, 2, 1, 1, 10, 11, 16, 10, 6, 10, | |
3, 2, 1, 1, 1, 6, 4, 14, 5, 29, 15, 3, 18, 7, 7, 20, 4, 9, 3, 11, 38, 6, 3, 13, 12, 5, 10, 3 | |
}; | |
private static final String[] SWING = { | |
"CO", "FL", "IA", "MI", "MN", "NC", "NH", "NV", "OH", "PA", "VA", "WI" | |
}; | |
private static final long SWING_FLAG = flags(SWING); | |
private static final String[] DEM = { | |
"CA", "CT", "DC", "DE", "HI", "IL", "MA", "MD", "ME", "NJ", "NM", "NY", "OR", "RI", "VT", "WA" | |
}; | |
private static final long DEM_FLAG = flags(DEM); | |
private static final String[] GOP = { | |
"AK", "AL", "AR", "AZ", "GA", "ID", "IN", "KS", "KY", "LA", "MO", "MS", "MT", "ND", "NE", "OK", | |
"SC", "SD", "TN", "TX", "UT", "WV", "WY" | |
}; | |
private static final long GOP_FLAG = flags(GOP); | |
public static void main(String[] args) { | |
long count = | |
LongStream.rangeClosed(DEM_FLAG, DEM_FLAG | SWING_FLAG) | |
.parallel() | |
.filter(Ties::typicalRedBlue) | |
.filter(Ties::isTie) | |
// .peek(flags -> System.out.println(showStates(flags))) | |
.count(); | |
System.out.println(count + " tie cases"); | |
} | |
private static boolean typicalRedBlue(long flags) { | |
return (flags & DEM_FLAG) == DEM_FLAG && (flags & GOP_FLAG) == 0; | |
} | |
private static boolean isTie(long flags) { | |
return IntStream.range(0, EC_VOTES.length) | |
.filter(i -> match(flags, i)) | |
.map(i -> EC_VOTES[i]) | |
.sum() | |
== 269; | |
} | |
private static boolean match(long flags, int candidateIndex) { | |
return (flags & (1 << candidateIndex)) != 0; | |
} | |
private static String showStates(long flags) { | |
return IntStream.range(0, STATES.length) | |
.filter(i -> match(flags, i)) | |
.mapToObj(i -> STATES[i]) | |
.collect(Collectors.joining(", ")); | |
} | |
private static String showFLag(long flag) { | |
return String.format("%64s", Long.toBinaryString(flag)); | |
} | |
private static long flags(String[] states) { | |
Set<String> statesSet = Set.of(states); | |
return IntStream.range(0, STATES.length) | |
.filter(i -> statesSet.contains(STATES[i])) | |
.mapToLong(i -> (1L << i)) | |
.reduce(0L, (a, b) -> a ^ b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment