24
\$\begingroup\$

When using a tool such as git to merge two files, a conflict could be detected and added to the result of the merge.

A merge of these two files:

my file:

Common line of code 1
Common line of code 2
my lines
Common line of code 3
Common line of code 4

their file:

Common line of code 1
Common line of code 2
their lines
Common line of code 3
Common line of code 4

would result in:

Common line of code 1
Common line of code 2
<<<<<<< Mine
my lines
=======
their lines
>>>>>>> Theirs
Common line of code 3
Common line of code 4

See Conflict Marker Lines

Resolving this conflict with Mine would create this file:

Common line of code 1
Common line of code 2
my lines
Common line of code 3
Common line of code 4

Resolving this conflict with Theirs would create this file:

Common line of code 1
Common line of code 2
their lines
Common line of code 3
Common line of code 4

The objective of this challenge is to write a source file that contains a conflict and still compiles/executes.

Write a source file which:

  1. contains one valid, two-way, conflict marked by the proper patch conflict markers (<<<<<<<, =======, >>>>>>>) Mine and Theirs file descriptors after the markers are optional.
  2. compiles/executes without errors/warnings if the markers remain a part of the source
  3. compiles/executes without errors/warnings if the conflict is resolved by using mine
  4. compiles/executes without errors/warnings if the conflict is resolved by using theirs
  5. outputs "Hello Conflict" when compiling/executing the conflicted file
  6. outputs "Hello Mine" when compiling/executing the mine version
  7. outputs "Hello Theirs" when compiling/executing the theirs version

The markers should be located in the source file in such a way that kdiff3 recognizes the conflict.

Standard loopholes are forbidden.

The shortest code wins.

Score is the length of the conflicted source

\$\endgroup\$
  • \$\begingroup\$ Are function submissions allowed, or just full programs? \$\endgroup\$ – Jakob May 25 '18 at 17:36
  • \$\begingroup\$ @Jakob - it must be possible to execute/run it. So if there is a REPL that can execute the function (or just the expression), sure. \$\endgroup\$ – Erno de Weerd May 25 '18 at 17:41

16 Answers 16

2
\$\begingroup\$

Jelly, 51 bytes

<<<<<<<
“½&;»
“£<Ø»
=======

“8ẉI»
>>>>>>>
“¢5Ṛ»;2£

Try it online!

Explanation

The conflict markers here are positioned so that one of three lines becomes the second line of the program after the conflict is resolved; this will be a constant, named . The second line of the original program encodes the string " Conflict" (in Jelly's compressed notation); the third line encodes the string " Mine" (this will become the second line if the conflict is resolved as mine); the sixth line encodes the string " Theirs" (and will become the second line if the conflict is resolved as theirs).

The main program is always the last line, no matter how many lines before it are deleted. It takes the compressed encoding of "Hello", and appends (;) the value of to it, thus producing the desired output.

\$\endgroup\$
13
\$\begingroup\$

JavaScript (ES6), 102 94 93 90 bytes

console.log('Hello',(a=`
<<<<<<<
Mine
=======
Theirs
>>>>>>>
Conflict`.split`
`)[6]||a[1])

If the conflict has been resolved, then there is no sixth line, so it prints the now first line instead. Edit: Saved 3 bytes thanks to @nderscore.

\$\endgroup\$
  • \$\begingroup\$ Creative use of regular expressions! \$\endgroup\$ – Erno de Weerd Feb 23 '17 at 9:08
  • \$\begingroup\$ @ErnodeWeerd Oops, I just changed it... \$\endgroup\$ – Neil Feb 23 '17 at 9:08
  • \$\begingroup\$ :) no worries, I was happy to see it \$\endgroup\$ – Erno de Weerd Feb 23 '17 at 9:10
  • \$\begingroup\$ -3 bytes: console.log('Hello',(a=`...`)[6]||a[1]) \$\endgroup\$ – nderscore Feb 23 '17 at 19:12
10
\$\begingroup\$

Brachylog, 68 67 66 bytes

"Hello "wċ₂↰₁w∨"Conflict"w
<<<<<<<
"Mine"
=======
"Theirs"
>>>>>>>

Try it online!

Try the "Hello Mine" version here

Try the "Hello Theirs" version here

Explanation

