# Intersection of Two Lines

Given equation of two lines in the format Ax+By=C, determine their intersection point. If they are parallel print 'parallel' without quotes. For input/output format refer sample input/output.

``````Input
1x+1y=1
1x-1y=1

Output
(1.00,0.00)

Input
0x+1y=0
1x+0y=0

Output
(0.00,0.00)
``````
1. -1000000 <= A,B,C <= 1000000.
2. Note their intersection point may not be integer. Print upto two decimal places.
3. Shortest solution wins.
-
 Is a function call valid? example : f[1x+1y=1, 1x-1y=1] – belisarius Apr 17 '11 at 20:58 @belisarius Yes, but the solution should be entire program. – fR0DDY Apr 18 '11 at 5:59 Is the format always `Ax+By=C` or can it also be `Ax-By=C` or even `-Ax+By=C`? Are negative numbers for A, B or C even allowed? »Print up to two decimal places« implies that less can be printed, your examples do not suggest that. If the output is locale-dependent (which can happen with plenty languages in formatted output), can we assume `en-US` or `C` as locale? (I.e. anything that uses a decimal point and not a comma). – Joey Apr 18 '11 at 10:43 @Joey Yes A,B and C can be negative. `Ax-By=C` is already given in one of the examples. The points should have 2 decimal places. – fR0DDY Apr 18 '11 at 11:45 @Joey Edited the question to show that. Thanks for pointing that out. – fR0DDY Apr 18 '11 at 13:49

## Ruby - 119 chars

``````a,b,c,d,e,f=(gets+gets).scan(/-?\d+/).map &:to_f
puts (t=a*e-b*d)==0?'parallel':"(%.2f,%.2f)"%[(e*c-b*f)/t,(a*f-c*d)/t]
``````
-
 It's odd but my ruby 1.9.x shows `-0.00`, :-\ and you can use `gets(p)` instead of 2 `gets`, it will read the entire contents (assuming it was piped) – st0le Apr 16 '11 at 15:55

## Python, 148 146 chars

``````import re
I=raw_input
a,b,p,c,d,q=eval(re.sub('x|y=','.,',I()+','+I()))
D=a*d-b*c
print'(%.2f,%.2f)'%((p*d-q*b)/D,(a*p-c*q)/D)if D else'parallel'
``````
-

## sage - 162 chars

``````x,y=var('x,y')
e=lambda:eval(raw_input().replace('x','*x').replace('y','*y='))
try:r=solve([e(),e()],[x,y])[0];print`r[0].rhs(),r[1].rhs()`
except:print'parallel'
``````
-

## Python+sympy - 161 chars

``````from sympy import*
x,y=symbols('xy')
e=lambda:eval(raw_input().replace('x','*x').replace('y=','*y-'))
r=solve([e(),e()],[x,y])
print r and`r[x],r[y]`or'parallel'
``````

Python+sympy - 143 chars (different output format)

``````from sympy import*
x,y=symbols('xy')
e=lambda:eval(raw_input().replace('x','*x').replace('y=','*y-'))
print solve([e(),e()],[x,y])or'parallel'
``````

output format for 143 char version is slightly different from the spec

``````In:
1x+1y=1
1x-1y=1

Out:
{x: 1, y: 0}

In:
0x+1y=0
1x+0y=0

Out:
{x: 0, y: 0}
``````
-

## J, 146132134 124

``````echo'parallel'"_`(1|.')(',[:(,',',])&(0j2&":)/[:,,.@{:%.}:)@.([:*@|[:-/ .*}:)|:".>{.`(<@}:@;@(1 2&{))`{:(`:0)"1;:;._2(1!:1)3
``````

Parsing this is awful.

Edit: Realized that my output is basically broken, although it works for the examples given and parallels...

Edit: Fixed without too much pain...

Edit: Fixed a weird issue where the program was spread across multiple lines.

Somewhat ungolfed version:

``````words =: ;:;._2(1!:1)3
NB. Leverage J's parser itself to split each line into e.g. ax + by = c
lines =: ".>{.`(<@}:@;@(1 2&{))`{:(`:0)"1 words
NB. Use a 3-part gerund (`) to get boxed strings 'ax';'b';'c', unbox and parse
calc_and_format =: 1|.')(',[:(,',',])&(0j2&":)/[:,,.@{:%.}:
NB. %. (matrix multiply) first two rows (a d/b e) by third row turned (c/f)
NB. And then format laboriously. 0j2&": formats numbers with 2 decimals.
det_nonzero =: [:*@|[:-/ .*}:
NB. 1 if determinant (-/ .*) of (a b/d e) is nonzero (*@|)
echo'parallel'"_`calc_and_format@.det_nonzero|: lines
NB. transpose parsed input to get (a d/b e/c f).
NB. If det_nonzero, call calc_and_format, otherwise constant 'parallel'
NB. Print
``````
-

# OCaml + Batteries, 163 characters

As straightforward as it gets:

``````Scanf.scanf"%fx%fy=%f\n%fx%fy=%f\n"Float.(fun a b c d e
f->Printf.(fun u->if u=0.then printf"parallel"else
printf"(%.2f,%.2f)"((b*f-c*e)/u)((d*c-a*f)/u))(b*d-a*e))
``````

Edit:

• Initial version, 169
• Lambda binding of `u`, 163
-

## C-149 bytes

Not much of golfing just the basics.

``````float a,b,c,d,m,n,t;main(){scanf("%fx%fy=%f%fx%fy=%f",&a,&b,&m,&c,&d,&n);t=b*c-a*d;t?printf("(%.2f,%.2f)",(b*n-d*m)/t,(c*m-a*n)/t):puts("parallel");}
``````

Here is the ideone link for testing.

Instead of printing 'parallel' an alternative could be to print the distance between the lines when they are parallel.

-