# 1P5: Earthquake!

USGS's seismometers have just detected a major earthquake! Emergency response teams need a quick estimate of the number of people affected. Write a program to compute this estimate.

Your program receives 2 inputs. The first is the details of the earthquake itself. The earthquake modeled as a line segment along which the earth ruptured, along with the critical distance from the fault within which damage could be expected. The second input is a list of the location and population of cities in the area. Your program should compute the number of people living in the affected area, that is the sum of the population of all cities within the critical distance of the fault segment.

## Input

First a line describing the earthquake, containing the x,y coordinates of the start and end points of the fault, plus the critical distance. The format is A_x A_y B_x B_y D. For example:

3.0 3.0 7.0 4.0 2.5


encodes the fault extending from (3.0,3.0) to (7.0,4.0) and a critical distance of 2.5.

Second, one line per city in the area, containing the x,y coordinates of the city and its population. For example:

1.0 1.0 2500
5.0 7.0 8000
3.0 4.0 7500
9.0 6.0 3000
4.0 2.0 1000 ## Output

The number of people living in the affected area. For the above example only the third and fifth cities are in the danger area, so the output would be

8500


Shortest code wins.

### Example 2

0.0 0.0 10.0 0.0 5.0
5.0 4.0 10000
5.0 -4.0 1000
5.0 6.0 100
11.0 2.0 10
-4.0 4.0 1


generates

11010

• Does the output have to be an integer, or would 8500.0 be ok for the example? And could we maybe get some more testcases? May 8, 2011 at 12:21
• Also, what is the type of the input? As in, how many decimal places could it have? This is relevant for languages which don't have floating point. May 8, 2011 at 12:46
• The output needs to be an integer, can't have fractional people. Let's say the input is at most 2 decimal places. May 8, 2011 at 14:33
• Does the critical area extend in a semicircle around the endpoints, or is it just a rectangle? May 8, 2011 at 14:37
• @Peter: It ends in semicircles, so the whole thing is lozenge-shaped. May 8, 2011 at 17:24

## Ruby, 171152155 153

u,v,a,b,d=gets.split.map &:to_f
a-=u;b-=v
p eval$<.map{|l|"(x=%f-u;t=(a*x+b*y=%f-v)/(a*a+b*b);d*d<(x-a*t=t<0?0:t>1?1:t)**2+(y-t*b)**2?0:%d)"%l.split}*'+'  This is my first ruby submission and my first code-golf at all. Straight forward implementation of the task. Please give me some hints how to improve (there must be a shorter way to read floats...). • You can save a few characters by eliminating the parentheses around map and eval, and inlining t. And since you're already eval'ing, you can use format-strings instead of .to_f, so the last block can be shortened to p eval$<.map{|l|"(x=%f-u;t=(x*a+b*y=%f-v)/(a**2+b**2);d*d<(x-t=t<0?0:t>1?1:t)**2+(y-t*b)**2?0:%d)"%l.split}*'+' May 8, 2011 at 17:29
• @Ventero thank you. Inlining t again saves another two. May 8, 2011 at 17:46
• I can't get this to work on example #2 (just added), it errors with undefined method > for nil:NilClass (NoMethodError) May 9, 2011 at 3:42
• @Keith should work now on your input as well. May 9, 2011 at 4:55

## Javascript (437)

This can probably be golfed significantly, but not enough to beat the Ruby solution.

