-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.go
124 lines (102 loc) · 2.25 KB
/
day3.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"log"
"strconv"
"strings"
)
// Claim simple struct
type Claim struct {
claimID int
leftOffset int
topOffset int
width int
height int
}
// NewClaim parse a new claim
func NewClaim(line string) *Claim {
c := new(Claim)
claimParts := strings.Split(line, " ")
// ClaimID
idStr := strings.TrimPrefix(claimParts[0], "#")
id, err := strconv.Atoi(idStr)
if err != nil {
log.Fatalf("could not convert %s, %s\n", idStr, err)
}
c.claimID = id
offsets := strings.Split(strings.TrimSuffix(claimParts[2], ":"), ",")
// Left
leftOffset, err := strconv.Atoi(offsets[0])
if err != nil {
log.Fatalf("could not convert %s, %s\n", offsets[0], err)
}
c.leftOffset = leftOffset
// Top
topOffset, err := strconv.Atoi(offsets[1])
if err != nil {
log.Fatalf("could not convert %s, %s\n", offsets[1], err)
}
c.topOffset = topOffset
sizes := strings.Split(claimParts[3], "x")
// Width
width, err := strconv.Atoi(sizes[0])
if err != nil {
log.Fatalf("could not convert %s, %s\n", sizes[0], err)
}
c.width = width
// Height
height, err := strconv.Atoi(sizes[1])
if err != nil {
log.Fatalf("could not convert %s, %s\n", sizes[1], err)
}
c.height = height
return c
}
func (c Claim) grid(g [][]int) [][]int {
if cap(g) < c.topOffset+c.height {
newSize := c.topOffset + c.height
newSlice := make([][]int, newSize, newSize)
copy(newSlice, g)
g = newSlice
}
for i := c.topOffset; i < c.topOffset+c.height; i++ {
if cap(g[i]) < c.leftOffset+c.width {
newSize := c.leftOffset + c.width
newSlice := make([]int, newSize, newSize)
copy(newSlice, g[i])
g[i] = newSlice
}
for j := c.leftOffset; j < c.leftOffset+c.width; j++ {
g[i][j]++
}
}
return g
}
// CountIntersection squares
func CountIntersection(g [][]int) int {
var count int
for i := range g {
for j := range g[i] {
if g[i][j] > 1 {
count++
}
}
}
return count
}
func scanDay3File() []string {
lines, err := FileInput(3)
if err != nil {
log.Fatalf("failed to get data, %s", err)
}
return lines
}
func day3() int {
lines := scanDay3File()
grid := make([][]int, 0, 0)
for _, line := range lines {
claim := NewClaim(line)
grid = claim.grid(grid)
}
intersection := CountIntersection(grid)
return intersection
}