28
pannenkoek2012 aims to complete Super Mario 64 with as few presses as possible of the A button, which makes Mario jump. Each "A press" consists of three parts:
- Pressing the button
- Holding it for any length of time
- Releasing it
See this video (1:15 - 3:23) for a great explanation that includes the above image. (However, this challenge will not use the half-A-press terminology and will posit obstacles that require releasing A.)
Task:
Given a sequence of obstacles requiring pressing (P), holding (H), or releasing (R) the A button, output the smallest number of presses required to overcome them in the order given. The A button is initially not held.
Stated formally: given a string S of characters PHR
, consider strings of form (PH*R)*
that contain S as a subsequence, and output the smallest possible number of P
's in such a string. Or, alternatively, find the smallest number of chunks of the form P?H*R?
that S can be split into.
Example
Let's look at input RHRPHHHR
. The A button starts not held, so overcoming the initial obstacle R
requires the button be pressed and then released (press #1). Next we are required to hold the button H
, which again requires it first be pressed (press #2). Then, it can then be released afterwards to satisfy the R
after it. Finally, the remaining PHHHR
can be satisfied by a single press (press #3) followed by holding HHH
and releasing R
. So, the output count is 3.
Another way to see it, is that we can split the input string into 3 parts of form PHH..HHR
where letters may be omitted.
R
HR
PHHHR
Input format
The input will be a list or string of elements representing press, hold, and release as your choice of:
P, H, R
p, h, r
1, 2, 3
0, 1, 2
matched in the order given. The input will not be empty.
Test cases:
P 1
H 1
R 1
HP 2
RHP 3
HHR 1
PHRH 2
RHRPHHHR 3
HHHHHH 1
PPRRHHPP 6
HPPRHRPRHPPRHPPHRP 12
PRHRHPHHPRRRHPPRHHPPRRRHRHPRPHPRPRHHRPPPRHPRP 28
Leaderboard:
var QUESTION_ID=152701,OVERRIDE_USER=20260;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/152701/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<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="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><div id="language-list"> <h2>Winners 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><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>
1What about obstacles requiring the A button to not be held? There are four button states in the graph (I think that these might actually exist in the game, too) – Random832 – 2018-01-07T05:16:27.223
3In reality, there are 3 states: Press, Held, and Not-held. No state require an A button Release. The challenge is slightly wrong in comparison with the reality. – user202729 – 2018-01-07T07:26:17.870
@Random832 This challenge is only based on reality, not an exact replicate. – user202729 – 2018-01-07T07:26:45.390
@user202729 Makes sense, although I think an obstacle requiring an A button release is conceivable: consider an obstacle like a tunnel sloped downwards, which would require Mario to start off at top jump height and then release A at the exact right moment so he falls diagonally through the tunnel. Probable? No. – 11684 – 2018-01-07T15:41:44.463
1@11684 "as for the release, well, there is currently no cases where that's useful or important so don't worry about that part." (1:48 - 1:52) – user202729 – 2018-01-07T15:44:26.647
3Anyone want to do this in MIPS assembly? (the language used to program Super Mario 64) – user202729 – 2018-01-07T15:45:04.477
1@user202729 Wow, that’s one thorough pancake. Thanks! – 11684 – 2018-01-07T15:45:06.027
@user202729 it's been 1.5 years since my last MIPS answer and I completely forget how to do string processing. – qwr – 2018-01-07T16:36:39.913
I'm just curious: did you find the video because of the recent Twitter/@SwiftOnSecurity discussion about collision in physics engines? EDIT: this thread
– No don't shown my real name – 2018-01-07T23:27:46.3171@user202729 I'm aware, I said the challenge "will posit obstacles that require releasing A" because I found it too be too simple without them. – xnor – 2018-01-08T00:36:33.030
@Nodon'tshownmyrealname No, I've been watching pannenkoek's videos for a while. – xnor – 2018-01-08T00:37:36.933
Debouncing needs to be caught? – tuskiomi – 2018-01-13T21:04:25.100