-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3_1.go
63 lines (58 loc) · 962 Bytes
/
day3_1.go
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
62
63
package main
import (
"fmt"
"io"
"log"
)
func main() {
claims := readClaims()
w, h := totalDim(claims)
mark := make([]int, w*h)
for _, c := range claims {
for i := c.Top; i < c.Top+c.Height; i++ {
for j := c.Left; j < c.Left+c.Width; j++ {
mark[i*w+j] += 1
}
}
}
var total int
for _, m := range mark {
if m > 1 {
total++
}
}
fmt.Println(total)
}
type claim struct {
ID int
Left int
Top int
Width int
Height int
}
func readClaims() []claim {
var out []claim
for {
var c claim
_, err := fmt.Scanf("#%d @ %d,%d: %dx%d", &c.ID, &c.Left, &c.Top, &c.Width, &c.Height)
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
}
if err != nil {
log.Fatal(err)
}
out = append(out, c)
}
return out
}
func totalDim(claims []claim) (w, h int) {
for _, c := range claims {
if c.Left+c.Width > w {
w = c.Left + c.Width
}
if c.Top+c.Height > h {
h = c.Top + c.Height
}
}
return w, h
}