aboutsummaryrefslogtreecommitdiff
path: root/2021/05/second.go
blob: c79025a09c535d4fa62b3ee9bb362faf8f57d54a (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
61
package main

import (
	"bufio"
	"fmt"
	"os"

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

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
		}
		if l.X1 == l.X2 {
			if l.Y1 > l.Y2 {
				l.Y1, l.Y2 = l.Y2, l.Y1
			}
			for i := l.Y1; i <= l.Y2; i++ {
				matrix[l.X1][i]++
			}
		} else if l.Y1 == l.Y2 {
			if l.X1 > l.X2 {
				l.X1, l.X2 = l.X2, l.X1
			}
			for i := l.X1; i <= l.X2; i++ {
				matrix[i][l.Y1]++
			}
		} else {
			if l.X1 > l.X2 {
				l.X1, l.X2, l.Y1, l.Y2 = l.X2, l.X1, l.Y2, l.Y1
			}
			if l.Y1 < l.Y2 {
				for i := 0; i <= l.X2-l.X1; i++ {
					matrix[l.X1+i][l.Y1+i]++
				}
			} else {
				for i := 0; i <= l.X2-l.X1; i++ {
					matrix[l.X1+i][l.Y1-i]++
				}
			}
		}
	}
	score := 0
	for i := 0; i < 1000; i++ {
		for j := 0; j < 1000; j++ {
			if matrix[i][j] >= 2 {
				score++
			}
		}
	}
	fmt.Println(score)
}