Make America(n Maps) Great Again

Python 626

In the approach below, I added .rstate and .bstate based on .state in the CSS description. In my I renamed the provided .svg file to v.svg. It takes an input as described below and writes to a file w.png. To transfer from the full state name to the abbreviated version I look them up based on the first and last two letters of the states.

r='Ama,Aka,Ana,Aas,Cia,Cdo,Cut,Dre,Fda,Gia,Hii,Iho,Iis,Ina,Iwa,Kas,Kky,Lna,Mne,Mnd,Mts,Man,Mta,Mpi,Mri,Mna,Nka,Nda,Nre,Ney,Nco,Nrk,Nna,Nta,Oio,Oma,Oon,Pia,Rnd,Sna,Sta,Tee,Tas,Uah,Vnt,Via,Won,Wia,Win,Wng'.split(',')
y='lkzraotelaidlnasyaedainsotevhjmycdhkraicdnxttaaviy'
v=open('v.svg','r')
s=v.read()
v.close()
k=s.find('.state')
j=s.find('.',k+1)
t=input().split(';')
w=open('w.svg','w')
k+=1
c='#E0E0E0'
s=s[:j]+'.r'+s[k:j].replace(c,'red')+'.b'+s[k:j].replace(c,'blue')+s[j:]
c='rb'
for j in range(2):
 for d in t[j].split(','):k=s.find('state '+d[0].lower()+y[r.index(d[0]+d[-2:])]);s=s[:k]+c[j]+s[k:]
w.write(s)
w.close()

Example input:

'California,Illinois,Iowa,Mississippi;New Mexico,Pennsylvania,South Dakota,Vermont'

Example output: output figure

Or inspired by the flag of France: output figure france


Processing, 425 bytes (259 bytes + 1 +165 byte file)

Code:

size(959,593);String[]a=loadStrings("a"),b=loadStrings("b");PShape m=loadShape("M.svg");m.disableStyle();for(int i=0;i<51;i++){fill(255);int r=0;for(String j:a){if(j.isEmpty())r++;if(j.contains(b[i]))fill(r>0?#0000FF:#FF0000);}shape(m.getChild(i));}save("m");

Blank map should be named "M.svg" and stored in a folder called /data (all other files are in the same folder as the program.)

Input File ("a"):

Mississippi
California
Connecticut

Delaware
Florida
Wyoming
Hawaii

Key File ("b"): http://pastebin.com/0pNufAH9

Output ("m.tif"):

enter image description here

Ok, here is my try at my own challenge. Some notes:

  • The output map looks different from the input map in the following ways
    1. The input map had grey filling on a transparent background. The output has white filling on a gray background. I think this should be allowed, because white, gray, and transparency are all neutral.
    2. The output map is missing the lines around Hawaii and Alaska that the input had. Once again, I think this is ok because the lines are not a significant part of the map.
  • The program uses an external file to hold keys. According to this meta post, I just need to add 1 byte for one additional file.

If anyone has any discrepancies with my self-adjudication of my code, feel free to leave a comment.

Also, if anyone is curious about trying this challenge in Processing, it supports both reading SVG files into PShape's, as well as parsing SVG files as XML.


PHP, 714 bytes

The output is the blank SVG file, which should be stored in a file named a, with additional CSS to colour the states, which should be stored in a file named b in the following format:

Ohio0Indiana0Illinois1New York0New Jersey0Florida

I've added some newlines for readability.

<?
$x=str_replace;echo$x('.b','#'.$x([0,1],[',#','{fill:red}#'],$x(split(0,'Alabama0Alaska0
Arizona0Arkansas0California0Colorado0Connecticut0Delaware0Florida0Georgia0Hawaii0Idaho0Illin
ois0Indiana0Iowa0Kansas0Kentucky0Louisiana0Maine0Maryland0Massachusetts0Michigan0Minnesota0M
ississippi0Missouri0Montana0Nebraska0Nevada0New Hampshire0New Jersey0New Mexico0New York0Nor
th Carolina0North Dakota0Ohio0Oklahoma0Oregon0Pennsylvania0Rhode Island0South Carolina0South
 Dakota0Tennessee0Texas0Utah0Vermont0Virginia0Washington0West Virginia0Wisconsin0Wyoming'),s
tr_split(ALAKAZARCACOCTDEFLGAHIIDILINIAKSKYLAMEMDMAMIMNMSMOMTNENVNHNJNMNYNCNDOHOKORPARISCSDT
NTXUTVTVAWAWVWIWY,2),file(b)[0])).'{fill:blue}.b',implode('',file(a)));

Here is the ungolfed version:

<?php
$stateNames = 'Alabama0Alaska0Arizona0Arkansas0California0Colorado0Connecticut0Delaware0Florida0Georgia0Hawaii0Idaho0Illinois0Indiana0Iowa0Kansas0Kentucky0Louisiana0Maine0Maryland0Massachusetts0Michigan0Minnesota0Mississippi0Missouri0Montana0Nebraska0Nevada0New Hampshire0New Jersey0New Mexico0New York0North Carolina0North Dakota0Ohio0Oklahoma0Oregon0Pennsylvania0Rhode Island0South Carolina0South Dakota0Tennessee0Texas0Utah0Vermont0Virginia0Washington0West Virginia0Wisconsin0Wyoming';
$statesAbbreviations = 'ALAKAZARCACOCTDEFLGAHIIDILINIAKSKYLAMEMDMAMIMNMSMOMTNENVNHNJNMNYNCNDOHOKORPARISCSDTNTXUTVTVAWAWVWIWY';

$blankSVG = implode('', file('a'));

$inputWithStateNames = file('b')[0];
$inputWithStateAbbreviations = str_replace(
    explode('0', $stateNames),
    str_split($statesAbbreviations, 2),
    $inputWithStateNames
);

echo str_replace(
    '.border',
    '#'. str_replace(
        [
            '0',
            '1'
        ],
        [
            ',#',
            '{fill:red}#'
        ],
        $inputWithStateAbbreviations
    ) .'{fill:blue}.border',
    $blankSVG
);

The principle is simple: in the blank SVG, each path has an ID corresponding to the abbreviation of the state that it represents (for instance, <path d="…" id="HI" /> for Hawaii).

All we have to do is to add some CSS to colour this path in the appropriate shade. But there is already some CSS in the blank file (in particular the <style type="text/css">…</style> tag already exists), so it's really easy and short to do it. We can notice than the string .b is only found in the CSS for .border. Good news! We'll just replace .b with OUR_WONDERFUL_CSS.b.

Creating "our wonderful CSS" is not really more difficult:

  1. Read the input from the file:
    Ohio0Indiana0Illinois1New York0New Jersey0Florida.
  2. Replace the names of the states with their abbreviations:
    OH0IN0IL1NY0NJ0FL.
  3. Replace the 0 characters with ,#:
    OH,#IN,#IL1NY,#NJ,#FL.
  4. Replace the 1 character with {fill:red}#:
    OH,#IN,#IL{fill:red}#NY,#NJ,#FL.
  5. Add # at the beginning and {fill:blue} at the end:
    #OH,#IN,#IL{fill:red}#NY,#NJ,#FL{fill:blue}.