11
\$\begingroup\$

Goal

Your task is to create a program taking no input that returns the link to your answer. You can find a link to your answer by clicking at the share button on your answer.

Rules

  1. Standard loopholes apply except for searching for the answer online. In fact, you are expected to find the answer online.
  2. You are allowed to use curl or the equivalent, but you are not allowed to use any html parser library or built-in. If you need to import the library, the import declaration does NOT count into the code size as long as you don't rename any identifiers.
  3. You are not allowed to assume, or hardcode, the link to your answer. You have to search this page for your submission. If you remove your submission and re-add it, and your code breaks, you fail the challenge.
  4. You may still assume your username, the contents of your code, and the programming language/byte count you are using. You are not allowed to assume the description below your code. If you edit the description part of the submission and the code breaks, you fail the challenge. You may assume that the code of each submission is unique and no one copies your submission into another answer.
  5. Your answer has to be exactly the same as what you get when you clicked on the share button.
  6. You may post the dummy answer for the prototyping purpose. Just delete it immediately after posting, and edit your answer into it once completed.
  7. You are not allowed to take an user input

Scoring

This is so, the shortest answer in bytes wins.

\$\endgroup\$
9
  • 4
    \$\begingroup\$ BAN URL SHORTENERS before anyone has a 'clever idea'. \$\endgroup\$ – A username Jun 1 at 10:15
  • 1
    \$\begingroup\$ I added a couple of bits to the text. Namely, bolded the bit about requiring it to fetch the answer rather than hardcode, and changed the placeholder bit to tell users to delete their answer (your deleted answers are still visible to you), then edit in their actual submission \$\endgroup\$ – caird coinheringaahing Jun 1 at 10:28
  • \$\begingroup\$ "taking the input that returns the link to your answer"? Do you mean "taking no input"? \$\endgroup\$ – tsh Jun 1 at 10:32
  • 2
    \$\begingroup\$ May we hard-code our user ID (that goes on the end of the link)? \$\endgroup\$ – pxeger Jun 1 at 13:41
  • 2
    \$\begingroup\$ @pxeger there seems to be a consensus in the existing answers (even before I posted) that the user ID should be discarded, probably because it changes for every person clicking on the "share" link. IMO this is also a good idea because we have already a byte count advantage for the users with a shorter ID, and the cost for adding it at the end of the string is fixed for any answer given a language \$\endgroup\$ – Kaddath Jun 1 at 16:25
9
\$\begingroup\$

Jelly, 61 57 bytes

“¦ɦḍɓḃɠṭṢ⁽&½ṛøHØлȮ;⁾q/;“£ɓ⁶’ŒGœṣ“"/a/”iƇ”ɦṪṣ”=-3ịḊḣ6⁾a/;

Try it online!

What's a worse idea than parsing HTML with regex? Parsing HTML with Jelly

This outputs codegolf.stackexchange.com/a/226641. This will fail if this is "reposted" after ~780000 more posts are posted to the site, but that is currently 3.5 times the number of posts we currently have. A more future-proof version comes out as the same length

How it works

This works in two parts. First we build and print the string to fetch the HTML of the question (including the answers), then we process and extract my answer id

“¦ɦḍɓḃɠṭṢ⁽&½ṛøHØлȮ;⁾q/;“£ɓ⁶’ŒG - Main link
“¦ɦḍɓḃɠṭṢ⁽&½ṛøHØл              - Compressed string "codegolf.stackexchange.com/"
                  Ȯ             - Print this
                   ;⁾q/         - Concatenate "q/"
                        “£ɓ⁶’   - Compressed integer: 226635
                       ;        - Concatenate; "codegolf.stackexchange.com/q/226635"
                             ŒG - GET request, prepending "http://"

Building the string this way takes 2 more bytes than simply compressing codegolf.stackexchange.com/q/226635: “ñoHƑ;ɦŻt%WɓḤȦ3æḶ2ỤƭȮ¦1ẈA». However, by having “¦ɦḍɓḃɠṭṢ⁽&½ṛøHØл ("codegolf.stackexchange.com/") by itself at the start, we can print it using Ȯ, allowing us to just output a/226641 later on

œṣ“"/a/”iƇ”ɦṪṣ”=-3ịḊḣ6⁾a/; - Main link.
                             To our left, we have the HTML content of this page
  “"/a/”                   - Yield '"/a/'
œṣ                         - Split at '"/a/', splitting roughly into answers
        iƇ”ɦ               - Keep those with the "ɦ" character in them. Currently, that's just me
            Ṫ              - Extract this bit of HTML
             ṣ”=           - Split at equals signs
                -3ị        - Take the 4th from last, which is '"226641">    <div class'
                   Ḋ       - Remove the quote
                    ḣ6     - Extract the first 6 characters
                   fØD     - Future proof version: keep only digits
                      ⁾a/; - Prepend "a/" and output
                      
