package day24 import ( "strings" "gitea.paas.celticinfo.fr/oabrivard/aoc2023/utils" ) type Hailstone struct { px, py, pz, vx, vy, vz int64 } type Line struct { m, b float64 } func getLine(h Hailstone) Line { y2 := h.py + h.vy x2 := h.px + h.vx m := float64((y2 - h.py)) / float64((x2 - h.px)) b := float64(y2) - m*float64(x2) return Line{m, b} } func crossCoord(h1, h2 Hailstone) (bool, float64, float64) { l1 := getLine(h1) l2 := getLine(h2) if l1.m == l2.m { return false, -1, -1 } x := (l2.b - l1.b) / (l1.m - l2.m) y := l2.m*x + l2.b if h1.px > int64(x) && h1.vx >= 0 || h1.px < int64(x) && h1.vx <= 0 { return false, -1, -1 } if h2.px > int64(x) && h2.vx >= 0 || h2.px < int64(x) && h2.vx <= 0 { return false, -1, -1 } return true, x, y } func Part1(lines []string, min, max float64) int { stones := []Hailstone{} for _, line := range lines { values := utils.ParseInt64Array(strings.Replace(line, " @", ",", -1), ", ") stone := Hailstone{values[0], values[1], values[2], values[3], values[4], values[5]} stones = append(stones, stone) } result := 0 for i := range stones { for j := i + 1; j < len(stones); j++ { areCrossing, x, y := crossCoord(stones[i], stones[j]) if !areCrossing { continue } if x >= min && y >= min && x <= max && y <= max { result++ } //fmt.Println("x : ", x, ", y : ", y) } } return result }