day24, already bad
This commit is contained in:
parent
b6a56554af
commit
d749979aae
110
day24/lines.go
110
day24/lines.go
|
@ -8,8 +8,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CoordMin int = 7
|
// CoordMin int = 7
|
||||||
CoordMax int = 27
|
// CoordMax int = 27
|
||||||
|
CoordMin int = 200000000000000
|
||||||
|
CoordMax int = 400000000000000
|
||||||
)
|
)
|
||||||
|
|
||||||
type Point struct {
|
type Point struct {
|
||||||
|
@ -17,17 +19,20 @@ type Point struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type HailParam struct {
|
type HailParam struct {
|
||||||
p0, p1 Point
|
p0, p1 Point
|
||||||
Dx, Dy, Dz int
|
Dx, Dy, Dz int
|
||||||
line string
|
line string
|
||||||
|
// for 2d : ay + bx + c = 0
|
||||||
|
a, b, c int
|
||||||
}
|
}
|
||||||
func (h HailParam)String() string {
|
|
||||||
|
func (h HailParam) String() string {
|
||||||
return "(" + h.line + ")"
|
return "(" + h.line + ")"
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckPairwiseIntersections(hails []HailParam) (totalIntersections int) {
|
func CheckPairwiseIntersections(hails []HailParam) (totalIntersections int) {
|
||||||
for i, hail := range hails {
|
for i, hail := range hails {
|
||||||
for j := i+1; j < len(hails); j++ {
|
for j := i + 1; j < len(hails); j++ {
|
||||||
otherHail := hails[j]
|
otherHail := hails[j]
|
||||||
intersect := CheckTaskIntersection(hail, otherHail)
|
intersect := CheckTaskIntersection(hail, otherHail)
|
||||||
if intersect {
|
if intersect {
|
||||||
|
@ -40,34 +45,51 @@ func CheckPairwiseIntersections(hails []HailParam) (totalIntersections int) {
|
||||||
|
|
||||||
func CheckTaskIntersection(h1, h2 HailParam) (doIntersect bool) {
|
func CheckTaskIntersection(h1, h2 HailParam) (doIntersect bool) {
|
||||||
log.Printf("intersecting %+v and %+v\n", h1, h2)
|
log.Printf("intersecting %+v and %+v\n", h1, h2)
|
||||||
x, y, intersectAtAll := IntersectByTwoPoints(h1, h2)
|
// x, y, intersectAtAll := IntersectByLineEquasions(h1, h2)
|
||||||
|
// x, y, intersectAtAll := IntersectByTwoPoints(h1, h2)
|
||||||
|
x, y, intersectAtAll := IntersectByTwoPointsAttempt2(h1, h2)
|
||||||
if !intersectAtAll {
|
if !intersectAtAll {
|
||||||
log.Printf("no intersection at all\n")
|
log.Printf("no intersection at all\n")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
isH1Future := h1.FloatPointInFuture(x, y)
|
||||||
|
isH2Future := h2.FloatPointInFuture(x, y)
|
||||||
|
|
||||||
|
if !isH1Future {
|
||||||
|
log.Printf("point %f, %f in the past for h1\n", x, y)
|
||||||
|
}
|
||||||
|
if !isH2Future {
|
||||||
|
log.Printf("point %f, %f in the past for h2\n", x, y)
|
||||||
|
}
|
||||||
|
if !isH1Future || !isH2Future {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
if x < float64(CoordMin) || x > float64(CoordMax) ||
|
if x < float64(CoordMin) || x > float64(CoordMax) ||
|
||||||
y < float64(CoordMin) || y > float64(CoordMax) {
|
y < float64(CoordMin) || y > float64(CoordMax) {
|
||||||
log.Printf("intersect at %f %f but outside of area\n", x, y)
|
log.Printf("intersect at %f %f but outside of area\n", x, y)
|
||||||
return false // outside of area
|
return false // outside of area
|
||||||
}
|
}
|
||||||
isH1Future := h1.FloatPointInFuture(x,y)
|
|
||||||
isH2Future := h2.FloatPointInFuture(x,y)
|
|
||||||
|
|
||||||
if !isH1Future {
|
log.Println("> intersect inside of the area!")
|
||||||
log.Printf("in the past for h1\n")
|
|
||||||
}
|
|
||||||
if !isH2Future {
|
|
||||||
log.Printf("in the past for h2\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !isH1Future || !isH2Future {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("intersect inside of the area")
|
|
||||||
return true
|
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
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// WRONG
|
||||||
// in 2d only
|
// in 2d only
|
||||||
func IntersectByTwoPoints(h1, h2 HailParam) (intersectionX, intersectoinY float64, isIntersecting bool) {
|
func IntersectByTwoPoints(h1, h2 HailParam) (intersectionX, intersectoinY float64, isIntersecting bool) {
|
||||||
p1 := h1.p0
|
p1 := h1.p0
|
||||||
|
@ -75,13 +97,13 @@ func IntersectByTwoPoints(h1, h2 HailParam) (intersectionX, intersectoinY float6
|
||||||
p3 := h2.p0
|
p3 := h2.p0
|
||||||
p4 := h2.p1
|
p4 := h2.p1
|
||||||
|
|
||||||
denominator := (p1.x - p2.x)*(p3.y - p4.y) - (p1.y - p2.y)*(p3.x - p4.x)
|
denominator := (p1.x-p2.x)*(p3.y-p4.y) - (p1.y-p2.y)*(p3.x-p4.x)
|
||||||
if denominator == 0 {
|
if denominator == 0 {
|
||||||
return 0, 0, false
|
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)
|
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)
|
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)
|
x := float64(divisibleX) / float64(denominator)
|
||||||
y := float64(divisibleY) / float64(denominator)
|
y := float64(divisibleY) / float64(denominator)
|
||||||
|
@ -89,16 +111,36 @@ func IntersectByTwoPoints(h1, h2 HailParam) (intersectionX, intersectoinY float6
|
||||||
return x, y, true
|
return x, y, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h HailParam)PointInFuture(p Point) bool {
|
func IntersectByTwoPointsAttempt2(h1, h2 HailParam) (intersectionX, intersectionY float64, isIntersecting bool) {
|
||||||
xPositiveSteps := (p.x - h.p0.x) * h.Dx >= 0
|
x1 := h1.p0.x
|
||||||
yPositiveSteps := (p.y - h.p0.y) * h.Dy >= 0
|
y1 := h1.p0.y
|
||||||
zPositiveSteps := (p.Z - h.p0.Z) * h.Dz >= 0
|
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 (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
|
||||||
return xPositiveSteps && yPositiveSteps && zPositiveSteps
|
return xPositiveSteps && yPositiveSteps && zPositiveSteps
|
||||||
}
|
}
|
||||||
func (h HailParam)FloatPointInFuture(x, y float64) bool {
|
func (h HailParam) FloatPointInFuture(x, y float64) bool {
|
||||||
xPositiveSteps := (x - float64(h.p0.x)) * float64(h.Dx) >= 0
|
xPositiveSteps := (x-float64(h.p0.x))*float64(h.Dx) >= 0
|
||||||
yPositiveSteps := (y - float64(h.p0.y)) * float64(h.Dy) >= 0
|
// yPositiveSteps := (y - float64(h.p0.y)) * float64(h.Dy) >= 0
|
||||||
return xPositiveSteps && yPositiveSteps
|
// return xPositiveSteps && yPositiveSteps
|
||||||
|
return xPositiveSteps
|
||||||
}
|
}
|
||||||
|
|
||||||
// 19, 13, 30 @ -2, 1, -2
|
// 19, 13, 30 @ -2, 1, -2
|
||||||
|
@ -119,6 +161,10 @@ func ReadHailLine(line string) (h HailParam) {
|
||||||
h.p1.y = h.p0.y + h.Dy
|
h.p1.y = h.p0.y + h.Dy
|
||||||
h.p1.Z = h.p0.Z + h.Dz
|
h.p1.Z = h.p0.Z + h.Dz
|
||||||
|
|
||||||
|
h.a = h.p0.y - h.p1.y
|
||||||
|
h.b = h.p1.x - h.p0.x
|
||||||
|
h.c = -(h.p0.x*h.p1.y - h.p1.x*h.p0.y)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
func Run() int {
|
func Run() int {
|
||||||
fmt.Println("hello day 24, i'm getting tired")
|
fmt.Println("hello day 24, i'm getting tired")
|
||||||
filenae := "day24/example"
|
filenae := "day24/input"
|
||||||
hails := ReadHailFile(filenae)
|
hails := ReadHailFile(filenae)
|
||||||
return CheckPairwiseIntersections(hails)
|
return CheckPairwiseIntersections(hails)
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,3 +27,7 @@ no, i don't understant that
|
||||||
with 2 points. i guess
|
with 2 points. i guess
|
||||||
but also - check if the point in future of the hail, by comparing with speeds?
|
but also - check if the point in future of the hail, by comparing with speeds?
|
||||||
should be easy
|
should be easy
|
||||||
|
* and i got wrong result
|
||||||
|
day24 result: 8406
|
||||||
|
* another formula gives
|
||||||
|
day24 result: 8406
|
||||||
|
|
Loading…
Reference in New Issue