advent-of-code-2021/5/part1.go
2021-12-12 11:03:48 +01:00

57 lines
780 B
Go

package main
import (
"bufio"
"fmt"
"os"
)
type Matrix map[int64]int
func (m Matrix) Increment(x, y int) {
z := int64(x) << 32 | int64(y)
m[z]++
}
func (m Matrix) DrawLine(x1, y1, x2, y2 int) {
x := x1
y := y1
for x != x2 || y != y2 {
m.Increment(x, y)
if x1 > x2 {
x--
} else if x1 < x2 {
x++
}
if y1 > y2 {
y--
} else if y1 < y2 {
y++
}
}
m.Increment(x, y)
}
func main() {
m := Matrix{}
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
var x1, x2, y1, y2 int
_, _ = fmt.Sscanf(s.Text(), "%d,%d -> %d,%d", &x1, &y1, &x2, &y2)
// consider only horizontal or vertical lines
if x1 == x2 || y1 == y2 {
m.DrawLine(x1, y1, x2, y2)
}
}
sum := 0
for _, count := range m {
if count > 1 {
sum++
}
}
fmt.Println(sum)
}