Cody

# Problem 44351. Code breaker, Part I: Operation Phoenix

Solution 1278826

Submitted on 29 Sep 2017 by yurenchu
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
% This test suite can be updated if inappropriate 'hacks' % are discovered in any submitted solutions, % so your score/size may therefore change over time.

2   Pass
x = 'Dbodfm Pqfsbujpo Qipfojy jnnfejbufmz & ftdbqf.'; s_correct.shift = 1; s_correct.message = 'Cancel Operation Phoenix immediately & escape.'; s = decode(x); assert( isequal(s.shift, s_correct.shift) ) assert( isequal(s.message, s_correct.message) ) assert( isequal(class(s.shift), 'uint8') ) assert( isequal(class(s.message), 'char') )

3   Pass
x = 'Vwlyhapvu Wovlupe ilnpuz Ablzkhf! Vwlyhapvu Wovlupe pz vby avw wypvypaf.'; s_correct.shift = 7; s_correct.message = 'Operation Phoenix begins Tuesday! Operation Phoenix is our top priority.'; s = decode(x); assert( isequal(s.shift, s_correct.shift) ) assert( isequal(s.message, s_correct.message) ) assert( isequal(class(s.shift), 'uint8') ) assert( isequal(class(s.message), 'char') )

4   Pass
x = 'Eatpht cdit iwpi iwt ephhldgs wph qttc rwpcvts ugdb "Fxeudyn" id "Dvcsbwl". Diwtglxht Detgpixdc Ewdtcxm xh egdrttsxcv prrdgsxcv id eapc.'; s_correct.shift = 15; s_correct.message = 'Please note that the password has been changed from "Qipfojy" to "Ogndmhw". Otherwise Operation Phoenix is proceeding according to plan.'; s = decode(x); assert( isequal(s.shift, s_correct.shift) ) assert( isequal(s.message, s_correct.message) ) assert( isequal(class(s.shift), 'uint8') ) assert( isequal(class(s.message), 'char') )

5   Pass
x = 'Mncpyrgml Nfmclgv qryprcb rfpcc bywq yem. Yjj ncpqmllcj ypc rm pckygl ml bsrw slrgj Mncpyrgml Nfmclgv gq amknjcrcb.'; s_correct.shift = 24; s_correct.message = 'Operation Phoenix started three days ago. All personnel are to remain on duty until Operation Phoenix is completed.'; s = decode(x); assert( isequal(s.shift, s_correct.shift) ) assert( isequal(s.message, s_correct.message) ) assert( isequal(class(s.shift), 'uint8') ) assert( isequal(class(s.message), 'char') )

6   Pass
% Acknowledgements % Portions of this timing test code were inspired by: % (1) Problem 937. "Rubik's Mini Cube: Solve Randomized Cube in 11 Moves or Less; Score is by Time (msec)" by Richard Zapor. % (2) Problem 2733. "Evil Number" by Jan Orwat. % (3) Feedback in comments from Peng Liu. % (4) Problem Problem 1237. "It's race time! Write a faster function than the test suite call of unique()." by Jeremy. % Note: The Time Trial section does not check accuracy; that is done above. % Initialise x = 'Dbodfm Pqfsbujpo Qipfojy jnnfejbufmz & ftdbqf.'; cutoffTimeBig = 1000; % Maximum allowable walltime (in milliseconds) to run function in a loop with qBig iterations. for dummy = 1 : 20, disp(' . '); end; % Run once, untimed? includeOverheads = false; if includeOverheads, solution = decode( x ); end; % *** PRELIMINARY TIMING *** % In case the submitted function has a lot of text output, % get an estimate based on just a few iterations % Initialise qSmall = 50; qBig = 5000; t0 = clock; % Loop for i = 1 : qSmall solution = decode( x ); end; % Compute and display elapsed time. dt = etime(clock, t0) * 1000; disp(' -----=====----- ') fprintf('Your wall time to decode %u messages = %i msec.\n\r', qSmall, floor(dt)) fprintf('Your APPROXIMATE wall time to decode %u messages would be ~ %i msec.\n\r', qBig, ceil(dt * qBig / qSmall)) disp(' -----=====----- ') for dummy = 1 : 20, disp(' . '); end; % *** 'OFFICIAL' TIMING *** % Re-initialise timer t0 = clock; t0_cpu = cputime; % Loop for i = 1 : qBig solution = decode( x ); end; % Compute and display elapsed time. for dummy = 1 : 20, disp(' . '); end; disp(' -----=====|||||=====----- ') dt = etime(clock, t0) * 1000; fprintf('Your wall time to decode %u messages = %i msec.\n\r', qBig, floor(dt)) dt_cpu = (cputime - t0_cpu) * 1000; fprintf(' ( Your CPU time for this = %i msec. ) \n\r', floor(dt_cpu)) fDecode = @() decode(x); dt_timeit = timeit( fDecode ) * 1000; fprintf(' [ Your ''timeit'' time to decode %u messages = %i msec. ] \n\r', qBig, ceil(dt_timeit * qBig)) % Display a size-based score. all_nodes = mtree('decode.m', '-file'); % This is the default in Cody. str_nodes = mtfind(all_nodes, 'Kind', 'STRING'); eq_nodes = mtfind(all_nodes, 'Kind', 'EQUALS'); print_nodes = mtfind(all_nodes, 'Kind', 'PRINT'); expr_nodes = mtfind(all_nodes, 'Kind', 'EXPR'); size_score = count(all_nodes) ... +sum(str_nodes.nodesize-1) ... +2*(count(expr_nodes) ... +count(print_nodes) ... -count(eq_nodes)); fprintf('Your size-based score = %i.\n\r', size_score) % Report revised performance score. combinedScore = size_score + min( 200, floor(dt*(500/qBig)) ); fprintf('Your combined score = %i.\n\r', combinedScore) disp(' -----=====|||||=====----- ') % Now disallow any candidate solutions that are TOO SLOW! if dt > cutoffTimeBig, fprintf('Sorry, your submission is TOO SLOW. It must be able to finish within %u milliseconds.\n\r', cutoffTimeBig) end; assert( dt <= cutoffTimeBig, 'Exceeded time limit specified in Test Suite.' )

. . . . . . . . . . . . . . . . . . . . -----=====----- Your wall time to decode 50 messages = 9 msec. Your APPROXIMATE wall time to decode 5000 messages would be ~ 970 msec. -----=====----- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . -----=====|||||=====----- Your wall time to decode 5000 messages = 824 msec. ( Your CPU time for this = 1200 msec. ) [ Your 'timeit' time to decode 5000 messages = 743 msec. ] Your size-based score = 39. Your combined score = 121. -----=====|||||=====-----