States and Capitals
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.
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 [email protected]..
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 .#[email protected]`
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.
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 [email protected].
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 [email protected]`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.