-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3_part1.go
98 lines (84 loc) · 1.62 KB
/
day3_part1.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
package day3_part1
import (
"fmt"
"strings"
"GoAdventOfCode/util"
)
const (
gridSize = 1000
)
type Area struct {
SX int
SY int
EX int
EY int
}
func getAnswer(lines []string) int {
grid := createGrid(gridSize)
for _, line := range lines {
area := getArea(line)
grid = claimArea(area, grid)
//fmt.Printf("%v\n", area)
}
return calculateOverlappingClaims(grid)
}
func createGrid(s int) [][]int {
grid := make([][]int, 0, s)
for y := 0; y < s; y++ {
row := make([]int, 0, s)
for x := 0; x < s; x++ {
row = append(row, 0)
}
grid = append(grid, row)
}
return grid
}
func getArea(line string) Area {
parts := strings.Fields(line)
p1 := strings.TrimSuffix(parts[2], ":")
parts1 := strings.Split(p1, ",")
parts2 := strings.Split(parts[3], "x")
sx := util.ConvertStringToInt(parts1[0])
sy := util.ConvertStringToInt(parts1[1])
area := Area{
SX: sx,
SY: sy,
EX: sx + util.ConvertStringToInt(parts2[0]) - 1,
EY: sy + util.ConvertStringToInt(parts2[1]) - 1,
}
return area
}
func claimArea(area Area, grid [][]int) [][]int {
for y := 0; y < gridSize; y++ {
for x := 0; x < gridSize; x++ {
if x >= area.SX && x <= area.EX &&
y >= area.SY && y <= area.EY {
grid[y][x]++
}
}
}
return grid
}
func calculateOverlappingClaims(grid [][]int) int {
n := 0
for y := 0; y < gridSize; y++ {
for x := 0; x < gridSize; x++ {
if grid[y][x] >= 2 {
n++
}
}
}
return n
}
func printGrid(grid [][]int) {
for y := 0; y < gridSize; y++ {
for x := 0; x < gridSize; x++ {
if grid[y][x] == 0 {
fmt.Print(".")
} else {
fmt.Printf("%d", grid[y][x])
}
}
fmt.Println()
}
}