\$\endgroup\$
5
  • 8
    \$\begingroup\$ a̲̲̼̙̮͇͔͍̭͈̮̖̲̒͑͐̆̏̌ͬͤ̎͒̆͑̄ͦ͐́̈ͬ̎͟͠ͅa̢͒̃ͯ̒̏̇̏̒͊͐ͧ̆ͤ̏̃̓̐̊̿͏̫̠͎̗͈̳a̛̤̣̩̺̲̼̩͕͕͈̪̺͎͓̰̩̙̍ͭͬ͐͢͢a̸͑̔͋̊̐̿̏̎҉͖̬̬̮̩ȃ̴̛̱̮͔͔͚̰͙̞̟͚̣͉̬̱̲͖̺͇ͪͨͦ͋̎ͭ̌ͩͮͥͫả̶̧̛̗̬͈̝͉͖̥͕̰̹̻͎̥ͣ̇̃ͬ͘͝a̸̴̟̗͈̮̰̓̎̇̊ͩ̒̌̓̚͢͟͟a̷̙̘̭͎̺̜̺̟̮̜̜̳̻̲̰ͦ͊̑͗ͥͨ̎̚͜͟á̶̧̨̳͚͍̞̜̺ͭͯ̑̔̿͗̀̚̕a̷̦͍̺͚̝̙͇̣̖̹͚͇͐̿̏́͌͊̀̏̀̓̌̾̍͛́́́̚̕a̡̧̧̫̹̺̜͙͖̦͈̗̺̠͕̰̺̓͆̃̽ͣͮ̅͐ͨͬͩ̿̒̀ͅa̸̴͇͔͈̟̥̘̝̝͚̲ͬ̆̽̊̅͞ą̧̛͋̂ͤͣ҉̗͓̠̙̮̘͔̦͙̪̝̦͚͕̻̩̣͇ͅa͌ͤ͊̆̏ͬ͌̌͌͏͙̥͕͚͡͝ả̸̈ͫ̄̓͊̂̔ͭ͛̃̀͒ͧͥ́̉͡҉̟͓̲̰̫͈̭̤ͅa̧͚̤͖̩̣͎̥͈̬͖̩ͯ̅̎ͤ̄͒ͫ̕͘ͅa̴̡̳̼͇̺̥̗̫͖͛̽̔̽͆͗͛́͞ą̨̼̠̭̪̇̈́ͬ̾͜͡ă̶̢͖̺̭̼̻ͤͦͬ̏̓̀ͥ̃̋a̴͎̼̰̩̣͍̥̳̺͌́͂̌͟͝a̴̦̻̻̯̥̗̝̲͖̜̭̥̠̮̣̩̦̜̰̿̑͒̇͐̏́ͣ͊ͬͦ́͑́͘a̓ͣ̑ͫ̌͛̈́͌̈͂͗̈͆͗͌ͨ͒̑́҉̷҉̘̰̥̼̖͚a̷̡̹͍͔̪̱̺̺͍̒ͮ̔̀ͩ͐ͫ͆ͤ͂̏ͥ̓̅̆͋́͜ \$\endgroup\$ – Unrelated String Jun 1 at 10:59
  • \$\begingroup\$ 57 unicode characters != 57 bytes \$\endgroup\$ – Tim Jun 1 at 18:49
  • 7
    \$\begingroup\$ @Tim Yes, but no. Jelly uses a custom code page to represent all single byte values as printable characters. The exact byte stream of this program is fe 05 a0 d5 9b e7 9f e0 b7 8d 26 0a de 1d 48 12 0f fb ca 3b 8e 71 2f 3b fe 02 9b 86 fd 13 47 1e df fe 22 2f 61 2f ff 69 90 ff a0 ce df ff 3d 2d 33 d8 c1 ed 36 8e 61 2f 3b (57 bytes). You can represent this as UTF-8 characters, but it isn't pretty, so Jelly uses its own code page to make it more "readable" \$\endgroup\$ – caird coinheringaahing Jun 1 at 18:56
  • 4
    \$\begingroup\$ @cairdcoinheringaahing Ahhhhh. Thank you! That makes a lot of sense. I thought I had lost my mind. Like "how are all these programmers unaware that unicode is 2 bytes per char?". haha. \$\endgroup\$ – Tim Jun 1 at 19:24
  • \$\begingroup\$ (@Tim Unicode is not 2 bytes per character though.) \$\endgroup\$ – Anders Kaseorg Jun 8 at 22:30
4
\$\begingroup\$

JavaScript (Browser Console), 155 bytes

alert(`${c='https://codegolf.stackexchange.com/'}a/${(await(await fetch(c+`search?q=user:${u=44718}+inquestion:226635`)).text()).match(/(?<=s#)\d+/)}/${u}`)

I'm using Firefox 88. The code need to be executed under same domain due to permission restriction of fetch.

Btw, top level await is allowed in most browsers' console.

