day24: adding python z3, example
This commit is contained in:
129
day24/lines.go
129
day24/lines.go
@@ -15,7 +15,7 @@ const (
|
||||
)
|
||||
|
||||
type Point struct {
|
||||
x, y, Z int
|
||||
x, y, z int
|
||||
}
|
||||
|
||||
type HailParam struct {
|
||||
@@ -28,6 +28,39 @@ type HailParam struct {
|
||||
slope, shift float64
|
||||
}
|
||||
|
||||
func (h *HailParam) SomeString() string {
|
||||
return h.line
|
||||
}
|
||||
|
||||
func (h *HailParam) GetCoord(name string) (result int) {
|
||||
switch name {
|
||||
case "x":
|
||||
result = h.p0.x
|
||||
case "y":
|
||||
result = h.p0.y
|
||||
case "z":
|
||||
result = h.p0.z
|
||||
default:
|
||||
panic("unknown param")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (h *HailParam) GetSpeedOf(name string) (result int) {
|
||||
switch name {
|
||||
case "x":
|
||||
result = h.Dx
|
||||
case "y":
|
||||
result = h.Dy
|
||||
case "z":
|
||||
result = h.Dz
|
||||
default:
|
||||
panic("unknown param")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func CheckPairwiseIntersections(hails []HailParam) (totalIntersections int) {
|
||||
for i, hail := range hails {
|
||||
@@ -44,10 +77,7 @@ func CheckPairwiseIntersections(hails []HailParam) (totalIntersections int) {
|
||||
|
||||
func CheckTaskIntersection(h1, h2 HailParam) (doIntersect bool) {
|
||||
log.Printf("intersecting %+v and %+v\n", h1, h2)
|
||||
// x, y, intersectAtAll := IntersectByLineEquasions(h1, h2)
|
||||
// x, y, intersectAtAll := IntersectByTwoPoints(h1, h2)
|
||||
// x, y, intersectAtAll := IntersectByTwoPointsAttempt2(h1, h2)
|
||||
// x, y, intersectAtAll := IntersectByTwoPointsAttempt3(h1, h2)
|
||||
x, y, intersectAtAll := IntersectBySlopeAndShift(h1, h2)
|
||||
if !intersectAtAll {
|
||||
log.Println("no intersection at all\n", x, y)
|
||||
@@ -76,79 +106,12 @@ func CheckTaskIntersection(h1, h2 HailParam) (doIntersect bool) {
|
||||
return true
|
||||
}
|
||||
|
||||
// WRONG
|
||||
func IntersectByLineEquasions(h1, h2 HailParam) (intersectionX, intersectionY float64, isIntersecting bool) {
|
||||
D := h1.a*h2.b - h1.b*h2.a
|
||||
if D == 0 {
|
||||
return 0, 0, false
|
||||
}
|
||||
Dx := h1.c*h2.b - h1.b*h2.c
|
||||
Dy := h1.a*h2.c - h1.b*h2.a
|
||||
intersectionX = float64(Dx) / float64(D)
|
||||
intersectionY = float64(Dy) / float64(D)
|
||||
isIntersecting = true
|
||||
func IntersectInTheeDimentions(h1, h2 HailParam) (interX, interY, interZ float64,
|
||||
interT float64, isIntersecting bool) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// WRONG
|
||||
// in 2d only
|
||||
func IntersectByTwoPoints(h1, h2 HailParam) (intersectionX, intersectoinY float64, isIntersecting bool) {
|
||||
p1 := h1.p0
|
||||
p2 := h1.p1
|
||||
p3 := h2.p0
|
||||
p4 := h2.p1
|
||||
|
||||
denominator := (p1.x-p2.x)*(p3.y-p4.y) - (p1.y-p2.y)*(p3.x-p4.x)
|
||||
if denominator == 0 {
|
||||
return 0, 0, false
|
||||
}
|
||||
|
||||
divisibleX := (p1.x*p2.y-p1.y*p2.x)*(p3.x-p4.x) - (p1.x-p2.x)*(p3.x*p4.y-p3.y*p4.x)
|
||||
divisibleY := (p1.x*p2.y-p1.y*p2.x)*(p3.y-p4.y) - (p1.y-p2.y)*(p3.x*p4.y-p3.y*p4.x)
|
||||
|
||||
x := float64(divisibleX) / float64(denominator)
|
||||
y := float64(divisibleY) / float64(denominator)
|
||||
|
||||
return x, y, true
|
||||
}
|
||||
|
||||
func IntersectByTwoPointsAttempt2(h1, h2 HailParam) (intersectionX, intersectionY float64, isIntersecting bool) {
|
||||
x1 := h1.p0.x
|
||||
y1 := h1.p0.y
|
||||
x2 := h1.p1.x
|
||||
y2 := h1.p1.y
|
||||
x3 := h2.p0.x
|
||||
y3 := h2.p0.y
|
||||
x4 := h2.p1.x
|
||||
y4 := h2.p1.y
|
||||
divisor := (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)
|
||||
if divisor == 0 {
|
||||
return
|
||||
}
|
||||
intersectionX = float64((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4)) / float64(divisor)
|
||||
intersectionY = float64((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4)) / float64(divisor)
|
||||
isIntersecting = true
|
||||
return
|
||||
}
|
||||
|
||||
func determinant(a, b Point) int {
|
||||
return a.x * b.y - a.y * b.x
|
||||
}
|
||||
func IntersectByTwoPointsAttempt3(h1, h2 HailParam) (intersectionX, intersectionY float64, isIntersecting bool) {
|
||||
xdiff := Point{ h1.p0.x - h1.p1.x, h2.p0.x - h2.p1.x, 0 }
|
||||
ydiff := Point { h1.p0.y - h1.p1.y, h2.p0.y - h2.p1.y , 0 }
|
||||
|
||||
div := determinant(xdiff, ydiff)
|
||||
if div == 0 {
|
||||
return
|
||||
}
|
||||
d := Point { determinant(h1.p0, h1.p1), determinant(h2.p0, h2.p1), 0}
|
||||
x := float64(determinant(d, xdiff)) / float64(div)
|
||||
y := float64(determinant(d, ydiff)) / float64(div)
|
||||
|
||||
return x, y, true
|
||||
}
|
||||
|
||||
func IntersectBySlopeAndShift(h1, h2 HailParam) (intersectionX, intersectionY float64, isIntersecting bool) {
|
||||
if h1.slope == h2.slope {
|
||||
return
|
||||
@@ -156,9 +119,9 @@ func IntersectBySlopeAndShift(h1, h2 HailParam) (intersectionX, intersectionY fl
|
||||
// y = slope * x + shift
|
||||
// slope1 * x + shift1 = slope2 * x + shift2
|
||||
// x = ( shift2 - shift1 ) / (slope1 - slope2)
|
||||
|
||||
|
||||
x := (h2.shift - h1.shift) / (h1.slope - h2.slope)
|
||||
y := h1.slope * x + h1.shift
|
||||
y := h1.slope*x + h1.shift
|
||||
|
||||
return x, y, true
|
||||
}
|
||||
@@ -166,7 +129,7 @@ func IntersectBySlopeAndShift(h1, h2 HailParam) (intersectionX, intersectionY fl
|
||||
func (h HailParam) PointInFuture(p Point) bool {
|
||||
xPositiveSteps := (p.x-h.p0.x)*h.Dx >= 0
|
||||
yPositiveSteps := (p.y-h.p0.y)*h.Dy >= 0
|
||||
zPositiveSteps := (p.Z-h.p0.Z)*h.Dz >= 0
|
||||
zPositiveSteps := (p.z-h.p0.z)*h.Dz >= 0
|
||||
return xPositiveSteps && yPositiveSteps && zPositiveSteps
|
||||
}
|
||||
func (h HailParam) FloatPointInFuture(x, y float64) bool {
|
||||
@@ -185,16 +148,16 @@ func ReadHailLine(line string) (h HailParam) {
|
||||
|
||||
h.p0.x = AtoIOrPanic(fields[0])
|
||||
h.p0.y = AtoIOrPanic(fields[1])
|
||||
h.p0.Z = AtoIOrPanic(fields[2])
|
||||
h.p0.z = AtoIOrPanic(fields[2])
|
||||
h.Dx = AtoIOrPanic(fields[3])
|
||||
h.Dy = AtoIOrPanic(fields[4])
|
||||
h.Dz = AtoIOrPanic(fields[5])
|
||||
|
||||
countP1AfterMillis := 1
|
||||
|
||||
h.p1.x = h.p0.x + countP1AfterMillis * h.Dx
|
||||
h.p1.y = h.p0.y + countP1AfterMillis * h.Dy
|
||||
h.p1.Z = h.p0.Z + countP1AfterMillis * h.Dz
|
||||
h.p1.x = h.p0.x + countP1AfterMillis*h.Dx
|
||||
h.p1.y = h.p0.y + countP1AfterMillis*h.Dy
|
||||
h.p1.z = h.p0.z + countP1AfterMillis*h.Dz
|
||||
|
||||
h.a = h.p0.y - h.p1.y
|
||||
h.b = h.p1.x - h.p0.x
|
||||
@@ -203,8 +166,8 @@ func ReadHailLine(line string) (h HailParam) {
|
||||
h.slope = float64(h.Dy) / float64(h.Dx)
|
||||
// y = slope * x + shift
|
||||
// shift = y - slope * x // for some point
|
||||
h.shift = float64(h.p0.y) - h.slope * float64(h.p0.x)
|
||||
|
||||
h.shift = float64(h.p0.y) - h.slope*float64(h.p0.x)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user