23
\$\begingroup\$

Given a string as input, output the US state whose capital it is if it is a state capital, the capital of the state if it is a state, or Arstotzka if it is neither.

Examples:

Austin -> Texas
Alaska -> Juneau
The Nineteenth Byte -> Arstotzka

All the capitals and their respective states:

Baton Rouge, Louisiana
Indianapolis, Indiana
Columbus, Ohio
Montgomery, Alabama
Helena, Montana
Denver, Colorado
Boise, Idaho
Austin, Texas
Boston, Massachusetts
Albany, New York
Tallahassee, Florida
Santa Fe, New Mexico
Nashville, Tennessee
Trenton, New Jersey
Jefferson, Missouri
Richmond, Virginia
Pierre, South Dakota
Harrisburg, Pennsylvania
Augusta, Maine
Providence, Rhode Island
Dover, Delaware
Concord, New Hampshire
Montpelier, Vermont
Hartford, Connecticut
Topeka, Kansas
Saint Paul, Minnesota
Juneau, Alaska
Lincoln, Nebraska
Raleigh, North Carolina
Madison, Wisconsin
Olympia, Washington
Phoenix, Arizona
Lansing, Michigan
Honolulu, Hawaii
Jackson, Mississippi
Springfield, Illinois
Columbia, South Carolina
Annapolis, Maryland
Cheyenne, Wyoming
Salt Lake City, Utah
Atlanta, Georgia
Bismarck, North Dakota
Frankfort, Kentucky
Salem, Oregon
Little Rock, Arkansas
Des Moines, Iowa
Sacramento, California
Oklahoma City, Oklahoma
Charleston, West Virginia
Carson City, Nevada