Thankfully, <<<<<<<, ======= and >>>>>>> are all valid rule definitions in Brachylog. They respectively mean:

  • Input is less than an implicit varible, itself less than..., etc., itself less than the output.
  • All elements of the input are equal, and all elements of the input are equal, and..., and Input = Output
  • Same as the first but greater than instead.

If we remove conflicts, we end up with "Mine" or "Theirs" on the second line, which means they become predicate number 1. Calling that predicate with ↰₁ on the first line will unify its input and output with Mine / Theirs, which we then print with w.

If we call ↰₁ on the conflicted file, we end up calling <<<<<<<. We therefore call that predicate with a string as input (using ċ₂ - coerce to string). < will fail with a string as input. We then put a disjunction ∨"Conflict"w in the main predicate which states that if predicate 1 fails, then we print Conflict instead. ↰₁ with a string as input won't fail for the "Mine" or "Theirs" lines because they are strings.

\$\endgroup\$
9
\$\begingroup\$

PHP, 74 65 bytes

Note: uses IBM-850 encoding

Hello<?='
<<<<<<<
2:<?PU_~
=======
+;73"&_~
>>>>>>>
'^~ıǼ¡Ñ»¬áü;

Store to a file and run like this:

php -nf conflict.php

Explanation

Hello       # Print "Hello"
<?='        # Print result of expression
<<<<<<<     # String with merge conflict
2:<?PU_~
=======
+;73"&_~
>>>>>>>
'
^           # XOR that string with...
~ıǼ¡Ñ»¬áü; # ... this string, negated.

The binary XOR results in either of the following 3:

'
<<<<<<<
' ^ ~'ıǼ¡Ñ»¬áü'
==> ' Conflict' 
--------------------------------------------------------------------------
'
2:<?PU_~' ^ ~'ıǼ¡Ñ»¬áü'
==> ' Mine' (right padded with nul bytes)
--------------------------------------------------------------------------
'
+;73"&_~' ^ ~'ıǼ¡Ñ»¬áü'
==> ' Theirs' (right padded with nul bytes)

Tweaks

  • Saved 9 bytes by using binary logic on strings
\$\endgroup\$
  • \$\begingroup\$ Someday PHP will have an ======= operator, and then this'll be a bit easier. \$\endgroup\$ – Jakob May 25 '18 at 17:27
7
\$\begingroup\$

Pip, 61 bytes