\$\endgroup\$
10
  • 1
    \$\begingroup\$ <s>I'm not sure why this may fail as search result cannot find out this post...</s> OK, finally get to work, seems search result has some latency... \$\endgroup\$ – tsh Jun 1 at 11:02
  • \$\begingroup\$ remove last ${u} and add a +u ad the end. saves 2 bytes. \$\endgroup\$ – EliteDaMyth Jun 1 at 11:16
  • \$\begingroup\$ You don't need the user ID in the final URL at all. \$\endgroup\$ – Shaggy Jun 1 at 11:18
  • \$\begingroup\$ 143 bytes \$\endgroup\$ – EliteDaMyth Jun 1 at 11:21
  • \$\begingroup\$ @EliteDaMyth "missing ) after argument list?" \$\endgroup\$ – ophact Jun 1 at 11:27
2
+50
\$\begingroup\$

Red, 151 byte

r: :rejoin n:"104280"u: https://codegolf.stackexchange.com/ r[u"a/"parse read r[u"questions/226635"][collect[thru[{"}ahead[22 skip n]]keep to{"}]]"/"n]

No TIO link, since it uses an outdated toolchain and errors out during lexing. Try it locally instead, using the latest automated build.


Red's implementation of GZIP and DEFLATE decompression is completely broken, so, instead of working with StackExchange API, I had to fall back on HTML parsing... which is a nice showcase in its own regard, since the solution relies on the embedded PEG parsing DSL.

\$\endgroup\$
4
  • \$\begingroup\$ " you are not allowed to use any html parser library or built-in " Does this rely on a html parser library or is the parse DSL only used inside your program? \$\endgroup\$ – Wzl Jun 1 at 20:38
  • \$\begingroup\$ @Wzl, it's an embedded domain-specific language, not an HTML parsing library. I'm not quite sure what OP means by "built-in": technically, everything in the standard library is a built-in. \$\endgroup\$ – 9214 Jun 1 at 20:42
  • \$\begingroup\$ I understand that, I mean if there was any external anything specifically for parsing HTML that you used. It looks like not (so +1). \$\endgroup\$ – Wzl Jun 9 at 20:50
  • \$\begingroup\$ I'll amend that to +6 ;) \$\endgroup\$ – Wzl 2 days ago
2
\$\begingroup\$

PHP 5, 211 bytes

foreach(json_decode(gzdecode(join(file('http://api'.($s='.stackexchange.com/').'2.2/questions/226635/answers?site=codegolf'))))->items as$i)if($i->owner->user_id==90841)echo"https://codegolf{$s}a/$i->answer_id";

(Cannot) Try it online!

Unfortunately, file and gzdecode functions re disabled in PHP online testers, but it works so far on my localhost

EDIT: first version didn't work (because of the content of my own answer, lol) and was easy to break with a single comment, switched to a supposedly foolproof version with the API, much longer though

EDIT 2: saved 5 bytes with strings optimizations

EDIT 3: another byte saved (the dust) by using http instead of https for the API

\$\endgroup\$
1
\$\begingroup\$

Bash + Wget + Sed, 114 bytes (104 bytes + 5 byte user ID twice)

U=https://codegolf.stackexchange.com
wget $U/q/226635 -O-|sed -nE '/44694/s?.*answer-([0-9]+).*?'$U'/a/\1/44694?p'

Prints https://codegolf.stackexchange.com/a/226723/44694 on stdout.

Note that wget prints logs to stderr, which should be ignored.

(No TIO link because wget doesn't work on TIO; I assume TIO blocks internet access.)

Explanation:

  • Save the string https://codegolf.stackexchange.com in $U
  • wget the webpage for this question, piping it to sed
  • Using sed:
    • Search for all lines containing my user ID.
    • (Specifically we are looking for the line containing <div id="answer-XXXX" class="answer" data-answerid="XXXX" data-ownerid="YYYY" ..., where YYYY is my user ID, and XXXX is the answer ID we are looking for.)
    • If the line contains answer-XXXX, then
      • The XXXX is the answer ID; capture it in a subexpression.
      • Print the string saved in $U followed by /a/, followed by the answer ID using \1 subexpression replacement, followed by a slash and my user ID.

Bash + Wget + Sed, 100 bytes (95 bytes + 5 byte user ID)

If the output format is not as strict, then I have a 100 byte version that prints the URL without the https:// or the trailing user ID.

Prints codegolf.stackexchange.com/a/226723 on stdout.

U=codegolf.stackexchange.com
wget $U/q/226635 -O-|sed -nE '/44694/s?.*answer-([0-9]+).*?'$U'/a/\1?p'
  • -8 bytes: no https://
  • -(1+5) bytes: no trailing / and user ID
\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 216 bytes

fetch("https://api.stackexchange.com/2.2/questions/226635/answers?site=codegolf").then(r=>r.json()).then(j=>"https://codegolf.stackexchange.com/a/"+j.items.find(a=>a.owner.user_id==17602).answer_id).then(console.log)

\$\endgroup\$
4

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