Rules

  • No built-ins or libraries/modules that provide a mapping from states to capitals or capitals to states, or lists of capitals and/or states (for example, Mathematica's CityData built-in)
  • Standard loopholes apply
  • This is , so shortest answer in bytes wins!

Leaderboard

The Stack Snippet at the bottom of this post generates the leaderboard from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

<style>body { text-align: left !important} #answer-list { padding: 10px; width: 290px; float: left; } #language-list { padding: 10px; width: 290px; float: left; } table thead { font-weight: bold; } table td { padding: 5px; }</style><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="language-list"> <h2>Shortest Solution by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr> </thead> <tbody id="languages"> </tbody> </table> </div> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr> </thead> <tbody id="answers"> </tbody> </table> </div> <table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table><script>var QUESTION_ID = 64254; var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe"; var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk"; var OVERRIDE_USER = 45941; var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page; function answersUrl(index) { return "https://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER; } function commentUrl(index, answers) { return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER; } function getAnswers() { jQuery.ajax({ url: answersUrl(answer_page++), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { answers.push.apply(answers, data.items); answers_hash = []; answer_ids = []; data.items.forEach(function(a) { a.comments = []; var id = +a.share_link.match(/\d+/); answer_ids.push(id); answers_hash[id] = a; }); if (!data.has_more) more_answers = false; comment_page = 1; getComments(); } }); } function getComments() { jQuery.ajax({ url: commentUrl(comment_page++, answer_ids), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { data.items.forEach(function(c) { if (c.owner.user_id === OVERRIDE_USER) answers_hash[c.post_id].comments.push(c); }); if (data.has_more) getComments(); else if (more_answers) getAnswers(); else process(); } }); } getAnswers(); var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/; var OVERRIDE_REG = /^Override\s*header:\s*/i; function getAuthorName(a) { return a.owner.display_name; } function process() { var valid = []; answers.forEach(function(a) { var body = a.body; a.comments.forEach(function(c) { if(OVERRIDE_REG.test(c.body)) body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>'; }); var match = body.match(SCORE_REG); if (match) valid.push({ user: getAuthorName(a), size: +match[2], language: match[1], link: a.share_link, }); else console.log(body); }); valid.sort(function (a, b) { var aB = a.size, bB = b.size; return aB - bB }); var languages = {}; var place = 1; var lastSize = null; var lastPlace = 1; valid.forEach(function (a) { if (a.size != lastSize) lastPlace = place; lastSize = a.size; ++place; var answer = jQuery("#answer-template").html(); answer = answer.replace("{{PLACE}}", lastPlace + ".") .replace("{{NAME}}", a.user) .replace("{{LANGUAGE}}", a.language) .replace("{{SIZE}}", a.size) .replace("{{LINK}}", a.link); answer = jQuery(answer); jQuery("#answers").append(answer); var lang = a.language; lang = jQuery('<a>'+lang+'</a>').text(); languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang.toLowerCase(), user: a.user, size: a.size, link: a.link}; }); var langs = []; for (var lang in languages) if (languages.hasOwnProperty(lang)) langs.push(languages[lang]); langs.sort(function (a, b) { if (a.lang_raw > b.lang_raw) return 1; if (a.lang_raw < b.lang_raw) return -1; return 0; }); for (var i = 0; i < langs.length; ++i) { var language = jQuery("#language-template").html(); var lang = langs[i]; language = language.replace("{{LANGUAGE}}", lang.lang) .replace("{{NAME}}", lang.user) .replace("{{SIZE}}", lang.size) .replace("{{LINK}}", lang.link); language = jQuery(language); jQuery("#languages").append(language); } }</script>

\$\endgroup\$
  • 4
    \$\begingroup\$ Related \$\endgroup\$ – Sp3000 Nov 19 '15 at 0:01
  • 4
    \$\begingroup\$ Please provide a full list of states and their capitals in their proper spelling in plain text. \$\endgroup\$ – orlp Nov 19 '15 at 0:05
  • 2
    \$\begingroup\$ @sysreq The spelling and case must exactly match the list provided. As "United States" is neither a US state nor a US state capital, it should return "Arstotzka". \$\endgroup\$ – Mego Nov 19 '15 at 0:12
  • 6
    \$\begingroup\$ @sysreq That doesn't sound like a very good idea to me. \$\endgroup\$ – SuperJedi224 Nov 19 '15 at 1:32
  • 4
    \$\begingroup\$ @FarazMasroor Builtins are banned \$\endgroup\$ – a spaghetto Nov 19 '15 at 2:51

23 Answers 23

10
\$\begingroup\$

Pyth, 597 596 590

00000000: 40 4a 63 72 58 73 40 4c 2b 62 47 6a 43 22 05 77 c6  @JcrXs@L+bGjC".w.
00000011: fa 94 29 be 77 9b 88 35 9b e0 86 05 86 8a d3 cd 53  ..).w..5........S
00000022: e6 97 c1 20 f4 bd b5 76 f3 f7 14 ab 4b fa 8e 4d 2c  ... ...v....K..M,
00000033: be 71 4d 13 ca 9c 67 3e 35 21 76 6b 74 f8 88 8b c1  .qM...g>5!vkt....
00000044: bc 2a ab da 6b ba 1f 2b a3 2c 12 bc d1 b8 c3 b6 7c  .*..k..+.,......|
00000055: b0 4d 0f 11 a2 dc 15 a2 94 45 39 1c 20 12 08 03 a3  .M.......E9. ....
00000066: 5c 71 d9 11 1c 56 f5 c2 2d cb 63 64 b3 c0 d8 e7 e3  \q...V..-.cd.....
00000077: 9b 82 28 85 57 2b e4 28 ea 8f 17 02 1d a8 8f f3 0e  ..(.W+.(.........
00000088: 31 5f 8e c4 2b b3 8f 7e b2 64 fc d2 99 dc 9d 98 e5  1_..+..~.d.......
00000099: 3a b3 8b 59 52 5f 63 5a ae c9 3d 8a 7a b6 a2 0a 8a  :..YR_cZ..=.z....
000000aa: 2f 4c 43 78 fa 56 9b 07 ce 21 3a 2f 50 5e d3 f3 a9  /LCx.V...!:/P^...
000000bb: 90 ad 21 85 75 cd 9e 07 29 a3 3d b7 c2 cd f4 b8 5f  ..!.u...).=....._
000000cc: 60 b4 72 cd 47 93 17 14 28 3e da b1 65 90 ec 5f 35  `.r.G...(>..e.._5
000000dd: 4d c6 0e b1 66 40 af 8a 67 95 47 f8 8f fd 38 66 c8  M...f@..g.G...8f.
000000ee: df fd 51 6c 5f 06 06 35 a1 77 ae 93 84 73 33 63 be  ..Ql_..5.w...s3c.
000000ff: 98 3e 1c f3 43 de ca 0a 13 6d e9 64 52 96 b8 41 e8  .>..C....m.dR..A.
00000110: db 23 a6 01 c5 60 38 db 63 9c 2a d9 5d 03 fc 38 c8  .#...`8.c.*.]..8.
00000121: 94 1e 2f 70 8e 4d 0f 93 44 4a 6c 57 e8 af cc e7 e2  ../p.M..DJlW.....
00000132: 70 82 84 a3 06 d3 2a 4f cd 8b b6 68 a5 80 98 9e 49  p.....*O...h....I
00000143: 4f bc fd 91 20 55 a6 66 12 1d c0 49 f2 3e 9e ef 5d  O... U.f...I.>..]
00000154: 89 bc b3 30 ef dd cc ca 93 70 27 2c 14 03 6a 53 32  ...0.....p',..jS2
00000165: ef af e1 b5 d3 5a cc 3e fd 1d 78 a5 b3 06 ae 67 58  .....Z.>..x....gX
00000176: ec e4 7a 99 16 f5 da a7 1e 51 0a 94 fc cd e9 e8 fe  ..z......Q.......
00000187: 5a d1 bc 0f bb 24 9d 45 9a ac 97 58 a7 9b ea f0 77  Z....$.E...X....w
00000198: 89 e8 a1 fa e7 83 2c b9 ea 5c a7 f1 b3 76 ba 97 fb  ......,..\...v...
000001a9: 41 b7 74 9a 09 2d 96 97 73 80 13 17 7a fe 15 5f c6  A.t..-..s...z.._.
000001ba: cf 89 3d 7f df 54 cd 16 fe 8a ea d7 0a 91 84 54 61  ..=..T.........Ta
000001cb: 4f 03 87 a6 d2 1c c8 a9 b8 0f 52 2e 39 13 2e 48 8b  O.........R.9..H.
000001dc: 04 ce 2d 35 5e 02 96 63 11 a7 f8 75 6d 8a e2 13 e2  ..-5^..c...um....
000001ed: d6 1b f9 43 07 3e 99 29 a9 69 7f f5 6a 72 15 7b 92  ...C.>.).i..jr.{.
000001fe: 5b 26 cb 74 48 1b c0 fc fb 64 45 05 2d 37 d6 21 6a  [&.tH....dE.-7.!j
0000020f: d9 83 88 13 fb e7 e8 f2 17 c7 54 de 48 f6 23 1e bf  ..........T.H.#..
00000220: b0 34 f5 4f 63 ce 46 40 a9 16 e7 4a 60 71 ce a3 d3  .4.Oc.F@...J`q...
00000231: 6a 15 4c 8b 52 d6 3f cb 53 dd 96 97 10 f6 e5 22 32  j.L.R.?.S......"2
00000242: 37 5c 71 64 33 62 5f 68 68 78 4a 7a                 7\qd3b_hhxJz

The above hexdump can be reversed with xxd -r -c 17. Try it online in the Pyth Compiler.

Idea

We encode the required information as follows:

columbia
columbus
frankfort
⋮
kentucky
ohio
southqcarolina
arstotzka

All the names have been cast to lowercase, spaces have been replaced with q's, and names are separated by linefeeds.

All capitals are listed first (in an order that managed to minimize the byte count). Then the corresponding states are listed in reverse order. Finally, Arstotzka is appended.

This way, the capital/state at index i corresponds to the state/capital at index -(x+2). Also, an invalid input will either have index 100 (Arstotzka) or -1 (not found), and -(100+2) ≡ -1 ≡ -(-1+2) mod 101. This layout prevents Arstotzka from being a special case, without prepending or appending anything to the list.

To compress the information, we find each character's index in "\nabc...xyz" and convert the result from base 27 to base 256.

Decoding is straightforward: we go back from base 256 to base 27, retrieve the corresponding elements from "\nabc…xyz", replace q's with spaces, and capitalize the first letter of each resulting word.

Code

@JcrXs@L+bGjC"…"27\qd3b_hhxJz

            C"…"               Convert the 562-character from base 256 to integer.
           j    27             Convert the result from integer to base 27.
        +bG                    Compute "\n" + "abc…xyz".
      @L                       Retrieve the corresponding characters.
     s                         Flatten the resulting array of strings.
    X             \qd          Replace each q with a space.
   r                 3         Apply title casing.
 Jc                   b        Split at linefeeds and save in J.
                          xJz  Compute the index of the input (z) in J.
                       _hh     Increment twice and negate.
@                              Retrieve the element at that index from J.
\$\endgroup\$
24
\$\begingroup\$

Python3, 997 bytes

i=input()
s="Baton Rouge,Louisiana,Indianapolis,Indiana,Columbus,Ohio,Montgomery,Alabama,Helena,Montana,Denver,Colorado,Boise,Idaho,Austin,Texas,Boston,Massachusetts,Albany,New York,Tallahassee,Florida,Santa Fe,New Mexico,Nashville,Tennessee,Trenton,New Jersey,Jefferson,Missouri,Richmond,Virginia,Pierre,South Dakota,Harrisburg,Pennsylvania,Augusta,Maine,Providence,Rhode Island,Dover,Delaware,Concord,New Hampshire,Montpelier,Vermont,Hartford,Connecticut,Topeka,Kansas,Saint Paul,Minnesota,Juneau,Alaska,Lincoln,Nebraska,Raleigh,North Carolina,Madison,Wisconsin,Olympia,Washington,Phoenix,Arizona,Lansing,Michigan,Honolulu,Hawaii,Jackson,Mississippi,Springfield,Illinois,Columbia,South Carolina,Annapolis,Maryland,Cheyenne,Wyoming,Salt Lake City,Utah,Atlanta,Georgia,Bismarck,North Dakota,Frankfort,Kentucky,Salem,Oregon,Little Rock,Arkansas,Des Moines,Iowa,Sacramento,California,Oklahoma City,Oklahoma,Charleston,West Virginia,Carson City,Nevada,Arstotzka".split(",")+[i]
print(s[1^s.index(i)])

I can not be bothered to do the base-conversion or binary packing that the winning version will undoubtly do, but I do want to post this to show a really cool trick.

XOR'ing a number by 1 is equivalent to adding 1 if the number is even, or subtracting 1 if the number is odd. You can make really easy and golfy bi-directional maps using this trick.

\$\endgroup\$
  • 2
    \$\begingroup\$ Same trick as mine, different language. You beat me by 1 minute! \$\endgroup\$ – Level River St Nov 19 '15 at 1:40
  • \$\begingroup\$ aah, this is what I wanted to do with mine but I had no idea how. nice golfing, though! \$\endgroup\$ – cat Nov 19 '15 at 2:17
  • \$\begingroup\$ If you switch to 2.7 you can eliminate parens in print() and everything else should still work \$\endgroup\$ – cat Nov 19 '15 at 2:19
  • 1
    \$\begingroup\$ You could just require the input have quotations (when using raw_input). @orlp \$\endgroup\$ – Zach Gates Nov 19 '15 at 2:36
  • 1
    \$\begingroup\$ @ΚριτικσιΛίθος That is a TypeError. \$\endgroup\$ – orlp Nov 19 '15 at 10:47
13
\$\begingroup\$

CJam, 625 609 603 599 bytes

00000000: 22 05 77 c6 fa 94 29 be 77 9b 88 35 9b e0 86 05 86  ".w...).w..5.....
00000011: 8a d3 cd 53 e6 97 c1 20 f4 bd b5 76 f3 f7 14 ab 4b  ...S... ...v....K
00000022: fa 8e 4d 2c be 71 4d 13 ca 9c 67 3e 35 21 76 6b 74  ..M,.qM...g>5!vkt
00000033: f8 88 8b c1 bc 2a ab da 6b ba 1f 2b a3 2c 12 bc d1  .....*..k..+.,...
00000044: b8 c3 b6 7c b0 4d 0f 11 a2 dc 15 a2 94 45 39 1c 20  ...|.M.......E9. 
00000055: 12 08 03 a3 5c 71 d9 11 1c 56 f5 c2 2d cb 63 64 b3  ....\q...V..-.cd.
00000066: c0 d8 e7 e3 9b 82 28 85 57 2b e4 28 ea 8f 17 02 1d  ......(.W+.(.....
00000077: a8 8f f3 0e 31 5f 8e c4 2b b3 8f 7e b2 64 fc d2 99  ....1_..+..~.d...
00000088: dc 9d 98 e5 3a b3 8b 59 52 5f 63 5a ae c9 3d 8a 7a  ....:..YR_cZ..=.z
00000099: b6 a2 0a 8a 2f 4c 43 78 fa 56 9b 07 ce 21 3a 2f 50  ..../LCx.V...!:/P
000000aa: 5e d3 f3 a9 90 ad 21 85 75 cd 9e 07 29 a3 3d b7 c2  ^.....!.u...).=..
000000bb: cd f4 b8 5f 60 b4 72 cd 47 93 17 14 28 3e da b1 65  ..._`.r.G...(>..e
000000cc: 90 ec 5f 35 4d c6 0e b1 66 40 af 8a 67 95 47 f8 8f  .._5M...f@..g.G..
000000dd: fd 38 66 c8 df fd 51 6c 5f 06 06 35 a1 77 ae 93 84  .8f...Ql_..5.w...
000000ee: 73 33 63 be 98 3e 1c f3 43 de ca 0a 13 6d e9 64 52  s3c..>..C....m.dR
000000ff: 96 b8 41 e8 db 23 a6 01 c5 60 38 db 63 9c 2a d9 5d  ..A..#...`8.c.*.]
00000110: 03 fc 38 c8 94 1e 2f 70 8e 4d 0f 93 44 4a 6c 57 e8  ..8.../p.M..DJlW.
00000121: af cc e7 e2 70 82 84 a3 06 d3 2a 4f cd 8b b6 68 a5  ....p.....*O...h.
00000132: 80 98 9e 49 4f bc fd 91 20 55 a6 66 12 1d c0 49 f2  ...IO... U.f...I.
00000143: 3e 9e ef 5d 89 bc b3 30 ef dd cc ca 93 70 27 2c 14  >..]...0.....p',.
00000154: 03 6a 53 32 ef af e1 b5 d3 5a cc 3e fd 1d 78 a5 b3  .jS2.....Z.>..x..
00000165: 06 ae 67 58 ec e4 7a 99 16 f5 da a7 1e 51 0a 94 fc  ..gX..z......Q...
00000176: cd e9 e8 fe 5a d1 bc 0f bb 24 9d 45 9a ac 97 58 a7  ....Z....$.E...X.
00000187: 9b ea f0 77 89 e8 a1 fa e7 83 2c b9 ea 5c a7 f1 b3  ...w......,..\...
00000198: 76 ba 97 fb 41 b7 74 9a 09 2d 96 97 73 80 13 17 7a  v...A.t..-..s...z
000001a9: fe 15 5f c6 cf 89 3d 7f df 54 cd 16 fe 8a ea d7 0a  .._...=..T.......
000001ba: 91 84 54 61 4f 03 87 a6 d2 1c c8 a9 b8 0f 52 2e 39  ..TaO.........R.9
000001cb: 13 2e 48 8b 04 ce 2d 35 5e 02 96 63 11 a7 f8 75 6d  ..H...-5^..c...um
000001dc: 8a e2 13 e2 d6 1b f9 43 07 3e 99 29 a9 69 7f f5 6a  .......C.>.).i..j
000001ed: 72 15 7b 92 5b 26 cb 74 48 1b c0 fc fb 64 45 05 2d  r.{.[&.tH....dE.-
000001fe: 37 d6 21 6a d9 83 88 13 fb e7 e8 f2 17 c7 54 de 48  7.!j..........T.H
0000020f: f6 23 1e bf b0 34 f5 4f 63 ce 46 40 a9 16 e7 4a 60  .#...4.Oc.F@...J`
00000220: 71 ce a3 d3 6a 15 4c 8b 52 d6 3f cb 53 dd 96 97 10  q...j.L.R.?.S....
00000231: f6 e5 22 32 35 36 62 32 37 62 27 60 66 2b 27 60 2f  .."256b27b'`f+'`/
00000242: 7b 27 71 2f 33 32 61 66 2e 5e 53 2a 7d 25 5f 71 61  {'q/32af.^S*}%_qa
00000253: 23 7e 28 3d                                         #~(=

The above hexdump can be reversed with xxd -r -c 17. Try it online in the CJam interpreter.

Idea

We encode the required information as follows:

columbia`columbus`frankfort`olympia`desqmoines`jefferson`springfield`carsonqcity`topeka`sacramento`littleqrock`saltqlakeqcity`boston`trenton`batonqrouge`indianapolis`madison`charleston`austin`denver`hartford`santaqfe`atlanta`augusta`boise`oklahomaqcity`dover`helena`tallahassee`richmond`lansing`honolulu`saintqpaul`salem`providence`raleigh`cheyenne`nashville`concord`albany`bismarck`phoenix`jackson`montpelier`montgomery`harrisburg`juneau`pierre`annapolis`lincoln`nebraska`maryland`southqdakota`alaska`pennsylvania`alabama`vermont`mississippi`arizona`northqdakota`newqyork`newqhampshire`tennessee`wyoming`northqcarolina`rhodeqisland`oregon`minnesota`hawaii`michigan`virginia`florida`montana`delaware`oklahoma`idaho`maine`georgia`newqmexico`connecticut`colorado`texas`westqvirginia`wisconsin`indiana`louisiana`newqjersey`massachusetts`utah`arkansas`california`kansas`nevada`illinois`missouri`iowa`washington`kentucky`ohio`southqcarolina`arstotzka

All the names have been cast to lowercase, spaces have been replaced with q's, and names are separated by backticks.

All capitals are listed first (in an order that managed to minimize the byte count). Then the corresponding states are listed in reverse order. Finally, Arstotzka is appended.

This way, the capital/state at index i corresponds to the state/capital at index -(x+2). Also, an invalid input will either have index 100 (Arstotzka) or -1 (not found), and -(100+2) ≡ -1 ≡ -(-1+2) mod 101. This layout prevents Arstotzka from being a special case, without prepending or appending anything to the list.

To compress the information, we subtract 97 (code point of `) from each character in the string and convert the result from base 27 to base 256.

Decoding is straightforward: we go back from base 256 to base 27, add the resulting base-27 digits to the backtick character, replace q's with spaces, and capitalize the first letter of each resulting word.

Code

"…"          e# Push a string of 562 characters.
256b27b      e# Convert it from base 256 to base 27.
'`f+         e# Add each base-27 digit to the character '`'.
'`/          e# Split at occurrences of '`'.
{            e#   For each resulting chunk:
  'q/        e#   Split at occurrences of 'q'.
  32a        e#   Push [32].
  f.^        e#   For each chunk, perform vectorized XOR with 32.
             e#   This capitalizes the first character of the chunk.
  S*         e#   Join the chunks, separating by spaces.
}%           e#
_            e# Push a copy of the resulting array.
qa           e# Read all user input and wrap it in an array.
#            e# Find its index (i) in the copy.
~(           e# Apply bitwise NOT and decrements to calculate
             e# ~i - 1 = -(i + 1) - 1 = -(i + 2).
=            e# Retrieve element at the corresponding index.
\$\endgroup\$
  • \$\begingroup\$ Which encoding is this? \$\endgroup\$ – lirtosiast Nov 19 '15 at 2:16
  • \$\begingroup\$ ISO 8559-1, so each char is one byte. \$\endgroup\$ – Dennis Nov 19 '15 at 2:17
  • \$\begingroup\$ It is amazing what you can do with CJam... \$\endgroup\$ – GamrCorps Nov 19 '15 at 2:23
  • \$\begingroup\$ I think this is the largest CJam program I've seen. \$\endgroup\$ – Mego Nov 19 '15 at 10:11
9
\$\begingroup\$

JavaScript (ES6), 821 806 bytes

Uses Base64 decode to compress strings. Live demo

s=>(S=(r=x=>btoa(x).replace(/\+/g," ").split`/`)`.¢²&§kò'v&§kó¡À¦Ú¯Ì¢{Z¯Â¢Z+iÚ?!Ö¡£ôÞÅ«?1«,iÈn±ëm³ó^Ãæ(®OÅâu¯Í{{£ôÞw¬±ç¿5ì>%êì{/ÌË(º¸¿V*àxý*.¶jJ-kóÞ{2ö§¯Ìj)Þýhuï²V§wðÞ¬­ïÍ{jjl*ÞýW«íü*'ç-Ë­ü¦§±«?2)çzÊ-kð%jÉü×­«$kóh®Ø~    ªè)Úýh¬rìÖjÈbhð+:'kó"r jÇk¢ó"²È¬²*iò%)èÏÒ¢ëaø&«¢X§kó¯)ZßÖÊ¢Ôµ¨ê+&¿6íàÚZü§§¶ç$Ëó«z
'ü
äj{³ò(Á¯ÂjX¢¹âkó¤¨h¯ÖzË~V*àxüׯiÖ¿`,C=r`«hähº¿"wbjv©¢X¬ü*%ºfî³ó(Ø(êòüw¥zv¿
éïz¿Á¢+ü¬¶)ÿ-¢À¶§ËôÚV¡jË{ôÖ¾ïÍjÈoY^ý:ÞÚ'ü}êì¢ÑÈf¢w>'«­ïÇjºâ±»«ð.ë-kóë¢øzwü:/z¿Â¢w(­ßÌ¢{izX¯ñÚ®×è­ßÓ¢¤kô{~=«¥ü§y«¿.)Ü¢YÿE©^1§b²ÿ:\\¦¦&¿>,-©ìx?èénüÊ'ý*kxé]ü*%ºfâkð'ªh+?
²zyÞý&¥·âÚïÜ¿ÙZÖ¿+&j·$üZÚGè®ßÒjW¦ü¸­¶W¾F$ü7¬øÊ"ë?I§+jg§¶ÎV¡¢f¾
+rü(Z®W¬¶ÿ  ªì¢Ü¿`,S[C.indexOf(s)]||C[S.indexOf(s)]||"Arstotzka")

Here is a hexdump; use xxd -r filename to reverse.

00000000: 733d 3e28 533d 2872 3d78 3d3e 6274 6f61  s=>(S=(r=x=>btoa
00000010: 2878 292e 7265 706c 6163 6528 2f5c 2b2f  (x).replace(/\+/
00000020: 672c 2220 2229 2e73 706c 6974 602f 6029  g," ").split`/`)
00000030: 602e 3fa2 b226 a76b f227 7626 a76b f3a1  `.?..&.k.'v&.k..
00000040: 3f8f c03f a6da 3faf cca2 7b5a 9daf c2a2  ?..?..?...{Z....
00000050: 5a2b 69da 3f21 d6a1 a3f4 dec5 ab3f 31ab  Z+i.?!.......?1.
00000060: 2c69 c86e b1eb 6db3 f35e c3e6 28ae 4fc5  ,i.n..m..^..(.O.
00000070: 3f3f e275 afcd 7b0f 3f7b 183f a3f4 de3f  ??.u..{.?{.?...?
00000080: 77ac b1e7 bf35 ec3e 25ea ec7b 2fcc 3fcb  w....5.>%..{/.?.
00000090: 28ba b8bf 562a e03f 783f fd2a 2eb6 1f3f  (...V*.?x?.*...?
000000a0: 6a4a 2d6b f3de 3f7b 323f f6a7 3faf cc6a  jJ-k..?{2?..?..j
000000b0: 29de fd18 6875 ef3f b256 a777 f0de 3fac  )...hu.?.V.w..?.
000000c0: 1aad efcd 7b0f 3f6a 6a6c 3f2a defd 57ab  ....{.?jjl?*..W.
000000d0: 3f3f edfc 2a27 9de7 2d3f cbad fca6 a7b1  ??..*'..-?......
000000e0: ab3f 3229 e77a ca2d 6bf0 256a c91a fcd7  .?2).z.-k.%j....
000000f0: 3fad ab24 6bf3 68ae d87e 09aa e83f 29da  ?..$k.h..~...?).
00000100: fd68 ac72 3fec 3f7f d66a c862 3f0b 683f  .h.r?.?..j.b?.h?
00000110: f02b 3f3a 276b f322 7218 206a 7fc7 6b06  .+?:'k."r. j..k.
00000120: a23f f322 b2c8 acb2 2a69 3ff2 253f 29e8  .?."....*i?.%?).
00000130: 3fcf d2a2 eb61 f826 aba2 58a7 6bf3 1aaf  ?....a.&..X.k...
00000140: 295a 9ddf d6ca 3fa2 3f0f d4b5 a87f 19ea  )Z....?.?.......
00000150: 2b3f 26bf 363f ed3f e0da 3f3f 5afc a7a7  +?&.6?.?..??Z...
00000160: b6e7 24cb f3ab 7a0a 27fc 0ae4 6a7b 1ab3  ..$...z.'...j{..
00000170: f228 c1af c26a 583f a2b9 e26b f3a4 3fa8  .(...jX?...k..?.
00000180: 683f afd6 7acb 7e56 2ae0 3f78 3ffc d7af  h?..z.~V*.?x?...
00000190: 69d6 bf60 2c43 3d72 6005 ab68 3fe4 68ba  i..`,C=r`..h?.h.
000001a0: 07bf 2277 626a 76a9 a258 acfc 2a25 ba66  .."wbjv..X..*%.f
000001b0: eeb3 f328 3fd8 283f eaf2 fc77 a57a 76bf  ...(?.(?...w.zv.
000001c0: 0ae9 ef7a bfc1 a22b 1efc 0bac b629 ff06  ...z...+.....)..
000001d0: 3f2d a27f c03f b6a7 cbf4 da3f 56a1 6acb  ?-...?.....?V.j.
000001e0: 1e7b f43f 3fd6 be15 efcd 6ac8 6f3f 595e  .{.??.....j.o?Y^
000001f0: fd3a de3f da27 fc3f 3f7d eaec a27f d13f  .:.?.'.??}.....?
00000200: c866 a277 7f3e 27ab adef c76a bae2 b1bb  .f.w.>'....j....
00000210: ab3f f02e 3feb 2d6b f3eb a2f8 9d7a 771e  .?..?.-k.....zw.
00000220: fc3a 2f7a bfc2 a277 28ad dfcc a27b 697a  .:/z...w(....{iz
00000230: 583f aff1 daae d7e8 addf d3a2 3fa4 6bf4  X?..........?.k.
00000240: 3f3f 7b7e 3dab a5fc 3fa7 79ab bf2e 29dc  ??{~=...?.y...).
00000250: a259 ff45 a95e 3f08 7f31 a762 b23f ff3a  .Y.E.^?..1.b.?.:
00000260: 5c5c a6a6 26bf 3e1a 1e3f 2c7f 2da9 ec3f  \\..&.>..?,.-..?
00000270: 783f 1e3f e83f e96e fc3f 3f3f ca27 fd2a  x?.?.?.n.???.'.*
00000280: 6b3f 781f 3fe9 5dfc 2a25 ba66 e26b f027  k?x.?.].*%.f.k.'
00000290: 9daa 683f 2b3f 0a17 b27a 79de fd26 a5b7  ..h?+?...zy..&..
000002a0: e2da 3fef 3f3f dcbf 02d9 5a3f d6bf 062b  ..?.??....Z?...+
000002b0: 266a b724 fc5a da3f 47e8 aedf d26a 57a6  &j.$.Z.?G....jW.
000002c0: fcb8 adb6 57be 463f 24fc 37ac f8ca 229d  ....W.F?$.7...".
000002d0: eb3f 49a7 2b6a 67a7 b68f ce3f 56a1 a266  .?I.+jg....?V..f
000002e0: be0a 2b72 fc28 5aae 57ac b63f ff09 aaec  ..+r.(Z.W..?....
000002f0: a27f 3f3f dcbf 602c 535b 432e 696e 6465  ..??..`,S[C.inde
00000300: 784f 6628 7329 5d7c 7c43 5b53 2e69 6e64  xOf(s)]||C[S.ind
00000310: 6578 4f66 2873 295d 7c7c 2241 7273 746f  exOf(s)]||"Arsto
00000320: 747a 6b61 2229                           tzka")
\$\endgroup\$
  • \$\begingroup\$ Is the r= necessary? \$\endgroup\$ – Conor O'Brien Nov 20 '15 at 2:14
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ Yep, it's reused to decode both the state and capital strings. \$\endgroup\$ – intrepidcoder Nov 20 '15 at 3:41
7
\$\begingroup\$

PHP, 1272 1239 bytes

Make sure you have enabled short_open_tag in php.ini:

This code uses argv[1] as the input.

<?$c=json_decode('{"Baton Rouge":"Louisiana","Indianapolis":"Indiana","Columbus":"Ohio","Montgomery":"Alabama","Helena":"Montana","Denver":"Colorado","Boise":"Idaho","Austin":"Texas","Boston":"Massachusetts","Albany":"New York","Tallahassee":"Florida","Santa Fe":"New Mexico","Nashville":"Tennessee","Trenton":"New Jersey","Jefferson":"Missouri","Richmond":"Virginia","Pierre":"South Dakota","Harrisburg":"Pennsylvania","Augusta":"Maine","Providence":"Rhode Island","Dover":"Delaware","Concord":"New Hampshire","Montpelier":"Vermont","Hartford":"Connecticut","Topeka":"Kansas","Saint Paul":"Minnesota","Juneau":"Alaska","Lincoln":"Nebraska","Raleigh":"North Carolina","Madison":"Wisconsin","Olympia":"Washington","Phoenix":"Arizona","Lansing":"Michigan","Honolulu":"Hawaii","Jackson":"Mississippi","Springfield":"Illinois","Columbia":"South Carolina","Annapolis":"Maryland","Cheyenne":"Wyoming","Salt Lake City":"Utah","Atlanta":"Georgia","Bismarck":"North Dakota","Frankfort":"Kentucky","Salem":"Oregon","Little Rock":"Arkansas","Des Moines":"Iowa","Sacramento":"California","Oklahoma City":"Oklahoma","Charleston":"West Virginia","Carson City":"Nevada"}',1);$s=array_flip($c);$z=$argv[1];echo @$c[$z]?$c[$z]:(@$s[$z]?$s[$z]:'Arstotzka');
\$\endgroup\$
  • 1
    \$\begingroup\$ Your entire code will be WAY shorter if you do like this: <?$c=['Baton Rouge'=>Louisiana,$I=Indiana=>$I.polis, ... ];echo$c[$z=$argv[1]]?:(array_flip($c)[$z]?:Arstotzka);. You just need to fill the array. Ignore all warnings \$\endgroup\$ – Ismael Miguel Nov 20 '15 at 10:25
6
\$\begingroup\$

Java, 1062 964 bytes

s->{String t="Arstotzka",a[]="AlabamaMontgomeryAlaskaJuneauArizonaPhoenixArkansasLittle RockCaliforniaSacrementoColoradoDenverConecticutHartfordDelawareDoverFloridaTallahasseeGeorgiaAtlantaHawaiiHonoluluIdahoBoiseIllinoisSpringfieldIndianaIndianapolisIowaDes MoinesKansasTopekaKentuckyFrankfortLouisianaBaton RougeMaineAugustaMarylandAnnapolisMassachusettsBostonMichiganLansingMinnesotaSaint PaulMississippiJacksonMissouriJefferson CityMontanaHelenaNebraskaLincolnNevadaCarson CityNew HampshireConcordNew JerseyTrentonNew MexicoSanta FeNew YorkAlbanyNorth CarolinaRaleighNorth DakotaBismarckOhioColumbusOklahomaOklahoma CityOregonSalemPennsylvaniaHarrisburgRhode IslandProvidenceSouth CarolinaColumbiaSouth DakotaPierreTennesseeNashvilleTexasAustinUtahSalt Lake CityVermontMontpelierVirginiaRichmondWashingtonOlympiaWest VirginiaCharlestonWisconsinMadisonWyomingCheyenne".split("(?<=[a-z])(?=[A-Z])");for(int k=-1;k++<99;t=s.equals(a[k])?a[k%2*-2-~k]:t);return t;}

-17 bytes thanks to @KevinCruijssen.

Try it online!

Pretty straightforward; if a match is found, then add to or subtract from the current index of the array depending on whether it is even or odd. Otherwise print "Arstotzka".

The majority of bytes saved was due to removing the 99 :s and splitting the string based on the regex (?<=[a-z])(?=[A-Z]), which uses a positive lookbehind and lookahead to check for a difference in casing.

\$\endgroup\$
  • 1
    \$\begingroup\$ Thanks for the suggestion. Funny thing is that I was thinking about improvements I could make to this answer last night. I'll get around to implementing them soon. :) \$\endgroup\$ – TNT Nov 3 '17 at 17:24
5
\$\begingroup\$

R, 1023 985 bytes

function(s){g=strsplit
y="Bat9 Rouge,Indi2apol3,Columbus,M9tgomery,Hel6a,D6ver,Bo3e,Aust1,Bost9,Alb2y,Tallah5s4,S2ta Fe,N5hville,Tr6t9,Jeffers9,Richm9d,Pierre,H7r3burg,Augusta,Provid6ce,Dover,C9c0d,M9tpelier,H7tf0d,Topeka,Sa1t Paul,Juneau,L1coln,Raleigh,Mad39,Olympia,Pho6ix,L2s1g,H9olulu,Jacks9,Spr1gfield,Columbia,Annapol3,Chey6ne,Salt Lake8,Atl2ta,B3m7ck,Fr2kf0t,Salem,Little Rock,Des Mo1es,Sacram6to,Oklahoma8,Ch7lest9,C7s98,Nevada,West Virg1ia,Oklahoma,Calif0nia,Iowa,Ark2s5,Oreg9,K6tucky,N0th Dakota,Ge0gia,Utah,Wyom1g,M7yl2d,South C7ol1a,Ill1o3,M3s3sippi,Hawaii,Michig2,Ariz9a,W5h1gt9,W3c9s1,N0th C7ol1a,Nebr5ka,Al5ka,M1nesota,K2s5,C9necticut,Verm9t,New Hampshire,Delaw7e,Rhode Isl2d,Ma1e,P6nsylv2ia,South Dakota,Virg1ia,M3souri,New Jersey,T6ness4,New Mexico,Fl0ida,New Y0k,M5sachusetts,Tex5,Idaho,Col0ado,M9t2a,Alabama,Ohio,Indi2a,Lou3i2a"
for(i in 0:9)y=gsub(i,g("or,in,an,is,ee,as,en,ar, City,on",",")[[1]][i+1],y)
x=g(y,",")[[1]]
'if'({y=match(s,x,0)},x[101-y],'Arstotzka')}

Try it online!

Different from the other R submission.

The code puts the capitals and states in the same vector. The states are in reverse order so that, given the index of either the state or the capital, the matching entry's index is length(data)-index+1, where length(data)=100, 50 (states) + 50 (capitals)

Also, the input data is packed as a single string and then split. The string could be compressed to golf it further.

Edit: added some unoptimized substitution of pairs of characters.

\$\endgroup\$
4
\$\begingroup\$

Javascript, 1057 1042 bytes

Answer 1: 1042

(()=>{for(i=0,b=prompt(),c='Baton Rouge,Louisiana,Indianapolis,Indiana,Columbus,Ohio,Montgomery,Alabama,Helena,Montana,Denver,Colorado,Boise,Idaho,Austin,Texas,Boston,Massachusetts,Albany,New York,Tallahassee,Florida,Santa Fe,New Mexico,Nashville,Tennessee,Trenton,New Jersey,Jefferson,Missouri,Richmond,Virginia,Pierre,South Dakota,Harrisburg,Pennsylvania,Augusta,Maine,Providence,Rhode Island,Dover,Delaware,Concord,New Hampshire,Montpelier,Vermont,Hartford,Connecticut,Topeka,Kansas,Saint Paul,Minnesota,Juneau,Alaska,Lincoln,Nebraska,Raleigh,North Carolina,Madison,Wisconsin,Olympia,Washington,Phoenix,Arizona,Lansing,Michigan,Honolulu,Hawaii,Jackson,Mississippi,Springfield,Illinois,Columbia,South Carolina,Annapolis,Maryland,Cheyenne,Wyoming,Salt Lake City,Utah,Atlanta,Georgia,Bismarck,North Dakota,Frankfort,Kentucky,Salem,Oregon,Little Rock,Arkansas,Des Moines,Iowa,Sacramento,California,Oklahoma City,Oklahoma,Charleston,West Virginia,Carson City,Nevada'.split(","),q="Arstotzka";i<100;i++){if(c[i]==b)q=c[i%2?i-1:i+1];}return q})()

Updated Answer 1 missed a zero :S, Also fixed general incorrectness Updated Answer 1 + 2 re-arranged structure a bit.

Answer 1 is recommended to be run in a Javascript console (your browser's, for example) an unnamed function that returns to the console. You can also test it here.

Answer 2 1049

alert((()=>{for(i=0,b=prompt(),c='Baton Rouge,Louisiana,Indianapolis,Indiana,Columbus,Ohio,Montgomery,Alabama,Helena,Montana,Denver,Colorado,Boise,Idaho,Austin,Texas,Boston,Massachusetts,Albany,New York,Tallahassee,Florida,Santa Fe,New Mexico,Nashville,Tennessee,Trenton,New Jersey,Jefferson,Missouri,Richmond,Virginia,Pierre,South Dakota,Harrisburg,Pennsylvania,Augusta,Maine,Providence,Rhode Island,Dover,Delaware,Concord,New Hampshire,Montpelier,Vermont,Hartford,Connecticut,Topeka,Kansas,Saint Paul,Minnesota,Juneau,Alaska,Lincoln,Nebraska,Raleigh,North Carolina,Madison,Wisconsin,Olympia,Washington,Phoenix,Arizona,Lansing,Michigan,Honolulu,Hawaii,Jackson,Mississippi,Springfield,Illinois,Columbia,South Carolina,Annapolis,Maryland,Cheyenne,Wyoming,Salt Lake City,Utah,Atlanta,Georgia,Bismarck,North Dakota,Frankfort,Kentucky,Salem,Oregon,Little Rock,Arkansas,Des Moines,Iowa,Sacramento,California,Oklahoma City,Oklahoma,Charleston,West Virginia,Carson City,Nevada'.split(","),q="Arstotzka";i<100;i++){if(c[i]==b)q=c[i%2?i-1:i+1];}return q})())

Answer 2 will work with the code snippet button

\$\endgroup\$
  • \$\begingroup\$ I totally put the wrong piece of code up...zzz fixing \$\endgroup\$ – Spaceman Nov 19 '15 at 1:56
  • \$\begingroup\$ @GamrCorps all fixed :) sorry about that \$\endgroup\$ – Spaceman Nov 19 '15 at 2:01
  • \$\begingroup\$ Is answer one an acceptable answer in that it must work in console? \$\endgroup\$ – Spaceman Nov 19 '15 at 2:01
  • \$\begingroup\$ I would say yes. However. meta may disagree, although I doubt that people will. \$\endgroup\$ – GamrCorps Nov 19 '15 at 2:03
  • \$\begingroup\$ I just specified your console line a bit more and added a testing link, just so you know. \$\endgroup\$ – GamrCorps Nov 19 '15 at 2:10
3
\$\begingroup\$

Ruby,989

->s{a="Baton Rouge|Louisiana|Indianapolis|Indiana|Columbus|Ohio|Montgomery|Alabama|Helena|Montana|Denver|Colorado|Boise|Idaho|Austin|Texas|Boston|Massachusetts|Albany|New York|Tallahassee|Florida|Santa Fe|New Mexico|Nashville|Tennessee|Trenton|New Jersey|Jefferson|Missouri|Richmond|Virginia|Pierre|South Dakota|Harrisburg|Pennsylvania|Augusta|Maine|Providence|Rhode Island|Dover|Delaware|Concord|New Hampshire|Montpelier|Vermont|Hartford|Connecticut|Topeka|Kansas|Saint Paul|Minnesota|Juneau|Alaska|Lincoln|Nebraska|Raleigh|North Carolina|Madison|Wisconsin|Olympia|Washington|Phoenix|Arizona|Lansing|Michigan|Honolulu|Hawaii|Jackson|Mississippi|Springfield|Illinois|Columbia|South Carolina|Annapolis|Maryland|Cheyenne|Wyoming|Salt Lake City|Utah|Atlanta|Georgia|Bismarck|North Dakota|Frankfort|Kentucky|Salem|Oregon|Little Rock|Arkansas|Des Moines|Iowa|Sacramento|California|Oklahoma City|Oklahoma|Charleston|West Virginia|Carson City|Nevada".split(?|)
i=a.index(s)
i ?a[i^1]:"Arstotzka"}

Split the data into a single array. Find the index of the input in the array (index returns a falsy value of nil if the input is not there)

If falsy, return "Arstotzka" otherwise XOR the index with 1 to find the other member of the pair

In test program:

f=->s{a="Baton Rouge|Louisiana|Indianapolis|Indiana|Columbus|Ohio|Montgomery|Alabama|Helena|Montana|Denver|Colorado|Boise|Idaho|Austin|Texas|Boston|Massachusetts|Albany|New York|Tallahassee|Florida|Santa Fe|New Mexico|Nashville|Tennessee|Trenton|New Jersey|Jefferson|Missouri|Richmond|Virginia|Pierre|South Dakota|Harrisburg|Pennsylvania|Augusta|Maine|Providence|Rhode Island|Dover|Delaware|Concord|New Hampshire|Montpelier|Vermont|Hartford|Connecticut|Topeka|Kansas|Saint Paul|Minnesota|Juneau|Alaska|Lincoln|Nebraska|Raleigh|North Carolina|Madison|Wisconsin|Olympia|Washington|Phoenix|Arizona|Lansing|Michigan|Honolulu|Hawaii|Jackson|Mississippi|Springfield|Illinois|Columbia|South Carolina|Annapolis|Maryland|Cheyenne|Wyoming|Salt Lake City|Utah|Atlanta|Georgia|Bismarck|North Dakota|Frankfort|Kentucky|Salem|Oregon|Little Rock|Arkansas|Des Moines|Iowa|Sacramento|California|Oklahoma City|Oklahoma|Charleston|West Virginia|Carson City|Nevada".split(?|)
i=a.index(s)
i ?a[i^1]:"Arstotzka"}

puts f[gets.chop]
\$\endgroup\$
  • \$\begingroup\$ I wonder if you could save space by using ~a.index(s) and making the states and capitals relate that way (e.g., ["New York", "New Jersey", "California",..., "Sacramento", "Trenton", "Albany"]) \$\endgroup\$ – Not that Charles Nov 19 '15 at 19:09
3
\$\begingroup\$

PHP, 674 bytes

<?php $a=explode("\n",gzinflate('=SÛŽê0.|÷WôWº =Àr.p..GÓšÆj.WN.ÛýúãÀ²R¥ÞÆžñxRkL’¾{„7L.ªƒäŽ`-™#c@X†¶ÜGñ._/0.Ÿ‡KŽ°s,°‘.:.H\'¨=^p@X.\'Õ?.?§p#-e¢Ø.¼.G‚e‹N Î1q€.}a´.¦&À.cÄÆåH)EkzÁ0Á–îÕ?Ñ.Nè=:ƒ.Á»uä.áˆÆT½Ó.µ¡/n.¶.Ý.½\'k..=ð\'¥P..lE.i‚.]¯öTh9FÉÊpàÆ..Zødí80žI•à(9¹jŽ½$..U9^²v°7‚8ù..l.;›É¦G..{•.·..‚ƒ“–ªeôh.çR,™“Ç;Z癄F´}.[à0FÇöµ.8’7rø$5I©°¦kAZE &q“.œd$[á.†h...8U{ÌÞF*“.µ«..sÙP4äš.Î...úøp@OÜ9ØŠÚ„3T[xÙ ¶\¬9sl$D[ÔÎOÃhSžÍ].]1sï„..A.ü-V´Æ‚쌼qÜa€….KŒÏ&þŽÌ°Â¦..^®qd8Žj5W&ßÂÒ.¹eä\'hÆö4þWV.^‘Ü N.?gŽ¦²f8O2.ö#úT.±§jÆi‚¿..ÔÉ— À..[¬ežã€Úô?cÿ,ö]1ôæq‚.KKnú©4£.vJ.É^sJžì¨Xa.ýÓô9Åj#¶q;&r/‰l.‡’6˜¡gkW²±ë-º2àSÓëÍÄ£zzdÿl·ê7w6±.õDoé†-þ.'));$b=array_search($argv[1],$a);echo $a[$b?$b%2?$b+1:$b-1:0];

Hex:

00000000: 3C 3F 70 68 70 20 24 61 - 3D 65 78 70 6C 6F 64 65 |<?php $a=explode|
00000010: 28 22 5C 6E 22 2C 67 7A - 69 6E 66 6C 61 74 65 28 |("\n",gzinflate(|
00000020: 27 3D 53 DB 8E EA 30 0C - 7C F7 57 F4 57 BA A0 3D |'=S   0 | W W  =|
00000030: C0 72 13 70 16 9D 47 D3 - 9A C6 6A 1A 57 4E 02 DB | r p  G   j WN  |
00000040: FD FA E3 C0 B2 52 A5 DE - C6 9E F1 78 52 6B 4C 92 |     R     xRkL |
00000050: BE 7B 84 37 4C 12 AA 83 - E4 8E 60 2D 99 23 63 40 | { 7L     `- #c@|
00000060: 58 86 B6 DC 47 F1 1C 5F - 2F 30 13 9F 87 4B 8E B0 |X   G  _/0   K  |
00000070: 73 2C B0 91 90 3A 19 48 - 5C 27 A8 3D 5E 70 40 58 |s,   : H\' =^p@X|
00000080: 90 5C 27 C3 95 3F 05 3F - A7 70 23 2D 65 A2 D8 0A | \'  ? ? p#-e   |
00000090: BC 09 47 82 65 8B 4E A0 - CE 31 71 80 13 7D 61 B4 |  G e N  1q  }a |
000000a0: 1F A6 26 C0 06 63 C4 C6 - E5 48 29 45 6B 7A C1 30 |  &  c   H)Ekz 0|
000000b0: C1 96 EE D5 3F D1 1E 4E - E8 3D 3A 83 10 C1 BB 75 |    ?  N =:    u|
000000c0: E4 16 E1 88 C6 54 BD D3 - 03 B5 A1 2F 6E 04 B6 18 |     T     /n   |
000000d0: DD 8D BD 5C 27 6B 1E 02 - 3D F0 5C 27 A5 50 18 0A |   \'k  = \' P  |
000000e0: 6C 45 1A 69 82 15 5D AF - F6 54 68 39 46 C9 CA 70 |lE i  ]  Th9F  p|
000000f0: E0 C6 0D 12 5A F8 64 ED - 38 30 C2 9E 49 95 E0 28 |    Z d 80  I  (|
00000100: 39 B9 6A 8E BD 24 1B 12 - 55 39 5E B2 76 B0 37 82 |9 j  $  U9^ v 7 |
00000110: 38 F9 1B 16 6C 9D 3B 9B - C9 A6 47 0E 04 7B 95 1B |8   l ;   G  {  |
00000120: B7 14 1A 82 83 93 96 AA - 65 F4 68 AD E7 52 2C 99 |        e h  R, |
00000130: 93 C7 3B 5A E7 99 84 46 - B4 7D 08 5B E0 30 46 C7 |  ;Z   F } [ 0F |
00000140: F6 B5 18 38 92 37 72 F8 - 24 35 49 A9 B0 A6 6B 41 |   8 7r $5I   kA|
00000150: 5A 45 A0 26 71 93 13 9C - 64 24 5B E1 07 86 68 2E |ZE &q   d$[   h.|
00000160: 1E 8D 38 55 7B CC DE 46 - 2A 93 17 B5 AB 1C 08 73 |  8U{  F*      s|
00000170: D9 50 34 E4 9A 8D CE 17 - 1F 2E FA F8 70 40 4F DC | P4      .  p@O |
00000180: 39 D8 8A DA 84 33 54 5B - 78 D9 20 B6 5C AC 39 73 |9    3T[x   \ 9s|
00000190: 6C 24 44 5B D4 CE 4F C3 - 68 53 9E CD 5D 0E 5D 31 |l$D[  O hS  ] ]1|
000001a0: 73 EF 84 02 7F 41 AD FC - 2D 56 B4 C6 82 EC 8C BC |s    A  -V      |
000001b0: 71 DC 61 80 85 04 4B 8C - CF 26 FE 8E CC B0 C2 A6 |q a   K  &      |
000001c0: 7F 19 5E AE 71 64 38 8E - 6A 35 57 26 DF C2 D2 1B |  ^ qd8 j5W&    |
000001d0: B9 65 E4 5C 27 68 C6 F6 - 34 FE 57 56 1D 5E 91 DC | e \'h  4 WV ^  |
000001e0: A0 4E 0F 3F 67 8E A6 B2 - 66 38 4F 32 14 F6 23 FA | N ?g   f8O2  # |
000001f0: 54 AD B1 A7 6A C6 69 82 - BF 09 1D D4 C9 97 A0 C0 |T   j i         |
00000200: 1F 12 5B AC 65 9E E3 80 - DA F4 3F 63 FF 2C F6 5D |  [ e     ?c , ]|
00000210: 31 F4 E6 71 82 0F 4B 4B - 6E FA A9 34 A3 01 76 4A |1  q  KKn  4  vJ|
00000220: 9D C9 5E 73 4A 9E EC A8 - 58 61 AD FD D3 F4 39 C5 |  ^sJ   Xa    9 |
00000230: 6A 23 B6 71 3B 26 72 2F - 89 6C 14 87 92 36 98 A1 |j# q;&r/ l   6  |
00000240: 67 6B 57 B2 B1 EB 2D BA - 32 E0 53 D3 EB CD C4 A3 |gkW   - 2 S     |
00000250: 7A 7A 64 FF 6C B7 EA 37 - 77 36 B1 19 F5 44 6F E9 |zzd l  7w6   Do |
00000260: 86 2D FE 07 27 29 29 3B - 24 62 3D 61 72 72 61 79 | -  '));$b=array|
00000270: 5F 73 65 61 72 63 68 28 - 24 61 72 67 76 5B 31 5D |_search($argv[1]|
00000280: 2C 24 61 29 3B 65 63 68 - 6F 20 24 61 5B 24 62 3F |,$a);echo $a[$b?|
00000290: 24 62 25 32 3F 24 62 2B - 31 3A 24 62 2D 31 3A 30 |$b%2?$b+1:$b-1:0|
000002a0: 5D 3B                   -                         |];|
000002a2;

Explanation

<?php
// create array of capitals and states
$a = explode("\n", gzinflate(/* Arstotzka
                                Baton Rouge
                                Louisiana
                                Indianapolis
                                Indiana
                                ...
                             */));
$b = array_search($argv[1], $a); // find user input in list
echo $a[
  $b          // if $b is nonzero, the match succeded
    ?$b % 2   // checks if $b is odd, indicating a capital
      ?$b + 1 // $b points to capital; return the next item, the state
      :$b - 1 // $b points to state; return the previous item, the capital
    :0        // select "Arstotzka" otherwise
];
\$\endgroup\$
2
\$\begingroup\$

Prolog, 1221 bytes

p(X):-L=['Baton Rouge','Louisiana','Indianapolis','Indiana','Columbus','Ohio','Montgomery','Alabama','Helena','Montana','Denver','Colorado','Boise','Idaho','Austin','Texas','Boston','Massachusetts','Albany','New York','Tallahassee','Florida','Santa Fe','New Mexico','Nashville','Tennessee','Trenton','New Jersey','Jefferson','Missouri','Richmond','Virginia','Pierre','South Dakota','Harrisburg','Pennsylvania','Augusta','Maine','Providence','Rhode Island','Dover','Delaware','Concord','New Hampshire','Montpelier','Vermont','Hartford','Connecticut','Topeka','Kansas','Saint Paul','Minnesota','Juneau','Alaska','Lincoln','Nebraska','Raleigh','North Carolina','Madison','Wisconsin','Olympia','Washington','Phoenix','Arizona','Lansing','Michigan','Honolulu','Hawaii','Jackson','Mississippi','Springfield','Illinois','Columbia','South Carolina','Annapolis','Maryland','Cheyenne','Wyoming','Salt Lake City','Utah','Atlanta','Georgia','Bismarck','North Dakota','Frankfort','Kentucky','Salem','Oregon','Little Rock','Arkansas','Des Moines','Iowa','Sacramento','California','Oklahoma City','Oklahoma','Charleston','West Virginia','Carson City','Nevada'],nth0(I,L,X),J is I xor 1,nth0(J,L,E),write(E),!.
p(X):-write('Arstotzka').

Most of the byte count comes from the list of states and capitals.
All names of states and capitals need to be quoted atoms since they begin with upper case letters, costing us 202 bytes in 's alone.

How it works

 nth0(I,L,X)

Gets index I of element X in list L.

J is I xor 1

Xor index with 1 to get index of return value.

nth0(J,L,E)

Gets element E at index J of list L.

If any rule fails, print Arstotzka

Example

>p('Austin').
Texas

>p('Texas').
Austin
\$\endgroup\$
2
\$\begingroup\$

Python 2.7, 1271 1232 1054 bytes

y='Little Rock,Boise,Richmond,Denver,Olympia,Sacramento,Springfield,Baton Rouge,Tallahassee,Austin,Trenton,Lansing,Annapolis,Dover,Phoenix,Pierre,Charleston,Salt Lake City,Montpelier,Indianapolis,Concord,Providence,Madison,Oklahoma City,Santa Fe,Frankfort,Columbia,Atlanta,Boston,Salem,Juneau,Bismarck,Helena,Montgomery,Des Moines,Saint Paul,Jefferson,Lincoln,Hartford,Augusta,Carson City,Albany,Cheyenne,Columbus,Harrisburg,Honolulu,Raleigh,Nashville,Jackson,Topeka'.split(',')
z='Arkansas,Idaho,Virginia,Colorado,Washington,California,Illinois,Louisiana,Florida,Texas,New Jersey,Michigan,Maryland,Delaware,Arizona,South Dakota,West Virginia,Utah,Vermont,Indiana,New Hampshire,Rhode Island,Wisconsin,Oklahoma,New Mexico,Kentucky,South Carolina,Georgia,Massachusetts,Oregon,Alaska,North Dakota,Montana,Alabama,Iowa,Minnesota,Missouri,Nebraska,Connecticut,Maine,Nevada,New York,Wyoming,Ohio,Pennsylvania,Hawaii,North Carolina,Tennessee,Mississippi,Kansas'.split(',')
def a(b):
 m='Arstotzka'
 if b in y:m=z[y.index(b)]
 if b in z:m=y[z.index(b)]
 print m

Example I/O:

>>> a('Baton Rouge')
Louisiana
>>> a('Montana')
Helena
>>> a('asdfghjkl;')
Arstotzka

Edit: way improved

\$\endgroup\$
  • \$\begingroup\$ I wish there was some way I could compress the dictionary... \$\endgroup\$ – cat Nov 19 '15 at 0:56
  • \$\begingroup\$ @Sp3000 oh, no, not zip(), anything but that! (also I think trying to use string interpolation/concatenation on this will just make the code way longer while marginally shortening the dictionary, but i don't know how to try) \$\endgroup\$ – cat Nov 19 '15 at 1:05
  • \$\begingroup\$ Python2 is actually the exact same length because even though you can eliminate parens in print, you can't bind print to a letter. \$\endgroup\$ – cat Nov 19 '15 at 1:18
2
\$\begingroup\$

Perl 5, 999 bytes

My first post in codegolf.

%m;@r=qw/la ol ia nt en or is an in on/;map{$x=$_;$x=~s/$_/$r[$_]/eg for(0..9);$x=~s/\b(\w)/\U$1/g;($c,$s)=split'\|',$x;$m{$c}=$s;$m{$s}=$c}qw(bat9_rouge|lou62na 8d2nap16|8d2na c1umbus|ohio mo3gomery|a0bama hel4a|mo37a d4ver|c15ado bo6e|idaho aust8|texas bost9|massachusetts alb7y|new_y5k tal0hassee|fl5ida sa3a_fe|new_mexico nashville|t4nessee tre39|new_jersey jeffers9|m6souri richm9d|virg82 pierre|south_dakota harr6burg|p4nsylv72 augusta|ma8e provid4ce|rhode_60nd dover|de0ware c9c5d|new_hampshire mo3pelier|vermo3 hartf5d|c9necticut topeka|k7sas sai3_paul|m8nesota juneau|a0ska l8c1n|nebraska raleigh|n5th_car18a mad69|w6c9s8 1ymp2|wash8gt9 pho4ix|ariz9a 0ns8g|michig7 h91ulu|hawaii jacks9|m6s6sippi spr8gfield|ill8o6 c1umb2|south_car18a 7nap16|mary0nd chey4ne|wyom8g salt_0ke_city|utah at03a|ge5g2 b6marck|n5th_dakota fr7kf5t|ke3ucky salem|5eg9 little_rock|ark7sas des_mo8es|iowa sacrame3o|calif5n2 ok0homa_city|ok0homa charlest9|west_virg82 cars9_city|nevada);print $m{$ARGV[0]}||"Arstotzka"

Ungolfed:

%m;
@r=qw/la ol ia nt en or is an in on/; # Top 10 bigrams
map{
    $x=$_;
    $x=~s/$_/$r[$_]/eg for(0..9); # Resolve bigram placeholders
    $x=~s/\b(\w)/\U$1/g;          # ucfirst each word
    ($c,$s)=split'\|',$x;         # split city and state
    $m{$c}=$s;                    # key on both city and state
    $m{$s}=$c
} qw(
    bat9_rouge|lou62na
    8d2nap16|8d2na
    c1umbus|ohio
    mo3gomery|a0bama
    hel4a|mo37a
    d4ver|c15ado
    bo6e|idaho
    aust8|texas
    bost9|massachusetts
    alb7y|new_y5k
    tal0hassee|fl5ida
    sa3a_fe|new_mexico
    nashville|t4nessee
    tre39|new_jersey
    jeffers9|m6souri
    richm9d|virg82
    pierre|south_dakota
    harr6burg|p4nsylv72
    augusta|ma8e
    provid4ce|rhode_60nd
    dover|de0ware
    c9c5d|new_hampshire
    mo3pelier|vermo3
    hartf5d|c9necticut
    topeka|k7sas
    sai3_paul|m8nesota
    juneau|a0ska
    l8c1n|nebraska
    raleigh|n5th_car18a
    mad69|w6c9s8
    1ymp2|wash8gt9
    pho4ix|ariz9a
    0ns8g|michig7
    h91ulu|hawaii
    jacks9|m6s6sippi
    spr8gfield|ill8o6
    c1umb2|south_car18a
    7nap16|mary0nd
    chey4ne|wyom8g
    salt_0ke_city|utah
    at03a|ge5g2
    b6marck|n5th_dakota
    fr7kf5t|ke3ucky
    salem|5eg9
    little_rock|ark7sas
    des_mo8es|iowa
    sacrame3o|calif5n2
    ok0homa_city|ok0homa
    charlest9|west_virg82
    cars9_city|nevada
);
print $m{$ARGV[0]}||"Arstotzka"
\$\endgroup\$
1
\$\begingroup\$

Perl 5 , 1029 bytes

$h{$h{$_}}=$_ for keys{%h=(Indianapolis,Indiana,Columbus,Ohio,Montgomery,Alabama,Helena,Montana,Denver,Colorado,Boise,Idaho,Austin,Texas,Boston,Massachusetts,Tallahassee,Florida,Nashville,Tennessee,Jefferson,Missouri,Richmond,Virginia,Harrisburg,Pennsylvania,Augusta,Maine,Dover,Delaware,Montpelier,Vermont,Hartford,Connecticut,Topeka,Kansas,Juneau,Alaska,Lincoln,Nebraska,Madison,Wisconsin,Olympia,Washington,Phoenix,Arizona,Lansing,Michigan,Honolulu,Hawaii,Jackson,Mississippi,Springfield,Illinois,Annapolis,Maryland,Cheyenne,Wyoming,Atlanta,Georgia,Frankfort,Kentucky,Salem,Oregon,Sacramento,California,Charleston,'West Virginia','Carson City',Nevada,'Baton Rouge',Louisiana,Albany,'New York','Santa Fe','New Mexico','Trenton','New Jersey',Pierre,'South Dakota',Providence,'Rhode Island',Concord,'New Hampshire','Saint Paul',Minnesota,Raleigh,'North Carolina',Columbia,'South Carolina','Salt Lake City',Utah,Bismarck,'North Dakota','Little Rock',Arkansas,'Des Moines',Iowa,'Oklahoma City',Oklahoma)};say$h{$ARGV[0]}//Arstotzka
\$\endgroup\$
  • \$\begingroup\$ This is why I love Perl. Good job. \$\endgroup\$ – tale852150 Nov 19 '15 at 20:56
1
\$\begingroup\$

Haskell, 1077 1069 1068 1063 bytes

z s=last$"Arstotzka":[last$l:[r|s==l]|(l,_:r)<-span(<'~')<$>lines"Baton Rouge~Louisiana\nIndianapolis~Indiana\nColumbus~Ohio\nMontgomery~Alabama\nHelena~Montana\nDenver~Colorado\nBoise~Idaho\nAustin~Texas\nBoston~Massachusetts\nAlbany~New York\nTallahassee~Florida\nSanta Fe~New Mexico\nNashville~Tennessee\nTrenton~New Jersey\nJefferson~Missouri\nRichmond~Virginia\nPierre~South Dakota\nHarrisburg~Pennsylvania\nAugusta~Maine\nProvidence~Rhode Island\nDover~Delaware\nConcord~New Hampshire\nMontpelier~Vermont\nHartford~Connecticut\nTopeka~Kansas\nSaint Paul~Minnesota\nJuneau~Alaska\nLincoln~Nebraska\nRaleigh~North Carolina\nMadison~Wisconsin\nOlympia~Washington\nPhoenix~Arizona\nLansing~Michigan\nHonolulu~Hawaii\nJackson~Mississippi\nSpringfield~Illinois\nColumbia~South Carolina\nAnnapolis~Maryland\nCheyenne~Wyoming\nSalt Lake City~Utah\nAtlanta~Georgia\nBismarck~North Dakota\nFrankfort~Kentucky\nSalem~Oregon\nLittle Rock~Arkansas\nDes Moines~Iowa\nSacramento~California\nOklahoma City~Oklahoma\nCharleston~West Virginia\nCarson City~Nevada",s==l||s==r]

Try it online!

After wasting a lot of time trying to golf an implementation of Huffman coding I realized the straightforward approach is probably shorter. Ah, well.

EDIT: Thanks to @Laikoni for taking off 8 bytes!

EDIT: Thanks again Laikoni for that additional byte. Good idea to use '~'

EDIT: Thanks to Laikoni for taking off those 5 bytes!

\$\endgroup\$
  • 1
    \$\begingroup\$ You can use pattern matching instead of fmap tail: (l,_:r)<-span(/=','). \$\endgroup\$ – Laikoni Nov 2 '17 at 20:28
  • \$\begingroup\$ Finally span(>',') should also work. \$\endgroup\$ – Laikoni Nov 2 '17 at 20:47
  • \$\begingroup\$ @Laikoni span(>',') doesn't work if there's a space in the name of the capital (e.g. Baton Rouge) since ' '<',' \$\endgroup\$ – user1472751 Nov 3 '17 at 13:00
  • 1
    \$\begingroup\$ I see, but you could use ~ instead of ,, which is larger than all other characters used. \$\endgroup\$ – Laikoni Nov 3 '17 at 15:57
  • 1
    \$\begingroup\$ last$l:[r|s==l] is shorter than filter(/=s)[l,r]!!0 and s==l||s==r beats elem s[l,r] by one byte. \$\endgroup\$ – Laikoni Nov 4 '17 at 10:36
1
\$\begingroup\$

T-SQL, 1402 bytes (counting size of .sql file)

create table t(s varchar(99),c varchar(99))insert into t values('Baton Rouge','Louisiana'),('Indianapolis','Indiana'),('Columbus','Ohio'),('Montgomery','Alabama'),('Helena','Montana'),('Denver','Colorado'),('Boise','Idaho'),('Austin','Texas'),('Boston','Massachusetts'),('Albany','New York'),('Tallahassee','Florida'),('Santa Fe','New Mexico'),('Nashville','Tennessee'),('Trenton','New Jersey'),('Jefferson','Missouri'),('Richmond','Virginia'),('Pierre','South Dakota'),('Harrisburg','Pennsylvania'),('Augusta','Maine'),('Providence','Rhode Island'),('Dover','Delaware'),('Concord','New Hampshire'),('Montpelier','Vermont'),('Hartford','Connecticut'),('Topeka','Kansas'),('Saint Paul','Minnesota'),('Juneau','Alaska'),('Lincoln','Nebraska'),('Raleigh','North Carolina'),('Madison','Wisconsin'),('Olympia','Washington'),('Phoenix','Arizona'),('Lansing','Michigan'),('Honolulu','Hawaii'),('Jackson','Mississippi'),('Springfield','Illinois'),('Columbia','South Carolina'),('Annapolis','Maryland'),('Cheyenne','Wyoming'),('Salt Lake City','Utah'),('Atlanta','Georgia'),('Bismarck','North Dakota'),('Frankfort','Kentucky'),('Salem','Oregon'),('Little Rock','Arkansas'),('Des Moines','Iowa'),('Sacramento','California'),('Oklahoma City','Oklahoma'),('Charleston','West Virginia'),('Carson City','Nevada') declare @i varchar(99) = 'Austin' select coalesce((select s from t where c = @i union select c from t where s = @i),(select 'Arstotzka'))

Fort this kind of task set logic trumps procedural logic making easy and fast to solve it on a relational DB.

Below the ungolfed code. Note we can just run the create/populate part once in a separate batch

create table t
(
     s varchar(99)
    ,c varchar(99)
)

insert into t
 values
 ('Baton Rouge','Louisiana')
,('Indianapolis','Indiana')
,('Columbus','Ohio')
,('Montgomery','Alabama')
,('Helena','Montana')
,('Denver','Colorado')
,('Boise','Idaho')
,('Austin','Texas')
,('Boston','Massachusetts')
,('Albany','New York')
,('Tallahassee','Florida')
,('Santa Fe','New Mexico')
,('Nashville','Tennessee')
,('Trenton','New Jersey')
,('Jefferson','Missouri')
,('Richmond','Virginia')
,('Pierre','South Dakota')
,('Harrisburg','Pennsylvania')
,('Augusta','Maine')
,('Providence','Rhode Island')
,('Dover','Delaware')
,('Concord','New Hampshire')
,('Montpelier','Vermont')
,('Hartford','Connecticut')
,('Topeka','Kansas')
,('Saint Paul','Minnesota')
,('Juneau','Alaska')
,('Lincoln','Nebraska')
,('Raleigh','North Carolina')
,('Madison','Wisconsin')
,('Olympia','Washington')
,('Phoenix','Arizona')
,('Lansing','Michigan')
,('Honolulu','Hawaii')
,('Jackson','Mississippi')
,('Springfield','Illinois')
,('Columbia','South Carolina')
,('Annapolis','Maryland')
,('Cheyenne','Wyoming')
,('Salt Lake City','Utah')
,('Atlanta','Georgia')
,('Bismarck','North Dakota')
,('Frankfort','Kentucky')
,('Salem','Oregon')
,('Little Rock','Arkansas')
,('Des Moines','Iowa')
,('Sacramento','California')
,('Oklahoma City','Oklahoma')
,('Charleston','West Virginia')
,('Carson City','Nevada')

And with that data persisted only use this query

declare @i varchar(99) = 'Austin'

select coalesce(
    (
       select s from t
       where c = @i

       union

       select c from t where s = @i
    ),
    (select 'Arstotzka')
)

The variable declaration is not mandatory, of course you can just put the input directly in the query and spare 10 bytes but I prefer my queries parametized.

\$\endgroup\$
  • \$\begingroup\$ 387 Bytes? What did you count? The long line in the first part of the answer is 1437 Bytes. \$\endgroup\$ – matz Nov 20 '15 at 12:37
  • \$\begingroup\$ Sorry @matz posted the wrong file size \$\endgroup\$ – jean Nov 20 '15 at 12:43
  • \$\begingroup\$ Is this Oracle PL*SQL? You might wanna specify that in the title. Also might be able to save bytes using sqlldr format rather than direct insert - sqlldr doesn't need quotes everywhere, example \$\endgroup\$ – roblogic Sep 15 at 4:26
1
\$\begingroup\$

Zsh, 993 928 924 bytes

-4 bytes thanks to @spuck's Bash answer, replacing th Dakota and th Carolina

a=(_ Bat4\ Rouge Lou7i5a Indi5apol7 Indi5a Columbus Ohio M4tgomery Alabama Hel8a M4t5a D8ver Col9ado Bo7e Idaho Aust6 Texas Bost4 Massachusetts Alb5y New\ Y9k Tallahassee Fl9ida S5ta\ Fe New\ Mexico Nashville T8nessee Tr8t4 New\ Jersey Jeffers4 M7souri Richm4d Virg6ia Pierre Sou3 Harr7burg P8nsylv5ia Augusta Ma6e Provid8ce Rhode\ 7l5d Dover Delaware C4c9d New\ Hampshire M4tpelier Verm4t Hartf9d C4necticut Topeka K5sas Sa6t\ Paul M6nesota Juneau Alaska L6coln Nebraska Raleigh N92 Mad74 W7c4s6 Olympia Wash6gt4 Pho8ix Ariz4a L5s6g Michig5 H4olulu Hawaii Jacks4 M7s7sippi Spr6gfield Ill6o7 Columbia Sou2 5napol7 Maryl5d Chey8ne Wyom6g Salt\ Lake1 Utah Atl5ta Ge9gia B7marck N93 Fr5kf9t K8tucky Salem 9eg4 Little\ Rock Ark5sas Des\ Mo6es Iowa Sacram8to Calif9nia 01 0 Charlest4 West\ Virg6ia Cars41 Nevada)
for w (Oklahoma \ City th\ Carol6a th\ Dakota on an in is en or)a=(${a//$[i++]/$w})
<<<${a[$a[(ie)$1]^1]:-Arstotzka}

Try it online! Try it online! Try it online!

Manual compression by replacing numbers 0..9 in the array.

Because Zsh uses 1-indexed arrays, we have to add a dummy element at the start.

$a[(i)$1] gets the index of the matching array element. Adding the (e) flag disables pattern matching and uses plain string matching. If we don't do this, Ut* matches Utah and we will print Carson City. If the element is not found, this returns an index beyond the end of the array.

We then xor with 1 and get the corresponding paired element. If our dummy element at index 1 was matched, this will be $a[0], which substitutes the empty string. In any case, if this is empty, ${ :-Arstotzka} will substitute Arstotzka instead.

\$\endgroup\$
  • \$\begingroup\$ I like your pass to expand 0..9. Tempted to borrow it. \$\endgroup\$ – spuck Sep 30 at 17:48
1
\$\begingroup\$

Bash, 927 978 1003 1051 976 bytes

One more edit, reducing bytes taken by spaces and newlines in $T.

Also re-ordered the substitution for 0..9, fixing a problem handling "Carol1a", since the substitution of 1->"in" would have already been accomplished before that point.

Editing my answer after @GammaFuntion's comment below; I was not meeting the original rules to handle all input; a string that partially matched a state or capital would give wrong output.

His/Her Zsh answer inspired me to also use the XOR rather than the modulo 100 indexing and to do some manual compression using numerals 0..9. I wrote a script to do some quick and dirty calculations on the input list to decide which ten strings would give the best byte reduction. Then used that script iteratively to reduce the strings. Check it out here: Try it online!

T="Bat8.RougeLou3i2aIndi2apol3Indi2aColumbusOhioM8tgom9yAlabamaHel6aM8t2aD6v9Col5adoBo3eIdahoAust4Tex7Bost8M7sachusettsAlb2yNew.Y5kTallah7seeFl5idaS2ta.FeNew.MexicoN7hvilleT6nesseeTr6t8New.J9seyJeff9s8M3souriRichm8dVirg4iaPi9reSou0Harr3burgP6nsylv2iaAugustaMa4eProvid6ceRhode.Isl2dDov9DelawareC8c5dNew.HampshireM8tpeli9V9m8tHartf5dC8necticutTopekaK2s7Sa4t.PaulM4nesotaJuneauAl7kaL4colnNebr7kaRaleighN51Mad38W3c8s4OlympiaW7h4gt8Pho6ixAriz8aL2s4gMichig2H8oluluHawaiiJacks8M3s3sippiSpr4gfieldIll4o3ColumbiaSou1Annapol3Maryl2dChey6neWyom4gSalt.Lake.CityUtahAtl2taGe5giaB3marckN50Fr2kf5tK6tuckySalemOreg8Little.RockArk2s7Des.Mo4esIowaSacram6toCalif5niaOklahoma.CityOklahomaCharlest8West.Virg4iaCars8.CityNevada"
for t in th.Dakota th.Carol4a an is in or en as on er;{ T=${T//$[j++]/$t};}
for t in {A..Z};{ T=${T//$t/ $t};}
S=${T//. /.}
U=($S)
for s in $S;{ [ "${s//./ }" = "$1" ]&&echo ${U[i^1]//./ }&&exit;i=$[i+1];}
echo Arstotzka

Try it online!

Try it online!

Try it online!

Try it online!

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Clever thought to use replacement that way, but if I give Rock as an input, the output is Little Arkansas instead of Arstotzka. \$\endgroup\$ – GammaFunction Sep 28 at 13:58
  • \$\begingroup\$ @GammaFunction, you are right. I'm editing my answer to handle this. \$\endgroup\$ – spuck Sep 30 at 17:38
  • \$\begingroup\$ Feel free to rip-off my Zsh answer. The array expansions won't be as condensed, but you can take advantage of zero-indexing. \$\endgroup\$ – GammaFunction Sep 30 at 17:51
  • \$\begingroup\$ Got to 986: for t in on in an is th.Carol1a or en as th.Dakota er;{ S=${S//$[j++]/$t};}. Also, nice use of th., I'm adding it to my zsh answer. \$\endgroup\$ – GammaFunction Oct 1 at 16:19
  • \$\begingroup\$ Oh, and unset/empty variables are implicitly 0 in arithmetic contexts, you don't need to set i (or j) beforehand. \$\endgroup\$ – GammaFunction Oct 1 at 16:43
0
\$\begingroup\$

R, 1294 bytes

d=data.frame(a=c("Baton Rouge","Indianapolis","Columbus","Montgomery","Helena","Denver","Boise","Austin","Boston","Albany","Tallahassee","Santa Fe","Nashville","Trenton","Jefferson","Richmond","Pierre","Harrisburg","Augusta","Providence","Dover","Concord","Montpelier","Hartford","Topeka","Saint Paul","Juneau","Lincoln","Raleigh","Madison","Olympia","Phoenix","Lansing","Honolulu","Jackson","Springfield","Columbia","Annapolis","Cheyenne","Salt Lake City","Atlanta","Bismarck","Frankfort","Salem","Little Rock","Des Moines","Sacramento","Oklahoma City","Charleston","Carson City"),b=c("Louisiana","Indiana","Ohio","Alabama","Montana","Colorado","Idaho","Texas","Massachusetts","New York","Florida","New Mexico","Tennessee","New Jersey","Missouri","Virginia","South Dakota","Pennsylvania","Maine","Rhode Island","Delaware","New Hampshire","Vermont","Connecticut","Kansas","Minnesota","Alaska","Nebraska","North Carolina","Wisconsin","Washington","Arizona","Michigan","Hawaii","Mississippi","Illinois","South Carolina","Maryland","Wyoming","Utah","Georgia","North Dakota","Kentucky","Oregon","Arkansas","Iowa","California","Oklahoma","West Virginia","Nevada"),stringsAsFactors=F)
print(ifelse(any(e<-d[,1]%in%(n<-scan("","raw",sep='$'))),d[e,2],ifelse(any(f<-d[,2]%in%n),d[f,1],'Arstotzka')))

It is case senstive according to the input shown as example.

Ungolfed code (slightly different in the position of scan):

n<-scan("","raw",sep='$')
print(
  ifelse(
    any(e<-d[,1]%in%n), # test city
    d[e,2], # if yes return the state
    ifelse(
      any(f<-d[,2]%in%n), # test state
      d[f,1], # return city
      'Arstotzka'
    )
  )
)
\$\endgroup\$
0
\$\begingroup\$

Java, 1312

public class a{public static void main(String[]x){List<String>a=Arrays.asList("Baton Rouge","Louisiana","Indianapolis", "Indiana","Columbus","Ohio", "Montgomery","Alabama","Helena","Montana","Denver","Colorado","Boise","Idaho","Austin","Texas","Boston","Massachusetts","Albany","New York","Tallahassee","Florida","Santa Fe","New Mexico","Nashville","Tennessee","Trenton","New Jersey","Jefferson","Missouri","Richmond","Virginia","Pierre","South Dakota","Harrisburg","Pennsylvania","Augusta","Maine","Providence","Rhode Island","Dover","Delaware","Concord","New Hampshire","Montpelier","Vermont","Hartford","Connecticut","Topeka","Kansas","Saint Paul","Minnesota","Juneau","Alaska","Lincoln","Nebraska","Raleigh","North Carolina","Madison","Wisconsin","Olympia","Washington","Phoenix","Arizona","Lansing","Michigan","Honolulu","Hawaii","Jackson","Mississippi","Springfield","Illinois","Columbia","South Carolina","Annapolis","Maryland","Cheyenne","Wyoming","Salt Lake City","Utah","Atlanta","Georgia","Bismarck","North Dakota","Frankfort","Kentucky","Salem","Oregon","Little Rock","Arkansas","Des Moines","Iowa","Sacramento","California","Oklahoma City","Oklahoma","Charleston","West Virginia","Carson City","Nevada");int b=a.indexOf(x[0]);String s=b<0?s="Arstotzka":a.get(b/2*2+((b+1)%2));System.out.print(s);}}
\$\endgroup\$
0
\$\begingroup\$

Javascript, 1042

var x=prompt(),a="Baton Rouge,Louisiana,Indianapolis,Indiana,Columbus,Ohio,Montgomery,Alabama,Helena,Montana,Denver,Colorado,Boise,Idaho,Austin,Texas,Boston,Massachusetts,Albany,New York,Tallahassee,Florida,Santa Fe,New Mexico,Nashville,Tennessee,Trenton,New Jersey,Jefferson,Missouri,Richmond,Virginia,Pierre,South Dakota,Harrisburg,Pennsylvania,Augusta,Maine,Providence,Rhode Island,Dover,Delaware,Concord,New Hampshire,Montpelier,Vermont,Hartford,Connecticut,Topeka,Kansas,Saint Paul,Minnesota,Juneau,Alaska,Lincoln,Nebraska,Raleigh,North Carolina,Madison,Wisconsin,Olympia,Washington,Phoenix,Arizona,Lansing,Michigan,Honolulu,Hawaii,Jackson,Mississippi,Springfield,Illinois,Columbia,South Carolina,Annapolis,Maryland,Cheyenne,Wyoming,Salt Lake City,Utah,Atlanta,Georgia,Bismarck,North Dakota,Frankfort,Kentucky,Salem,Oregon,Little Rock,Arkansas,Des Moines,Iowa,Sacramento,California,Oklahoma City,Oklahoma,Charleston,West Virginia,Carson City,Nevada".split(","),b=a.indexOf(x),s=b<0?s="Arstotzka":a[Math.floor(b/2)*2+((b+1)%2)];alert(s);
\$\endgroup\$
0
\$\begingroup\$

AWK, 1009 bytes

{split("Baton Rouge,Louisiana,Indianapolis,Indiana,Columbus,Ohio,Montgomery,Alabama,Helena,Montana,Denver,Colorado,Boise,Idaho,Austin,Texas,Boston,Massachusetts,Albany,New York,Tallahassee,Florida,Santa Fe,New Mexico,Nashville,Tennessee,Trenton,New Jersey,Jefferson,Missouri,Richmond,Virginia,Pierre,South Dakota,Harrisburg,Pennsylvania,Augusta,Maine,Providence,Rhode Island,Dover,Delaware,Concord,New Hampshire,Montpelier,Vermont,Hartford,Connecticut,Topeka,Kansas,Saint Paul,Minnesota,Juneau,Alaska,Lincoln,Nebraska,Raleigh,North Carolina,Madison,Wisconsin,Olympia,Washington,Phoenix,Arizona,Lansing,Michigan,Honolulu,Hawaii,Jackson,Mississippi,Springfield,Illinois,Columbia,South Carolina,Annapolis,Maryland,Cheyenne,Wyoming,Salt Lake City,Utah,Atlanta,Georgia,Bismarck,North Dakota,Frankfort,Kentucky,Salem,Oregon,Little Rock,Arkansas,Des Moines,Iowa,Sacramento,California,Oklahoma City,Oklahoma,Charleston,West Virginia,Carson City,Nevada,Arstotzka,",S,",")
for(x in S)if(S[x]==$0)break
$0=S[x-1+x%2*2]}1

Try it online!

If there's a way to shrink this, I'd be happy to hear about it. :)

\$\endgroup\$
0
\$\begingroup\$

J, 843 829 bytes

-&.((<;._1'-rstotzk7Bat> Roug/Ind6?p@;5@umbus.o8gom4y,Hel97D9v4,Bo;e-ust=,Bost>-lb<y,Tal1h2se/Sa8a F/N2hvill/Tre8>,Jeff4s>,Richm>d,Pi4r/H3r;burg-ugust7Provid9c/Dov45>c:d.o8peli4,H3tf:d,Topek7Sai8 Paul,Juneau,L=c@n,Raleigh.ad;>,Olymp6,Pho9ix,L<s=g,H>@ulu,Jacks>Apr=gfield5@umb6-n?p@;5hey9n/Salt Lake City-t187B;m3ck,Fr<kf:tAalem,Little Rock,Des Mo=esAacrame8o,Ok1homa City5h3lest>53s> City0evad7West Virg=6,Ok1homa5alif:n6,Iowa-rk<s2,Oreg>,Ke8ucky0:th Dakot7Ge:g6,Utah,Wyom=g.3y1ndAouth C3@=7Ill=o;.;s;sippi,Hawaii.ichig<-riz>7W2h=gt>,W;c>s=0:th C3@=a0ebr2ka-1ska.=nesot7K<s25>necticut,V4mo80ew Hampshir/De1w3/Rhode Is1nd.a=/P9nsylv<6Aouth Dakot7Virg=6.;souri0ew J4sey,T9nesse/New Mexico,Fl:ida0ew Y:k.2sachusetts,Tex2,Idaho5@:ado.o8<a-1bam7Ohio,Ind6n7Lou;6?'rplc(45{21;/\a.),._2<\',A,Me,,Nlaasarer,Ciaa,ntenorisaninonnaol,S')i.<)

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.