11
\$\begingroup\$

You end up having a lot of very long, boring-looking conditionals in your code:

if flag == 1:

while have != needed:

if type == 7:

These can be transformed into their much more lovable <3 conditionals counterparts:

if abs(flag - 1) + 2 <3:

while 3 - abs(have - needed) <3:

if 2 + abs(type - 7) <3:

Task

Your task is to take a conditional and make it in terms of <3. The only spacing that matters is that there is none between < and 3.

Conditionals will be two expressions seperated by either ==, !=, >, <, >= or <=.
Expressions will only contain addition, subtraction, unary negation (-something), where there is one + or - before each variables or numbers (except the first which has nothing or - before it).
Numbers will be [0-9]+, and variables will be [a-z]+. If the answer needs to use |x| (The absolute value of x), use the abs() function. You may assume that all variables are integers, and all number constants in the input are < 1000.

The output does not need to be in it's simplest form. It does need to be a conditional like above, meaning that it is two expressions only, seperated by one conditional sign, but it can also use the abs function, enclosing a valid expression, and then it acts like a variable, in terms of validity.

If the input does not have an output for any value of a variable, output a condition that is always false, but still in terms of <3.

Part of the challenge is figuring out how to do it, but here are the steps for the have != needed above:

have != needed
have - needed != 0
abs(have - needed) > 0
-abs(have - needed) < 0
3 - abs(have - needed) <3

Scoring

This is code-golf, so the shortest valid code, in bytes, wins.

Test cases

(Note, these outputs aren't the only outputs, but I tried to simplify them.)

flag == 1
abs(flag - 1) + 2 <3

have != needed
3 - abs(have - needed) <3

type == 7
2 + abs(type - 7) <3

x > y
3 - x + y <3

x + 5 < -y
x + 8 + y <3

x + 6 <= y
x + 8 - y <3

-x >= y + 3
x + y + 5 <3

x < x
3 <3
# Unsimplified; both would be valid outputs.
x - x + 3 <3
\$\endgroup\$
  • \$\begingroup\$ Is there always exactly one space between input tokens? \$\endgroup\$ – Doorknob Jun 5 '17 at 15:04
  • \$\begingroup\$ @Doorknob No. There will be 0 or 1 spaces. \$\endgroup\$ – Artyer Jun 5 '17 at 15:09
3
\$\begingroup\$

Retina, 95 bytes

<=
<1+
>=
>-1+
(.*)(.=)(.*)
$2abs($1-($3))
==
2+
!=
3-
(.*)>(.*)
$2<$1
(.*)<(.*)
$1-($2)+3
$
<3

Try it online!

A rather naïve solution, but I haven't been able to find any improvements.

This is just a series of replacements:

<=
<1+
>=
>-1+

Get rid of "or equal to" comparisons by replacing x <= y with x < 1 + y, and x >= y with x > -1 + y.

(.*)(.=)(.*)
$2abs($1-($3))

Replace x == y with ==abs(x - y), and x != y with !=abs(x - y).

==
2+
!=
3-

Replace == with 2+ and != with 3-, so that the overall replacements become x == y2 + abs(x - y) and x != y3 - abs(x - y).

(.*)>(.*)
$2<$1

Normalize the direction of the remaining inequalities, replacing x > y with y < x.

(.*)<(.*)
$1-($2)+3

Replace x < y with x - y + 3.

$
<3

Append a heart to the end of the string.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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