Skip to main content
4 of 8
improved formatting

###EDIT:

#JavaScript (ES6) 336 bytes

v=s=>{d=[[48,44,59,121,92,62],[75,98,94,54,9],[107,110,29],[5,127,132,120,8,119],[114,18,63],[7,152],[56,77,68],[147,104,142,17,81],[143,69,85,27],[55],[50],[11],[82],[146,101],,[25],,[4,70]];d[26]=[148,71];d[35]=[19];d[52]=[51];d.map((a,j)=>{if(a&&~a.indexOf(s.split('').reduce((S,c,i)=>S*32+c.charCodeAt(0),0)%153))o=j});return o+3};

Usage:

v("New York")

Expanded version

v= s => {
    d=[[48,44,59,121,92,62],[75,98,94,54,9],[107,110,29],[5,127,132,120,8,119],[114,18,63],[7,152],[56,77,68],[147,104,142,17,81],[143,69,85,27],[55],[50],[11],[82],[146,101],,[25],,[4,70]];d[26]=[148,71];
    d[35]=[19];
    d[52]=[51];
    d.map( (a,j)=> {
         if ( a &&
              ~a.indexOf(s.split('').reduce((S,c,i)=>S*32+c.charCodeAt(0),0) %153) )
              o=j
    });
    return o+3
};

Approach:

Preprocessing:

Calculate an hash of each state name and store them in an array where one dimension is the number of votes the state has.

Processing:

Recalculate the hash and retrieve the information.

An hash of a state name is calculated with s.split('').reduce((S,c,i)=>S*32+c.charCodeAt(0),0)%153.

It transform "Iowa" in (32^3*'I' + 32^2*'o' + 32*'w' + 'a')%153 (with ascii value for characters).

Why 32 and 153? Because after a few empiric tests those values minimize hashes without collision between states that have different number of votes.

I don't believe that it will be hard to do something shorter with a better approach but since I spent a few time on it ;).