24
5
Description
Imaginary programming language (IPL) uses Polish Reverse Notation. It has the following commands:
- i -- input number and push it to the stack
- o -- non-destructive output top of the stack (number stays on the stack)
- d -- discard top of stack
- integer number -- push this number to the stack
- +-* -- pop two numbers from the stack, perform corresponding operation and push back the result. There is no division in IPL.
IPL works only with integers and is used for simple calculations. An IPL program is written on one line and separated by spaces. Empty string is a valid IPL program.
IPL Program:
i i + o
Inputs two numbers, adds them together and outputs the result.
Input numbers and integers that can be pushed to stack are in range [-999, 999], however output can be any. If your language do not support big numbers it is okay though.
Input/output format
You may choose any input/output format as long as it clear to understand and read/write: string, list, tokens etc.
Task
You are given some IPL program, you need to optimize it (reduce length):
i 12 + 3 + o d 2 3 + d
After optimization will become
i 15 + o
You do not have to preserve stack state, but amount of inputs and outputs and their order should match for the original and optimized program.
So IPL program:
-40 i * 2 * o i + 3 1 + o i 2 *
After optimisation will become
i -80 * o i 4 o i
or
-80 i * o i 4 o i
(note that you have to save all inputs, even if they are irrelevant).
There should be no hardcoding for test cases, code should work on any arbitrary IPL program and produce shortest possible IPL program that meets the requirements.
Scoring
Default code-golf scoring.
UPDATE: changed scoring to pure code golf scoring, as per @Sanchises suggestion.
Test cases:
Input:
(empty string)
Possible output:
(empty string)
Input:
i 4 * 2 + 3 * 6 - o
Possible output:
i 12 * o
Input:
1 1 + o
Possible output:
2 o
Input:
i 2 + 3 + o d 2 3 + d
Possible output:
i 5 + o
Input:
-40 i * 2 * o i + 3 1 + o i 2 *
Possible output:
-80 i * o i 4 o i
Input:
i i 1 + i 1 + i 1 + i 1 + d d d d o
Possible output:
i i i i i d d d d o
Input:
i i i 0 * * * o
Possible output:
i i i 0 o
Input:
i i i 1 * * * o
Possible output:
i i i * * o
Input:
i 222 + i 222 - + o
Possible output:
i i + o
Input:
i 2 + 3 * 2 + 3 * 2 + 3 * i * d i 2 + 3 * i + d i o 2 + 2 - 0 * 1 o
Possible output:
i i i i i o 1 o
Input:
i 1 + 2 * 1 + o
Possible output:
i 2 * 3 + o
Input:
1 1 + o i 2 + 3 + o d 2 3 + d 4 i * 2 * o i + 3 1 + o i 2 * i i 1 + i 1 + i 1 + i 1 + d d d d o i i i 0 * * * o i i i 1 * * * o i 2 + i 2 - + o i 2 + 3 * 2 + 3 * 2 + 3 * i * d i 2 + 3 * i + d i o 2 + 2 - 0 * 1 o
Possible output:
2 o i 5 + o 8 i * o i 4 o i i i i i i d d d d o i i i 0 o i i i * * * o i i + o i i i i i o 1 o
1A question: can you simplify
i i d o
toi o i
(the input is in order and the output is in order) or should you not simplify it? (the set of input and outputs should be in order) – Sanchises – 2018-08-01T12:01:03.2071@Sanchises no, inputs and outputs should be in order. If original program inputs 2 numbers before outputing anything optimised should do the same. – Андрей Ломакин – 2018-08-01T12:03:02.973
1Welcome to PPCG! Nice first Challenge! – Luis felipe De jesus Munoz – 2018-08-01T13:49:49.993
1May there be
d
oro
when stack is empty? – user202729 – 2018-08-02T02:03:56.1131Should we reduce number of tokens or number of bytes in the IPL program? – user202729 – 2018-08-02T02:09:56.377
1@user202729 no, input code is always valid – Андрей Ломакин – 2018-08-02T07:13:38.583
1@user202729 number of tokens – Андрей Ломакин – 2018-08-02T07:14:01.987
i i - o cannot be optimized to i i 0 o cause inputs can be different, so there is could be any output not only zero – Андрей Ломакин – 2018-08-02T07:15:06.703
@АндрейЛомакин, yes, sorry, that optimization would be questionable. Still, if the code cannot be shortened, is it valid to alter it without altering its functionality, i.e. optimizing
i 2 * o
to2 i * o
? – Jonathan Frech – 2018-08-02T12:29:26.887@JonathanFrech Ithink it is fine to reorder as long as it remains optimal. And I added test case, thanks. – Андрей Ломакин – 2018-08-02T13:34:37.527
6
From review queue, I do not think this challenge is unclear. If you do, please comment about why.
– mbomb007 – 2018-08-02T14:17:11.323@mbomb007 I'm certainly not sure what is meant by "no hardcoding cases". – Post Rock Garf Hunter – 2018-08-02T19:27:29.930
2@WW I think the OP means that you should not hardcode only the test cases listed in the question. You have to support arbitrary input. There should be no hardcoding for test cases, code should work *on any arbitrary IPL program* – mbomb007 – 2018-08-02T20:51:30.190