13
\$\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
  • 6
    \$\begingroup\$ BAN URL SHORTENERS before anyone has a 'clever idea'. \$\endgroup\$
    – emanresu A
    Jun 1, 2021 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\$ Jun 1, 2021 at 10:28
  • \$\begingroup\$ "taking the input that returns the link to your answer"? Do you mean "taking no input"? \$\endgroup\$
    – tsh
    Jun 1, 2021 at 10:32
  • 2
    \$\begingroup\$ May we hard-code our user ID (that goes on the end of the link)? \$\endgroup\$
    – pxeger
    Jun 1, 2021 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, 2021 at 16:25

8 Answers 8

15
\$\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\$
6
  • \$\begingroup\$ 57 unicode characters != 57 bytes \$\endgroup\$
    – Tim
    Jun 1, 2021 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\$ Jun 1, 2021 at 18:56
  • 5
    \$\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, 2021 at 19:24
  • \$\begingroup\$ (@Tim Unicode is not 2 bytes per character though.) \$\endgroup\$ Jun 8, 2021 at 22:30
  • \$\begingroup\$ Fails for me in TIO \$\endgroup\$
    – Seggan
    Jan 31, 2022 at 16:30
6
\$\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, 2021 at 11:02
  • 1
    \$\begingroup\$ remove last ${u} and add a +u ad the end. saves 2 bytes. \$\endgroup\$
    – user100752
    Jun 1, 2021 at 11:16
  • \$\begingroup\$ You don't need the user ID in the final URL at all. \$\endgroup\$
    – Shaggy
    Jun 1, 2021 at 11:18
  • \$\begingroup\$ 143 bytes \$\endgroup\$
    – user100752
    Jun 1, 2021 at 11:21
  • \$\begingroup\$ @EliteDaMyth "missing ) after argument list?" \$\endgroup\$
    – user100690
    Jun 1, 2021 at 11:27
5
+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\$
    – Wezl
    Jun 1, 2021 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, 2021 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\$
    – Wezl
    Jun 9, 2021 at 20:50
  • \$\begingroup\$ I'll amend that to +6 ;) \$\endgroup\$
    – Wezl
    Jun 10, 2021 at 15:07
4
\$\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\$
3
\$\begingroup\$

Go, 342 bytes

package main
import(."encoding/json"
."net/http")
func main(){t,u:="codegolf",".stackexchange.com/"
r,_:=Get("https://api"+u+"2.2/questions/226635/answers?site="+t)
var s struct{Items[]struct{Answer_ID int
Owner struct{User_ID int}}}
NewDecoder(r.Body).Decode(&s)
for _,i:=range s.Items{if i.Owner.User_ID==7815{print(t,u,"a/",i.Answer_ID)}}}
\$\endgroup\$
2
\$\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\$
1
\$\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
1
\$\begingroup\$

05AB1E, 60 bytes

’ƒËŠˆ.‚‹º.ŒŒ’žYì©…/q/•3{Ä•J.w“"/a/“¡ʒ'ʒå}`'=¡4(èþ'a®rŽÍ¿'/ý

Port of @cairdCoinheringaahing's Jelly answer, so make sure to upvote him.
Assumes this is the only answer that contains a ʒ. If another answer should pop up with this character, it could be switched to another unique character from my program that probably isn't used in a lot of languages instead, like þ or ®.

No TIO of the entire program, since .w is disabled.

Explanation:

Generate the question URL, and get its HTML content:

’ƒËŠˆ.‚‹º.ŒŒ’     # Push dictionary string "codegolf.stackexchange.com"
  žYì              # Prepend "https://"
     ©             # Store this string in variable `®` (without popping)
      …/q/         # Push string "/q/"
          •3{Ä•    # Push compressed integer 226635
               J   # Join the entire stack together
                .w # Browse to this URL and get its HTML content as string

Try it online (without .w).

And then get the answer-ID and generate the resulting URL:

“"/a/“¡           "# Split the HTML content on '"/a/'
       ʒ           # Filter these parts by:
        'ʒå       '#  Check if it contains character "ʒ"
       }`          # After the filter: pop and push this only part to the stack
         '=¡      '# Split it on "="
            4(è    # Index to get the 0-based -4'th item (fourth item from the back)
               þ   # Only leave its digits
'a                '# Push string "a"
  ®                # Push the string from variable `®`
   r               # Reverse the three values on the stack
    ŽÍ¿            # Push compressed integer 52210
       '/ý        '# Join the stack with "/" delimiter
                   # (after which the result is output implicitly)

The URL generated by TIO would be too big for a stackexchange answer and most URL-shorteners unfortunately, so here the TIO where you'll have to insert the question's HTML content yourself (Ctrl+U in your browser, select everything, and past it between the """ in the input-block of the TIO):
'Try it online.'

See this 05AB1E tip of mine (sections How to use the dictionary? and How to compress large integers?) to understand how ’ƒËŠˆ.‚‹º.ŒŒ’ is "codegolf.stackexchange.com"; •3{Ä• is 226635; and ŽÍ¿ is 52210.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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