aboutsummaryrefslogtreecommitdiff
path: root/2021/05/first_v2.go
blob: d99f80b978c15e2b75f7717ed8c520239ded6f1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main

import (
	"bufio"
	"fmt"
	"os"

	"git.adyxax.org/aoc/2021/05/line"
)

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func computeDelta(a, b int) int {
	if a < b {
		return 1
	} else if a > b {
		return -1
	}
	return 0
}

func main() {
	matrix := make([][]int, 1000)
	for i := 0; i < 1000; i++ {
		matrix[i] = make([]int, 1000)
	}

	parser := line.NewParser(bufio.NewReader(os.Stdin))
	for {
		l, err := parser.Parse()
		if err != nil {
			break
		}
		length := abs(l.X2 - l.X1)
		if length == 0 {
			length = abs(l.Y2 - l.Y1)
		}
		dx := computeDelta(l.X1, l.X2)
		dy := computeDelta(l.Y1, l.Y2)
		if dx == 0 || dy == 0 {
			for i := 0; i <= length; i++ {
				matrix[l.X1+i*dx][l.Y1+i*dy]++
			}
		}
	}
	score := 0
	for i := 0; i < 1000; i++ {
		for j := 0; j < 1000; j++ {
			if matrix[i][j] >= 2 {
				score++
			}
		}
	}
	fmt.Println(score)
}