"Hello ".("Conflict
<<<<<<<
Mine
=======
Theirs
>>>>>>>
"^n7)

Try it online!

Everything between "" is a string. We split the large string on newlines ("..."^n) and take the 7th element with cyclical indexing ((___7)). For the conflicted version, there are seven lines, so index 7 is equivalent to index 0 and we get Conflict. For the resolved versions, there are three lines, so index 7 is equivalent to index 1 and we get Mine/Theirs. Then concatenate "Hello " to the front and autoprint.

\$\endgroup\$
  • \$\begingroup\$ Very creative, I like the cyclical indexing. \$\endgroup\$ – Erno de Weerd Feb 23 '17 at 11:25
6
\$\begingroup\$

Batch, 133 129 bytes

@set s=Theirs
@goto t
<<<<<<<
:t
@set s=Mine
@goto m
=======
:m
@set s=Conflict
@goto t
>>>>>>>
:t
:m
echo Hello %s%

Explanation: The goto statement goes to the next label it can find. In the case of the conflict, this just ends up skipping the conflict markers, and s gets its final value. In the case of resolving with Mine, the gotos have no effect, but the last set no longer exists, so the result is Mine. In the case of resolving with Theirs the inital goto bypasses the remaining set so the result is its initial value. Edit: Saved 4 bytes thanks to @DLosc.

\$\endgroup\$
  • \$\begingroup\$ Nice! Can you remove :c and do @goto t instead? \$\endgroup\$ – DLosc Feb 23 '17 at 10:51
  • \$\begingroup\$ @DLosc Oh, so Batch goes to the next label, rather than the first? Neat! \$\endgroup\$ – Neil Feb 23 '17 at 13:58
  • \$\begingroup\$ I don't know--I haven't tested it. I might be completely wrong. \$\endgroup\$ – DLosc Feb 23 '17 at 20:17
4
\$\begingroup\$

Python 2, 88 87 bytes

print 'Hello','''
<<<<<<<
Mine
=======
Theirs
>>>>>>>
Conflict'''.split('\n')[1::5][-1]

Prints the sixth or (now) first line as appropriate.

\$\endgroup\$
  • 1
    \$\begingroup\$ It doesn't print the "Hello " part... \$\endgroup\$ – Erno de Weerd Feb 23 '17 at 9:15
  • \$\begingroup\$ @ErnodeWeerd Sorry, I'd made that mistake in my other answer too, and forgotten to fix this one. \$\endgroup\$ – Neil Feb 23 '17 at 10:04
  • \$\begingroup\$ Is there a space between Hello and whatever follows? \$\endgroup\$ – Erno de Weerd Feb 24 '17 at 10:00
  • \$\begingroup\$ @ErnodeWeerd The Python 2 print statement always uses a space as separator (and a trailing newline; Python 3's print function allows you to choose the separator and terminator). \$\endgroup\$ – Neil Feb 24 '17 at 10:14
3
\$\begingroup\$

.COM opcode, 77 bytes

0000h: B4 09 BA 17 01 CD 21 BA 1F 01 80 3E 1F 01 3C 75 ; ......!....>..<u
0010h: 03 BA 44 01 CD 21 C3 48 65 6C 6C 6F 20 24 0A 3C ; ..D..!.Hello $.<
0020h: 3C 3C 3C 3C 3C 3C 0A 4D 69 6E 65 24 0A 3D 3D 3D ; <<<<<<.Mine$.===
0030h: 3D 3D 3D 3D 0A 54 68 65 69 72 24 0A 3E 3E 3E 3E ; ====.Their$.>>>>
0040h: 3E 3E 3E 0A 43 6F 6E 66 6C 69 63 74 24          ; >>>.Conflict$

        org 100h
        mov ah, 9
        mov dx, str1
        int 21H
        mov dx, str2
        cmp [str2], byte '<'
        jne $+5
        mov dx, str3
        int 21H
        ret

str1    db 'Hello $', 10
str2    db '<<<<<<<', 10
        db 'Mine$', 10
        db '=======', 10
        db 'Their$', 10
        db '>>>>>>>', 10
str3    db 'Conflict$'

If a space after <<<<<<< allowed, 75 bytes

0000h: B4 09 BA 0D 01 CD 21 BA 1E 01 CD 21 C3 48 65 6C ; ......!....!.Hel
0010h: 6C 6F 20 24 0A 3C 3C 3C 3C 3C 3C 3C 20 0A 43 6F ; lo $.<<<<<<< .Co
0020h: 6E 66 6C 69 63 74 24 4D 69 6E 65 24 0A 3D 3D 3D ; nflict$Mine$.===
0030h: 3D 3D 3D 3D 0A 2A 2A 2A 2A 2A 2A 2A 2A 54 68 65 ; ====.********The
0040h: 69 72 24 0A 3E 3E 3E 3E 3E 3E 3E                ; ir$.>>>>>>>

        org 100h
        mov ah, 9
        mov dx, str1
        int 21H
        mov dx, str2
        int 21H
        ret

str1    db 'Hello $', 10
        db '<<<<<<< ', 10
str2    db 'Conflict$'
        db 'Mine$', 10
        db '=======', 10
        db '********Their$', 10
        db '>>>>>>>'     
\$\endgroup\$
  • \$\begingroup\$ Nice one! Would you be able to add the assembly version(s), I would enjoy being able to read the instructions :). Yes, space appears to be allowed (the specs aren't as clear as I would like them to be) \$\endgroup\$ – Erno de Weerd May 27 '18 at 9:37
2
\$\begingroup\$

Retina, 57 bytes


Hello 
$
<<<<<<<
Mine
=======
Theirs
>>>>>>>
<+
Conflict

Try it online!

Try the "Mine" version

Try the "Theirs" version

Explanation of Conflict program

As is often the case with Retina, this program contains many replace stages.


Hello 

Replace the empty/non-existent input with Hello .

$
<<<<<<<

Replace the end of the working string with <<<<<<<

Mine
=======

Replace Mine with =======. Since Mine doesn't appear anywhere in the working string, this does nothing.

Theirs
>>>>>>>

Replace Theirs with >>>>>>>. Same deal as with Mine; Theirs doesn't appear, so the replacement does nothing.

<+
Conflict

Replace a sequence of < with Conflict. We added <<<<<<< to the end of the string on the first replacement, so the working string becomes Hello Conflict, which is implicitly output at the end of the program.

Explanation of Mine/Theirs programs

When the conflict is resolved, the code will look like this:


Hello 
$
Mine
<+
Conflict

Hello 

Same as before, starting with the string Hello.

$
Mine

Now instead of appending <<<<<<< to Hello , we append Mine.

<+
Conflict

Next, we replace a sequence of < with Conflict. But there are no <s in the string, so nothing happens.

The working string, Hello Mine, is implicitly output. The "Theirs" program works the same way.

\$\endgroup\$
1
\$\begingroup\$

OIL, 88 80 77 bytes

4
2
Hello 
10
Conflict
16
16
8
4
11
3
<<<<<<<
Mine
=======
Theirs
>>>>>>>
4
4

4 2 is printing line 2 (Hello), 10 tests whether line 0 (4) is identical with line 16 (the one that contains a 4 when the conflict exists) and jumps to either line 16 or line 8 depending on the result. If it existed, 4 4 prints line 4 (Conflict). If it didn't, 4 11 prints either Mine or Theirs, depending on what was merged, and 3 exits.

\$\endgroup\$
1
\$\begingroup\$

Java 145 Bytes

()->{String s = "Hello ";/*
<<<<<<<
*/s+="Mine";/*
=======
*/s+="Theirs";/*
>>>>>>>
*/if(s.length()>15)s="Hello Conflict";System.out.println(s);}

Java has no multiline strings so some Comment trickery was needed

\$\endgroup\$
  • \$\begingroup\$ What is the reason for the extra * on the third line? I would think that just */s+="Mine";/* would work. \$\endgroup\$ – CAD97 Feb 24 '17 at 14:01
  • \$\begingroup\$ leftover from when comment lines were separate lines to avoid identical lines(refactored before posting and forgot that), thx for that hint and 3 bytes \$\endgroup\$ – masterX244 Feb 24 '17 at 14:02
1
\$\begingroup\$

Bash, 76 bytes

Golfed

sed 's/^/Hello /;s/<\+/Conflict/;q'<<E
<<<<<<<
Mine
=======
Theirs
>>>>>>>
E

How it works

Uses here doc, to feed the source text to sed.

Sed will prepend the first line it read with "Hello ", replace the <<<<<<< string by "Conflict" and then quit (q).

Try It Online !

\$\endgroup\$
1
\$\begingroup\$

ES6 (Javascript), 83, 82 bytes

Golfed

alert("Hello "+((T=`\
<<<<<<<
Mine
=======
Theirs
>>>>>>>
`)[1]>"<"?T:"Conflict"))

Try It

alert("Hello "+((T=`\
<<<<<<<
Mine
=======
Theirs
>>>>>>>
`)[1]>"<"?T:"Conflict"))

alert("Hello "+((T=`\
Mine
`)[1]>"<"?T:"Conflict"))

alert("Hello "+((T=`\
Theirs
`)[1]>"<"?T:"Conflict"))

\$\endgroup\$
1
\$\begingroup\$

Java 8, 108 bytes

This is a lambda accepting empty input and returning nothing.

n->System.out.print("Hello "+",Mine,Theirs,Conflict".split(",")[/*
<<<<<<<
*/1/*
=======
*/+2/*
>>>>>>>
*/])

Try It Online

Approach inspired by masterX244's Java solution.

\$\endgroup\$
1
\$\begingroup\$

C (GCC), 110 bytes

Function submission.

f(){char*s[]={0,"Mine","Theirs","Conflict"};printf("Hello %s",s[/*
<<<<<<<
*/1/*
=======
*/+2/*
>>>>>>>
*/]);}

Approach inspired by masterX244's Java solution.

TIOs

\$\endgroup\$
  • \$\begingroup\$ Nice one! Smart use of the multi-line comments. \$\endgroup\$ – Erno de Weerd May 25 '18 at 19:39
0
\$\begingroup\$

Perl 5, 68 bytes

Updated after realising that the version with 6 of each delimiter works as intended, but with the actual 7 delimiters, it doesn't... Oops...

say"Hello ",q
<<<<<<<
!Mine!;0
=======
!Theirs!;0
>>>>>>>
&&Conflict

Try the conflict online!

Try mine online!

Try itheirs online!

\$\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.