8
\$\begingroup\$

Normally, I'd open with some flavor text here about being bored and/or lazy but looking through the questions that would be far from original. So instead I'm going to open with a meta and self-referential flavor text describing this lack of originality that then describes itself and how meta and self-referential it is.

Challenge

Write a program that takes in the ID for some question on PPCG, and uses the SE API to grab the answers and select the winner.

You may assume that it is a question, and the following answer format:

# Language, [<s>XXX</s>] YYY bytes

[Thanks to @user1 for saving XX bytes!]
[Thanks to @user2 for saving YY bytes!]

print "badly formatted but easier to tally like this"

To clarify:

  • It might have 1 or more attributions or crossed out past scores, and it might not.
  • The attributions and crossed-out scores are not necessarily correlated; one can appear without the other.
  • If there are no attributions, then the code will start 2 newlines after the heading.

Anyway, you can either extract the length in the beginning or count the length of the code at the end. For the former, only use the last score. For the latter, don't count the attributions. You can assume that the two will match up.

Your program should spit out the ID for the answer with the fewest bytes. Your program should also do this in the fewest bytes possible, as the shortest answer in bytes will win.

Note: yes, I have seen Write code to determine the accepted answer, but that challenge has submissions voting on the winner whereas this is code golf with a specific task and an explicit winner independent of other answers.

\$\endgroup\$
6
  • \$\begingroup\$ @Mr.Xcoder if there aren't any answers, then you can return falsey or error. \$\endgroup\$
    – Nissa
    Dec 15, 2017 at 15:10
  • \$\begingroup\$ Sandbox. \$\endgroup\$
    – DELETE_ME
    Dec 15, 2017 at 15:12
  • 4
    \$\begingroup\$ Can you please add some test cases (as pointed out in the sandbox, a comment you might have chosen to ignore) --- Might be somewhat hard, because nearly no answers use that format, given that Try it online! or Explanation... are usually among the last lines? \$\endgroup\$
    – Mr. Xcoder
    Dec 15, 2017 at 15:13
  • \$\begingroup\$ @Mr.Xcoder and they're formatted. Actually, I'd suggest just replacing the request with STDIN on your TIO links. \$\endgroup\$
    – Nissa
    Dec 15, 2017 at 15:21
  • \$\begingroup\$ Will we have to handle bytes being a link? (codepage) \$\endgroup\$
    – user100690
    Jun 5, 2021 at 15:39

1 Answer 1

1
\$\begingroup\$

JavaScript, 283 bytes

n=>fetch(`https://api.stackexchange.com/2.2/questions/${n}/answers?site=codegolf&filter=!*SU8CGYZitCB.D*(BDVIficKj7nFMLLDij64nVID)N9aK3GmR9kT4IzT*5iO_1y3iZ)6W.G*`).then(r=>r.json()).then(s=>alert(s.items.sort((a,b)=>(g=s=>s.match(/\d+(?= bytes)/)[0])(a.body)-g(b.body))[0].answer_id))

Run on a subdomain of stackexchange.com.

\$\endgroup\$
1
  • \$\begingroup\$ You're allowed to run on api.stackexchange.com - there's a meta post somewhere. \$\endgroup\$
    – emanresu A
    Jul 5, 2021 at 20:10

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.