p=\$("#i").text().split("\n");for(i=0;i<p.length;i++){p[i]=p[i].split(" ")}
z=p;a=z;b=z;c=z;d=z;e=z;o=0;f=[a,b];g=[c,d];
function q(r,s){return Math.sqrt(Math.pow(s-r,2)+Math.pow(s-r,2))}
for(i=1;i<p.length;i++){w=p[i];u=((w-a)*(c-a)+(w-b)*(d-b))/Math.pow(q(f,g,2),2);
x=[(a*1)+u*(c-a),(b*1)+u*(d-b)];l=e;m=w*1;u=w;w=w;v=[u,w];


You can see it in action here.

• You can save 2 characters by using b-0 instead of (b*1), 1 character by removing the trailing semicolon. Finally, start with M=Math and replace all Maths with M, saving 6 characters. Using Prototype and .value saves 2 characters (the # and one parenthesis).
– Ry-
May 13, 2011 at 3:46

### C# - 743 715

namespace System{using Linq;using m=Math;class P{public float X,Y;}class E{static void Main(){Func<string,float>p=s=>float.Parse(s);Func<P,P,double>d=(a,b)=>{return a.X*b.X+a.Y*b.Y;},c=(a,b)=>{return a.X*b.Y-a.Y*b.X;};Func<P,P,P>u=(a,b)=>{return new P{X=a.X-b.X,Y=a.Y-b.Y};};Func<P,P,P,double>g=(A,B,C)=>{return d(u(C,B),u(B,A))>0?m.Sqrt(d(u(B,C),u(B,C))):d(u(C,A),u(A,B))>0?m.Sqrt(d(u(A,C),u(A,C))):m.Abs(c(u(B,A),u(C,A))/m.Sqrt(d(u(B,A),u(B,A))));};var n=IO.File.ReadAllLines("i");var i=n.Split();var q=new{A=new P{X=p(i),Y=p(i)},B=new P{X=p(i),Y=p(i)},D=p(i)};Console.WriteLine((from l in n.Skip(1)let f=l.Split()let w=new P{X=p(f),Y=p(f)}where g(q.A,q.B,w)<q.D select p(f)).Sum());}}}


Non-Golfed:

namespace System
{
using Linq;
using m = Math;
class Point { public float X, Y;}
class Earthquake
{
static void Main()
{
Func<string, float> parse = s => float.Parse(s);
Func<Point, Point, double> dotProduct = (a, b) => { return a.X * b.X + a.Y * b.Y; },
crossProduct = (a, b) => { return a.X * b.Y - a.Y * b.X; };
Func<Point, Point, Point> subtract = (a, b) => { return new Point { X = a.X - b.X, Y = a.Y - b.Y }; };
Func<Point, Point, Point, double> getDistance = (A, B, C) => {
return dotProduct(subtract(C, B), subtract(B, A)) > 0 ?
m.Sqrt(dotProduct(subtract(B, C), subtract(B, C))) :
dotProduct(subtract(C, A), subtract(A, B)) > 0 ?
m.Sqrt(dotProduct(subtract(A, C), subtract(A, C))) :
m.Abs(crossProduct(subtract(B, A), subtract(C, A)) / m.Sqrt(dotProduct(subtract(B, A), subtract(B, A))));
};
var quakeLine = inputLines.Split();
var quake = new {
PointA = new Point { X = parse(quakeLine), Y = parse(quakeLine) },
PointB = new Point { X = parse(quakeLine), Y = parse(quakeLine) },
Distance = parse(quakeLine)
};
var affectedPopulations = (from line in inputLines.Skip(1)
let fields = line.Split()
let location = new Point { X = parse(fields), Y = parse(fields) }
let population = parse(fields)
where getDistance(quake.PointA, quake.PointB, location) < quake.Distance
select population);
Console.WriteLine(affectedPopulations.Sum());
}
}
}

• Something is wrong with the golfed version, I get an error when I compile it (quake.cs(1,254): error CS1525: Unexpected symbol ',', expecting 'from', 'group', 'join', 'let', 'orderby', 'select', or 'where'). The ungolfed version works fine. May 9, 2011 at 3:52
• @KeithRandall, oops - too much golfing! May 9, 2011 at 4:19

## c -- 471 characters

#include <stdio.h>
#define F float
#define G getline(&v,&l,stdin)
F a,b,c,d,e,r,t,y,z;char*v;size_t l,n,p;
F s(F u,F v){y=u-v;z=u-v;return y*y+z*z;}
j(F g,F h,F i){*i=*g-*h;i=g-h;}
int i(){j(b,a,d);j(c,a,e);t=*d**e+d*e;
return s(a,c)<=r||s(b,c)<=r||t>0&&t/s(a,b)<=1&&s(a,c)-t*t/s(a,b)<=r;}
int main(){G;sscanf(v,"%f %f %f %f %f",a,a+1,b,b+1,&r);r*=r;
while(G!=-1)sscanf(v,"%f %f %i",c,c+1,&p),n+=p*i();printf("%d\n",n);}


It assumes your standard library has getline.

The method is clarified a bit in the comment to the ungolfed version:

#include <stdio.h>

float a,b,c,d,e,r,t,y,z;
char*v;
size_t l,n,p;
float s(float u,float v){ /* returns the square of the distance
between two points */
y=u-v;
z=u-v;
return y*y+z*z;
}
j(float g,float h,float i){ /* sets i=g-h */
i=g-h;
i=g-h;
}
int i/*sCLose*/(){
j(b,a,d); /* d=b-a */
j(c,a,e); /* e=c-a */
t=d*e+d*e; /* dot product */
return
(s(a,c)<=r) || /* near one end point */
(s(b,c)<=r) || /* near the other */
(
(t>0) && /* C lies more "towards" B than away */
(t/s(a,b)<=1) && /* Nearest point on AB to C lies between A and B */
(s(a,c)-t*t/s(a,b)<=r) /* length of the altitude less than R */
);
}
int main(){
getline(&v,&l,stdin);
sscanf(v,"%f %f %f %f %f",a,a+1,b,b+1,&r);
r*=r; /* r is now r squared, as that is the only way we use it */
printf("(%f, %f); (%f, %f): %f\n",a,a,b,b,r);
while (getline(&v,&l,stdin) != -1){
sscanf(v,"%f %f %i",c,c+1,&p);
printf("\t (%f, %f): %d\n",c,c,p);
n+=p*i/*sClose*/();
}
printf("%d\n",n);
}


### scala: 660 chars:

object E extends App{
type I=Int
type D=Double
def b(h:D,i:D,x:I,y:I,d:D)=(x-h)*(x-h)+(y-i)*(y-i)<=d*d
def a(p:java.awt.Polygon,x:I,y:I,h:I,i:I,d:D,r:Array[String])={
val w=r(0).toDouble
val j=r(1).toDouble
val n=r(2).toInt
if (p.contains(w,j)||b(j,w,x,y,d)||b(j,w,i,h,d))n
else 0}
val s=new java.util.Scanner(System.in)
val b=s.nextLine.split(" ")
val c=b.map(_.toDouble)
val e=c.map(_.toInt)
val(x,h,y,i,d)=(e(0),e(2),e(1),e(3),c(4))
val f=List(x,h)
val g=List(y,i)
val p=new java.awt.Polygon((f:::f.reverse).toArray,(g.map(_-e(4)):::g.reverse.map(_+e(4))).toArray,4)
var r=0
while(s.hasNext){
val row=s.nextLine
r+=a(p,x,y,h,i,d,row.split(" "))}
println(r)}


ungolfed:

object Earthquake extends App {

def bowContains (h: Double, i: Double, x:Int, y:Int, d: Double) : Boolean = {
(x-h)*(x-h) + (y-i)*(y-i) <= d*d
}

import java.awt._

def affected (polygon: Polygon, x:Int, y:Int, h: Int, i: Int, d: Double, row: Array[String]) : Int = {
val w = row (0).toDouble
val j = row (1).toDouble
val population = row (2).toInt
if (polygon.contains (w, j) || bowContains (j, w, x, y, d) || bowContains (j, w, i, h, d))
population
else 0
}
val sc = new java.util.Scanner (System.in)
val line = sc.nextLine.split (" ")

val li = line.map (_.toDouble)
val ll = li.map (_.toInt)

val (x, h, y, i, d) = (ll (0), ll (2), ll (1), ll (3), li(4))
val xs = List (x, h)
val ys = List (y, i)

val polygon = new Polygon ((xs ::: xs.reverse).toArray, (ys.map (_ - ll(4)) ::: ys.reverse.map (_ + ll(4))).toArray, 4)
var res = 0
while (sc.hasNext) {
val row = sc.nextLine
println ("line: " + line)
res += affected (polygon, x, y, h, i, d, row.split (" "))
}
println (res)
}