-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6_part1.go
94 lines (81 loc) · 1.61 KB
/
day6_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
package day6_part1
import (
"strings"
"GoAdventOfCode/util"
)
type Light struct {
On bool
}
type Coords struct {
SX int
SY int
EX int
EY int
}
const (
gridSize = 1000
)
func getAnswer(lines []string) int {
grid := createGrid(gridSize)
for _, line := range lines {
cmd, coords := getCommand(line)
grid = doCommand(cmd, coords, grid)
}
return getNumberOfLights(grid)
}
func createGrid(s int) [][]*Light {
grid := make([][]*Light, 0, s)
for y := 0; y < s; y++ {
row := make([]*Light, 0, s)
for x := 0; x < s; x++ {
light := Light{
On: false,
}
row = append(row, &light)
}
grid = append(grid, row)
}
return grid
}
func getCommand(line string) (string, Coords) {
idx := strings.IndexAny(line, "0123456789")
parts := strings.Fields(line[idx:])
cmd := strings.TrimSpace(line[:idx])
cs := strings.Split(parts[0], ",")
ce := strings.Split(parts[2], ",")
coords := Coords{
SX: util.ConvertStringToInt(cs[0]),
SY: util.ConvertStringToInt(cs[1]),
EX: util.ConvertStringToInt(ce[0]),
EY: util.ConvertStringToInt(ce[1]),
}
return cmd, coords
}
func doCommand(cmd string, pos Coords, grid [][]*Light) [][]*Light {
for y := pos.SY; y <= pos.EY; y++ {
for x := pos.SX; x <= pos.EX; x++ {
light := grid[y][x]
switch cmd {
case "toggle":
light.On = !light.On
case "turn off":
light.On = false
case "turn on":
light.On = true
}
grid[y][x] = light
}
}
return grid
}
func getNumberOfLights(grid [][]*Light) int {
lights := 0
for y := 0; y < gridSize; y++ {
for x := 0; x < gridSize; x++ {
if grid[y][x].On {
lights++
}
}
}
return lights
}